- Home /
How do I assign different parts of model to different materials during runtime?
Im working on a project that requires different parts of a single procedural mesh to pull from different materials, without using an atlas. I have the code to generate the mesh, and the code to assign the UVs (except the part where they come from different materials).
For example, given a minecraft situation, every chunk has a list of materials, and each tile (cube) needs to pull from the material associated with its type. How do I switch what material the UV generation is using when I generate the chunk?
Answer by Gamingdrake · Mar 19, 2014 at 09:48 AM
I figured it out. After a few days of screwing around with the documentation and code, I figured out that you can use submeshes and assign different triangles to different materials using SetTriangles. This makes it more difficult in that I need to loop through all the different cubes in the chunk, but I can assign each cube to its material list, and then assign the triangles from those cubes through the method call. Unfortunately, now I have to deal with two different collision meshes :(
If I understand your problem correctly, you can just assign material IDs in your 3D modeling application, export, and then the material array of your gameObject will correspond to the material IDs. This way, you have it all in one place.
Unfortunately no. The mesh is procedurally created in the editor, chunk by chunk
Your answer
![](https://koobas.hobune.stream/wayback/20220613140121im_/https://answers.unity.com/themes/thub/images/avi.jpg)