Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

See result of changing material tiling in editor

Discussion in 'Scripting' started by raycosantana, Aug 26, 2014.

  1. raycosantana

    raycosantana

    Joined:
    Dec 23, 2012
    Posts:
    319
    Hi

    Well I was tired of having 200 million different materials with the same texture, so I made a cube on Maya 2013 with 6 sloths for materials and then I created this script which allows me to apply different tiling for each face of the cube, it works great as you can see



    However I thought it would be cool to see the result directly when editing the scene instead of having to press play to see the result, is this possible?

    Here is the script I made

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ShaderTiling : MonoBehaviour {
    5.     public float XTilingFront;
    6.     public float YTilingFront;
    7.     public float XTilingTop;
    8.     public float YTilingTop;
    9.     public float XTilingBack;
    10.     public float YTilingBack;
    11.     public float XTilingBottom;
    12.     public float YTilingBottom;
    13.     public float XTilingLeft;
    14.     public float YTilingLeft;
    15.     public float XTilingRight;
    16.     public float YTilingRight;
    17.     void Awake(){
    18.         gameObject.renderer.materials[0].mainTextureScale = new Vector2(XTilingFront, YTilingFront);
    19.         gameObject.renderer.materials[0].SetTextureScale("_BumpMap", new Vector2(XTilingFront, YTilingFront));
    20.         gameObject.renderer.materials[1].mainTextureScale = new Vector2(XTilingTop, YTilingTop);
    21.         gameObject.renderer.materials[1].SetTextureScale("_BumpMap", new Vector2(XTilingTop, YTilingTop));
    22.         gameObject.renderer.materials[2].mainTextureScale = new Vector2(XTilingBack, YTilingBack);
    23.         gameObject.renderer.materials[2].SetTextureScale("_BumpMap", new Vector2(XTilingBack, YTilingBack));
    24.         gameObject.renderer.materials[3].mainTextureScale = new Vector2(XTilingBottom, YTilingBottom);
    25.         gameObject.renderer.materials[3].SetTextureScale("_BumpMap", new Vector2(XTilingBottom, YTilingBottom));
    26.         gameObject.renderer.materials[4].mainTextureScale = new Vector2(XTilingLeft, YTilingLeft);
    27.         gameObject.renderer.materials[4].SetTextureScale("_BumpMap", new Vector2(XTilingLeft, YTilingLeft));
    28.         gameObject.renderer.materials[5].mainTextureScale = new Vector2(XTilingRight, YTilingRight);
    29.         gameObject.renderer.materials[5].SetTextureScale("_BumpMap", new Vector2(XTilingRight, YTilingRight));
    30.      
    31.  
    32.     }
    33.     // Use this for initialization
    34.     void Start () {
    35.  
    36.     }
    37.  
    38.     // Update is called once per frame
    39.     void Update () {
    40.  
    41.     }
    42. }
    43.  
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Not so much
    When you do this at run time, you actually create 6 new material instances, if you did this at edit time, you would end up with
    You could instead have a single material with a consistent texture scale, but change the UVs on the mesh. But then you'd need to store and reference a different cube mesh for every different object.
     
    raycosantana likes this.
  3. raycosantana

    raycosantana

    Joined:
    Dec 23, 2012
    Posts:
    319
    Well seeing the alternatives, I think I will leave it as it is.
     
  4. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    There is a way that you could preview the effects in the editor, without the permanent creation of all the additional materials.
    If you only create the material instances while (for example) a custom editor window is open, and then destroy the extra materials when you are done with the preview, you avoid all the excess. The object reverts to its default appearance when you aren't previewing though, and if you forget to reset properly, you'll probably get a bunch of materials in memory that aren't used (though these will get cleaned up by Unity eventually)
     
    raycosantana likes this.
  5. raycosantana

    raycosantana

    Joined:
    Dec 23, 2012
    Posts:
    319
    I see that's a good idea, however since I know exactly how much I have to tile each object (the cube is exactly 1x1x1units and in the game everything is perfectly square) for me this too much hassle for so little gain so I will leave it as it is.

    I think I could even automate everything by just using Scale as input and adding a multiplier for especial cases.

    Thanks for you help!