- Home /
Adding extra materials to a mesh programmatically
I'd like to add extra procedural geometry / materials to an authored mesh at import time. I have a custom AssetPostprocessor
script that creates a new, larger, Material[]
array with the additional materials appended, assigns it to renderer.sharedMaterials
, sets the appropriate value to renderer.sharedMesh.subMeshCount
, and performs the renderer.sharedMesh.SetTriangles()
calls.
However, Unity appears to promptly discard the new materials, resetting the content of renderer.sharedMaterials
to just the materials present in the fbx, by the time Start() is called on monobehaviours attached to the parent gameobject - although the submesh count remains what I set it to, and the extra geometry is still present.
Why does this happen? What is the correct way of adding extra materials to a mesh?
Everything sounds about right... Do you create new materials or do you reuse them from somewhere? Can you post your code?