Unity 5 resource management

Hello!

I’m having issues finding good information regarding proper resource management. I’m hoping to get some guidance on how to proceed.

Our situation is that we want to dynamically instantiate assets in our game. Currently it’s not known how the assets will be structured (regarding textures, meshes, audio, child objects). My issue is that it’s hard to understand exactly how the loading of assets works with Unity and I would be happy if someone could shred some light on this.

If I have a prefab in a resource folder which in turn has references to textures and meshes. How can I ensure that we don’t load unnecessary assets into memory? What I mean is that the asset we want to load might need a mesh which is already in memory, but at the same time need something that is not in memory. This is related to both asset bundles and normal Resources.Load. If there are two prefabs which share a mesh for example and we load both with Resource.Load, will the mesh be loaded into memory twice? Does it make a difference if the mesh is in a resource folder or not?
If the prefab is quite large (many child objects with many different meshes, audios, scripts), should the prefab be an asset bundle or should we simply just use Resources.Load on that asset, and when we believe it’s not being used anymore, use UnloadUnusedAssets? As a side note, we aren’t talking about downloading any assets, this is simply regarding dynamically instantiating assets which might not be used very often.

Any help on the matter would be of great help!

Assets in Unity have unique Ids(guid). When we load an asset we can check to see if that asset already exists in memory by its id, so you should not have the same asset loaded twice. You can check this by loading them in the editor and click on the slot and seeing that they both point to the same object. Most of this is done by the AssetDatabase. Making a prefab an asset bundle or using resources wont make any difference to the memory it consumes when loaded. Using UnloadUnusedAssets will help but it can cause a noticeable performance hit when executed, you can also unload individual assets using Resources.UnloadAsset and if you do accidentally unload an asset that is still used then it will just be reloaded so not too bad :wink:

Great reply! Thanks for the information, it has given me great insight to the questions I had.
Regarding Resources.UnloadAsset, it takes an object as parameter and others have posted that it doesn’t work on prefabs. Is this information false?

Well it sounds like you don’t need asset bundles as your not downloading anything so it should not be an issue however I have used UnloadAsset in the past with Textures and Meshes that were loaded from prefabs and it worked fine. You need to call UnloadAsset on the actual texture/mesh though, calling it on a GameObject that contains it will not do the job.

A quick question more :slight_smile:
If a prefab in a resource folder has references to meshes/textures outside of resource folders, do I still need to unload those manually or will Unity handle that internally when the prefab is destroyed?

I might have found the answer when I read Unity - Manual: Loading Resources at Runtime
The prefabs will have references to dependencies which will be included in the resources.asset. This makes me believe that unloading of those dependencies has to be handled manually?

Unloading will occur between scene changes. If you want it to occur during the scene then you need to do it manually. It may be worth seeing how things go, how much extra memory are these assets using, do you even need to unload them during the scene or can it wait? The profiler will let you examine what is currently stored in memory and decide for yourself if you need to be unloading assets or if they can wait till the scene change.

Thanks a lot for the info! :slight_smile: