- Home /
Resources.UnloadAsset - How To Use on Prefab
Hello,
I'm trying to figure out the best practice for unloading a Prefab that has been dynamically loaded in via Resource.Load.
As it's an entire GameObject, I can't just call:
Resources.UnloadAsset(myPrefab);
As that just won't work as it needs to be specifically an Asset of sorts. Also, I don't want to use 'UnloadUnusedAssets' as this actually triggers a bug in my app (it removes textures that are in actual fact being used - thus leaving a black texture - iOS).
Would I cycle through all child objects, and pick out any assets, and call 'UnloadAsset' on that? Also, I know that I need to destroy the gameObject first, but if I do how do I then call 'UnloadAsset' on it? As the reference won't exist anymore, right?
Whats the best way to achieve this? Thanks.
Answer by hulahoolgames · Jan 11, 2016 at 05:44 PM
Try something like this:
// load prefab from resources into memory
GameObject prefab = Resources.Load<GameObject>(path);
// instantiate clones of this prefab
GameObject instance = GameObject.Instantiate(prefab);
Destroy(instance); // deletes the instance
Resources.UnloadAsset(prefab);
I have not really tested this logic, but from documentation this is how I think it works.
You're not calling any form of Unload - which means the assets are still within $$anonymous$$emory. The purpose of this is so I can keep my memory as low as possible.
I see what you mean! I edited my answer, why do you think calling Resources.UnloadAsset(prefab) wont work? GameObject derives from Object and since this was loaded from memory shouldn't this unload it?
"UnloadAsset may only be used on individual assets and can not be used on GameObject's / Components or AssetBundles". As my Prefab is a GameObject, it won't work.
Do not answer if you are not sure. Care about other people's time
Your answer
Follow this Question
Related Questions
Destroy single asset from AssetBundle 2 Answers
Memory problem. How do I dump a Streaming Asset from memory? 1 Answer
Memory Management: Assets between scenes 1 Answer
getting raycast working on prefabs made from resources created at runtime 1 Answer
When are resources outside the resource folder in memory? 2 Answers