Search Unity

Multiple materials on same GameObject

Discussion in 'Scripting' started by pocce90, Sep 5, 2017.

  1. pocce90

    pocce90

    Joined:
    Mar 21, 2015
    Posts:
    16
    Hello everybody, maybe you can help me: I need to set different materials on the same object that i create in runtime via script. I show you an example code where I create a gameObject with 2 faces, how I can add 2 different material, one for the first face and otherone for second?
    If i create the same obj with Cinema4D with 2 materials it works, so I think that it's possible, but i need to have the same result creating object in realtime. Can you help me? maybe applying the solution in this example code:

    Code (csharp):
    1.  
    2.  void Start()
    3.     {
    4.         GameObject tmp = new GameObject();
    5.         tmp.AddComponent<MeshFilter>();
    6.         tmp.AddComponent<MeshRenderer>();
    7.         var newVertices = new Vector3[] { new Vector3(0, 0, 0), new Vector3(1, 0, 0), new Vector3(0, 1, 0), new Vector3(1, 0, 0), new Vector3(1, 1, 0), new Vector3(0, 1, 0) };
    8.         Mesh mesh = new Mesh();
    9.         tmp.GetComponent<MeshFilter>().mesh = mesh;
    10.         mesh.vertices = newVertices;
    11.         Vector2[] newUV = new Vector2[] { new Vector2(0, 1), new Vector2(2, 3), new Vector2(4, 5), new Vector2(6, 7), new Vector2(8, 9), new Vector2(10, 11)};
    12.         mesh.uv = newUV;
    13.         mesh.triangles = new int[]{0,1,2, 3,4,5};
    14.     }
    15.  
     
  2. pocce90

    pocce90

    Joined:
    Mar 21, 2015
    Posts:
    16
    I've found the way: submeshes! when I'll write the code I'll post it here so I'll can help anyone that needs this information.
     
    Kurt-Dekker and sylon like this.
  3. pocce90

    pocce90

    Joined:
    Mar 21, 2015
    Posts:
    16
    So, this is the solution!

    Code (csharp):
    1.  
    2. void Start()
    3.     {
    4.         GameObject tmp = new GameObject();
    5.         tmp.AddComponent<MeshFilter>();
    6.         tmp.AddComponent<MeshRenderer>();
    7.         var newVertices = new Vector3[] { new Vector3(0, 0, 0), new Vector3(1, 0, 0), new Vector3(0, 1, 0), new Vector3(1, 0, 0), new Vector3(1, 1, 0), new Vector3(0, 1, 0) };
    8.         Mesh mesh = new Mesh();
    9.         tmp.GetComponent<MeshFilter>().mesh = mesh;
    10.         mesh.vertices = newVertices;
    11.         Vector2[] newUV = new Vector2[] { new Vector2(0, 1), new Vector2(2, 3), new Vector2(4, 5), new Vector2(6, 7), new Vector2(8, 9), new Vector2(10, 11)};
    12.         mesh.uv = newUV;
    13.         mesh.triangles = new int[]{0,1,2,3,4,5};
    14.         mesh.subMeshCount = 2;
    15.         mesh.SetTriangles(new int[] { 0, 1, 2 }, 0);
    16.         mesh.SetTriangles(new int[] { 3, 4, 5 }, 1);
    17.     }
    18.  
     
    Kurt-Dekker likes this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    Welcome! You have unlocked another critical step of procedural geometry generation in Unity3D, which is a whole world of awesome fun once you start to really jump in. Sounds like you have the patience to stick with it, so from one Unity3D fan to another, happy to have you onboard. Procedural generation in Unity is sooooo much fun to play with, plus there's tons of other sites and tutorials and assets out there to help you along and give you ideas.
     
    pocce90 likes this.