Search Unity

Assigning multiple materials to one Meshobject in script.

Discussion in 'Scripting' started by miniduck, Sep 30, 2012.

  1. miniduck

    miniduck

    Joined:
    Sep 27, 2012
    Posts:
    117
    Sorry for this newbie question - I have search and found a few threads but not answering this particular question.

    In script I have created one Mesh() with a few polys, in unity I have a couple of materials I made.
    Now I want to assign these materials to each polygon in script. Actually two tris per material.
    So what is the way of assigning which polys goes to which material in script?

    (would prefer not splitting up into one meshobject per material :) )

    Thanks people!
     
  2. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,659
    What you need is to configure your mesh to have two 'submeshes' - one per material - by setting mesh.submeshCount and then using the SetTriangles() function (instead of just mesh.triangles = ...) to set the triangle indices for each individual submesh.
     
  3. miniduck

    miniduck

    Joined:
    Sep 27, 2012
    Posts:
    117
    Ah perfect! thanks pig
    so submeshes would get the same indexorder as material a presume?
     
  4. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,659
    Yep. Just set renderer.materials / renderer.sharedMaterials with the array of materials you want to use.