Hi there, simple question right?
I animate the alpha on a gameobject and when the alpha goes back to 1 I want to revert back to the sharedMaterial to it can resume the batching with the other gameObjects.
I’ve tried simply doing myGO.renderer.material = myGO.renderer.sharedMaterial but that doesn’t seem to work. The material remains an instance of the original material.
Is there a particularity that I’m not aware?
1 Answer
1Just create a field to store the original shared material before making changes to .material.
I’ve used this pattern many times:
Material originalSharedMaterial
void ChangeMaterial()
{
originalSharedMaterial = renderer.sharedMaterial;
renderer.material.SetColor("MyCustomColor", Color.white);
}
void ResetMaterial()
{
renderer.material = originalSharedMaterial;
}
Caveat: This will still create a new instance every time you change the material, which the GC has to clean later. It is not a huge problem, but if you are going to do this many times, consider also caching the changed material after you change it for the first time, and then simply restoring the changed material on subsequent calls.
Possibly ... http://answers.unity3d.com/questions/177887/change-materialcolor-in-code-and-revert-on-quit.html
– FattieI would also like to know if this is possible. Assigning the shared material to the material seems to have no effect.
– DavidSWu