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.

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.