I am curious how textures used by models are managed from a memory perspective, and have two important questions.
Question 1: If I have a texture file and I use it for 100 models (identical or similar), does Unity allocate memory for only one instance of the image file content and share that memory allocation among the models?
Question 2: (especially if the answer to question one is no to sharing) if a model only uses 10% of the content of a texture image (e.g., the UV mapping only maps to 10% of the entire image), how much memory is consumed by the model’s use of the texture? 10% or 100% of the memory necessary for the texture file? For example, only 100kb of 1Mb image or the full 1Mb?
Any help in understanding the behavior here would be most appreciated, especially from someone within Unity that can offer insight into how and why.
It depends, actually. In the simple case where you have 100 materials all using the same texture and loaded via the standard unity process, yes, it will be shared.
However, if you load from asset bundles, it’s actually quite easy to duplicate textures if they aren’t explicitly added to a bundle of their own.
The textures is either loaded or it isn’t, so it consumes the full amount.
Thank you for clear answers to my questions. I use the GameObject.Instantiate() with files in my assets folder. I was hoping to be able to load one large texture file and use it repeatedly for numerous models. I was planning to have various different models use mostly overlapping, but somewhat different portions of the same texture, and my hope was that they would share a single texture file, even the small models that use very little of the texture file… Thanks!
You can also verify behavior like this by opening the profiler and switching the memory profile to detailed and taking a snapshot. It will list everything loaded, on whatever target (device, editor, etc)…