Using Instantiate(Resources.Load(...)) requires UnloadUnusedAssets?

Hi! I was using scenes to organize my project, but found several little problems that turned this method unappealing. So I decided to instantiate prefabs from the resources folder, this would be perfect if Unity allowed to edit prefabs in place… anyways, something I don’t understand from the very brief documentation is when should I call UnloadUnusedAssets? Is it required to keep memory optimal? Thanks in advance!

Yes it’s required to keep memory optimal, use it whenever the user won’t notice the brief pause from using it. You will have had to call Destroy or DestroyImmediate on all objects you need to unload.

Thank you very much hippocoder, that answer should be appended to the scripting reference :slight_smile:

A lot of things could be added to the docs :stuck_out_tongue:

The reference is ok since it’s brief and concise, but it should be a wiki or at least allow comments like the PHP online reference, so the community can share ideas and code fragments. It’s incredible how much you can learn just by reading comments in the PHP reference.

Consider also that UnloadUnusedAssets can lead to a very long pause on mobile (Android at least), so use it with care.

Thanks, I have that in mind. So I’m still thinking where to call it. I’m working on an RPG, then I was thinking to call it when the user opens a dialog, like the inventory… but it might be annoying. What about in between scenes? The user will think it’s part of the loading process and forgive the delay.

I’m not 100% sure, but I think Unity calls UnloadUnusedAssets automatically when loading a new scene, so if you can go without it until then there’s no need to call it.

It’s good to know that. But then how do you know when it’s required? :confused:

Oh, and I’m no longer using scenes as such, but instantiating prefabs :slight_smile:

It’s kind of up to you. When you think there’s too much/heavy unused stuff in the memory, it’s time to run it. If you have levels that can live without freeing unused resources, then waiting for a level change (or call it when you instantiate a new level/scene system) is the best option :wink:

Ok, great :slight_smile:

So if the cleaning function is going to take time I better do a full system cleanup. I’m calling this function:

function CleanupSystem()
{
	Resources.UnloadUnusedAssets();
	System.GC.Collect();
}

UnloadUnusedAssets is an async operation so you could also use it as a coroutine so that you can show a now loading animation instead of a long pause.

Not to bring a thread back from the dead, but this is the exact problem I was just running in to. Like Marcoantap I was loading resources instead of changing scenes, and began running into memory crashes on the iPad because I wasn’t calling UnloadUnusedAssets (I was just deleting the objects when I was done). This seems like an extremely critical thing that should be in bold right at the top of the scripting references to Resources.Load… :slight_smile:

Joining in the resurrection as I’m working on this for a game right now too.

IndieJoe, so from what I gather without called UnloadResource it’ll stay in memory via the Resources class.
If you were single deleting as required anyway couldn’t you expand this to a resource manager, use reference counting and call “Resources.UnloadAsset”.

Just wondering if anyone else has taken this approach compared to calling UnloadUnusedAssets as a coroutine?

I suppose that would work well, ScottH.

For my situation, something like that wasn’t required, because I’m essentially loading assets when I would normally be loading a new level. But for me, simply loading the assets required using Resources.Load is so much quicker than actually loading a level, so I use that. But once the level is loaded, I don’t really create any more of those resources, so I don’t need to keep track of things and unload individually.