Reputation: 771
I need to change and therefore instantiate only one material in the materials array. Currently, if I access MeshRenderer.materials
, unity returns an array of instantiated materials.
This function automatically instantiates the materials and makes them unique to this renderer. (Documentation)
Any ideas on how to achieve that?
More info on what I'm trying to do:
I have an object with 10 material slots that is used frequently in the scene.
The first 5 slots use Material-A which is using GPU Instancing with colour as the instancing property, And the second 5 slots use Material-B which is not using GPU Instancing and its texture is being assigned at runtime.
Therefore when a texture is assigned to the second 5 slots at runtime, It then defeats the purpose of GPU Instancing the first 5 slots.
Is there a way other than GPU Instance Material-B using Texture2DArray?
Upvotes: 0
Views: 2226
Reputation: 771
Solved it by replacing materials containing "Material-A"
in their name with Resources.Load("Material-A") as Material
to reference the original Material-A whenever I change Material-B's texture.
var mats = meshRenderer.materials;
mats[materialIndex].mainTexture = texture;
for (var i = 0; i < mats.Length; i++)
if (mats[i].name == "Material-A (Instance)")
mats[i] = Resources.Load("Material-A") as Material;
meshRenderer.materials = mats;
I would appreciate it if you have a better way...
Upvotes: 0