- Home /
UnloadAsset with GameObject best practice?
It appears that a GameObject loaded by Resources.Load() cannot be unloaded by Resources.UnloadAsset() - an error with the following message is thrown:
"UnloadAsset may only be used on individual assets and can not be used on GameObject's / Components or AssetBundles"
This has been documented multiple times in prior questions and forum threads:
http://answers.unity3d.com/questions/16020/how-can-i-unload-a-single-resource.html
http://answers.unity3d.com/questions/260144/resourcesunloadasset.html
http://forum.unity3d.com/threads/155553-Combinee-meshes-still-not-released-from-memory
I believe the issue is a GameObject is a container class and UnloadAsset() handles only individual assets i.e. Texture.
Has anybody got a solution or best practice for cleaning up the components/memory of a GameObject loaded by Resources.LoadAsset() apart from the obvious and complete overkill call to Resources.UnloadUnusedAssets() which walks the entire object tree every call?
Have you tried unloading all of it's components and child components then destroying the empty gameobject? (haven't run into this issue before myself since I've never used Resources.Load for a GameObject)
@FWCorey: Good suggestion. I had pondered that, but not tried for the following reasons:
You can't unload components, only assets, so would have to derive the component type, then write code to leech out the asset to unload. Given I am loading GameObjects that could contain InteractiveCloth, LODGroups, $$anonymous$$eshes etc. this would be a monster task.
Destroying the root GameObject actually destroys the asset/prefab full stop, poof and it's gone from your project :(
As I'm a Pro user I'm pondering AssetBundles but that seems like overkill and a pain for iteration. I was asking this with everything crossed for a nice, elegant silver bullet answer :D
What about putting the asset in an empty scene ins$$anonymous$$d of a resource, loading it additively then destroying the instance? I don't have pro and that's how I stream assets.
@FWCorey: Yup another good suggestion. Problem there is there are so many of them and they are relatively small - over 30 at the moment - and this number will extend over time; this would give me a maintenance nightmare with flooding the project with excess scenes and having to manage them through the Player etc.