I use SetPropertyBlock to paint color for my model. My model has 5 level of details (LOD0 to LOD4).
The problem is LODs in Unity doesn’t share the same materials array ([sharedMaterials][1]), thus I paint color for LOD0 doesn’t affect LOD1,…, LOD4. So I tried painting for other LOD too:
Transform[] arrLods;//The array of LODs
//Assign arrLods
//...
MaterialPropertyBlock propBlock = new MaterialPropertyBlock();
Renderer renderer = arrLods[0].GetComponent<Renderer>();
//Paint for LOD0
//...
//Paint for other LODs
Material[] matLODs;
Renderer rendererLODs;
for (int i = 1; i < arrLods.Length; ++i)
{
rendererLODs = arrLods*.GetComponent<Renderer>();*
matLODs = rendererLODs.sharedMaterials;
for (int m = 0; m < mat.Length; ++m)
{
for(int mLODs = 0; mLODs < matLODs.Length; ++mLODs)
{
if (mat[m].name == matLODs[mLODs].name)
{
rendererLODs.GetPropertyBlock(propBlock, mLODs);
if(propBlock.isEmpty)
{
renderer.GetPropertyBlock(propBlock, m);
rendererLODs.SetPropertyBlock(propBlock, mLODs);
break;
}
}
}
}
}
Because the order of other LODs doesn’t match the order of LOD0, the code “Paint for other LODs” is a little complex, but I still have bug in case the materials have same name (Brick1, Brick2,… use same material Brick, but difference color)
so I wonder if there is a better way to properly SetPropertyBlock for LOD?
[1]: Unity - Scripting API: Renderer.sharedMaterials