Search Unity

Texture Tiling broken in Unity 2019.2 LWRP

Discussion in 'Documentation' started by Derik-Stavast, Aug 15, 2019.

  1. Derik-Stavast

    Derik-Stavast

    Joined:
    Aug 24, 2013
    Posts:
    4
    I am trying to make a texture tile through a script based on my object's scale, in Unity 2018.30f2 it works great but the same code does nothing in Unity 2019.2 using the LWRP.

    As mentioned the code below works in Unity 2018.30f2 but not in Unity 2019.2 using the LWRP.
    and according to the documentation, this is the way to do it?

    This is my code:

    Code (CSharp):
    1. cube.GetComponent<Renderer>().material.mainTextureScale = new Vector2( 1.5f, 1.5f);
    This is the documentation Code:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Example : MonoBehaviour
    4. {
    5.     Renderer rend;
    6.  
    7.     void Start()
    8.     {
    9.         rend = GetComponent<Renderer>();
    10.     }
    11.  
    12.     void Update()
    13.     {
    14.         // Animates main texture scale in a funky way!
    15.         float scaleX = Mathf.Cos(Time.time) * 0.5f + 1;
    16.         float scaleY = Mathf.Sin(Time.time) * 0.5f + 1;
    17.         rend.material.mainTextureScale = new Vector2(scaleX, scaleY);
    18.     }
    19. }
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    4,283
    A bunch of the properties on materials, like .color and .mainTexture, makes assumptions about what the names of the shader properties are. mainTextureScale is implemented like this:

    Code (csharp):
    1. public Vector2 mainTextureScale
    2. {
    3.   get
    4.   {
    5.     return this.GetTextureScale("_MainTex");
    6.   }
    7.   set
    8.   {
    9.     this.SetTextureScale("_MainTex", value);
    10.   }
    11. }
    Which breaks if the main texture is not named "_MainTex".

    I'm guessing that the default shader in LWRP doesn't name it's main texture "_MainTex".
     
    Derik-Stavast likes this.
  3. Derik-Stavast

    Derik-Stavast

    Joined:
    Aug 24, 2013
    Posts:
    4
    You are 100% correct! I fixed this issue by calling "_BaseMap" as the string name of the texture instead of "_MainTex".
    Code looks like this:

    Code (CSharp):
    1.  rend.material.SetTextureScale("_BaseMap", new Vector2(5,5));