Determine if asset from AsssetBundle is in use

Hi everybody,

I have a custom AssetBundle management system to load external assets. Everything works peachy until I realized one thing: how do I know when I can safely unload an asset bundle with unloadAllLoadedObjects param set to true, to free up some memory?
I know I want to do this only when loading another scene. But the problem I see is that I don’t know if any of the assets inside an asset bundle are in use or referenced in any script in the current scene.

Is there a way to find out if an asset from an asset bundle is in use or referenced in a script in the current scene?

I was thinking about using FindObjectsOfType( … ) to search for all the assets of the same type as the asset I want to investigate and then go through the returned array to check this with the == operator to see if I find it. But I think you see the problem here: there could be different instances of that asset, so the comparison doesn’t help here.

But, if I unload an asset bundle with the unloadAllLoadedObjects param set to false, will Unity auto clean unused loaded assets from the bundle upon loading another scene? Or they will just remain in memory for ever and ever until death (Out Of Memory crash) do us part. :smile:

Any advice is appreciated!
Thank you so much!

Hi!
I suggest to create an array with asset names in it. When you load an asset you place it name in an array. When you destroy an asset - you set correspond cell of array to “null”. So, you always know what assets you are already loaded.
Regarding “AssetBundle-asset” relationship - you may create 2D-array, with name of AssetBundle in 1st dimension and names of corresponding assets in 2nd dimension.
I’ve done so, when I need to monitor specific assets.
Hope it helps.

Thank you for your reply.
It’s kind of hard to manually keep track of the loaded assets. Because some of them get auto-destroyed by the engine and you don’t always know when an asset is destroyed so you can set the corresponding array value to null.