When the main game is loaded (world.zen; without NPCs), we have over 1.2k Materials initialized. When looked at our mesh creation logic, we always create new Material(...) when creating a mesh or calling Renderer.mesh.Set*() when altering data which creates a clone of the Material.
TODOs:
[ ] Evaluate if the memory optimization is needed (e.g. on Mobile VR via Profiler)
[ ] Check on *MeshBuilder.cs classes where we leverage new Material() and alter instanciation of materials with same Texture to only once --> Especially useful for TextureArray (World+Items), where we have the same textures for every material and could leverage the same Material.
[ ] Dispose these materials (TextureManager, ...) after game stop, as Unity isn't handling it on it's own. https://docs.unity3d.com/ScriptReference/Renderer-material.html Quote: It is your responsibility to destroy the materials when the game object is being destroyed.
[ ] Add our usage of material/sharedMaterial as proper code comments or into the Wiki for future references.
Hints:
Whenever we call Renderer.material. (calling the getter), it will automatically duplicate the material under the hood. Therefore it should be avoided at all costs and Renderer.sharedMaterial. should be used. https://docs.unity3d.com/ScriptReference/Renderer-material.html Quote: Returns the first instantiated Material assigned to the renderer. Modifying material will change the material for this object only. If the material is used by any other renderers, this will clone the shared material and start using it from now on.
When the main game is loaded (world.zen; without NPCs), we have over 1.2k Materials initialized. When looked at our mesh creation logic, we always create
new Material(...)
when creating a mesh or calling Renderer.mesh.Set*() when altering data which creates a clone of the Material.TODOs:
Hints: