Mesh with two materials made by script

At the moment I'm messing around with meshes just trying different things. The last few times I've been trying to make the mesh have two materials for different triangles but I can't figure out how it works in a script. With Blender, which I'm normally using to make meshes, it's easy: Assign a second material to some faces and Unity will import it correctly for you.

But how does it work with a mesh created by a script while the game is running?

  1. Set subMeshCount on the Mesh object to the number of materials you need
  2. Call SetTriangles on the Mesh object once for each material/submesh. Pass the appropriate material/submesh index as the second parameter
  3. Set materials on the MeshRenderer to an array of materials

(You should only need one set of uvs)

Nothing too difficult - you need to assign to both the uv and uv2 arrays in the mesh, then you need to assign an array of two materials to renderer.materials - don't try to modify renderer.materials one item at a time