- Home /
Memory Managmenet
Hey guys,
I was wondering if objects are loaded into memory when they are referenced by scripts?
For example, imagine we have a single game object with a mesh renderer. On this object we have a script which allows the player to change the material to one of 10 materials. Are all 10 materials loaded into memory on level start, or are they loaded as required?
Thanks in advance! -Jack
Good question. I would guess that they are loaded, but would also like to know the answer.
Anyone got any idea on this? I think they are actually loaded into memory from what I can see...
Answer by Statement · May 23, 2011 at 01:41 PM
I don't have any reference to support this, but I'd say that they are loaded if you have a reference to it (for example if you bind it through the inspector). What you can do is place them in the Resources folder and load them dynamically, if you need to avoid having them loaded when they aren't used.
Use Resources.Load to load any sort of asset, including materials.
See also Resources.UnloadUnusedAssets and Object.Destroy.
I also couldn't find any documentation, but I'm quite sure you're correct.
Suspect this is how the load logic works as well "An asset is deemed to be unused if it isn't reached after walking the whole game object hierarchy, including script components. Static variables are also exa$$anonymous$$ed." From UnloadUnusedAssests
I did a quick test and using the Unity Profiler for an app running on an iPhone and this answer answer did prove to be true. A simple reference in an $$anonymous$$onoBehavior is enough to get the texture loaded into memory.