[HELP] EditorUtility.UnloadUnusedAssetsImmediate doesn't work

TL;DR i can’t unload assets via this method in editor code, it doesn’t unload them. And there is no method that allows to unload the loaded assets. Help.

I have a lot of models in the project. At edit time(editor code), i iterate over all of them, load two in memory, try to analyze them in some way, then unload, and go to the next one.

I am loading them via

AssetDatabase.LoadAssetAtPath<GameObject>(path)

and i try to unload them via EditorUtility.UnloadUnusedAssetsImmediate.

The issue i have, is that UnloadUnusedAssetsImmediate doesn’t unload the models from memory, so after a while i run out of ram(i have 60+gb of models total).

I wrote a piece of code as simple as possible to see if the issue lies within the method EditorUtility.UnloadUnusedAssetsImmediate, and seemingly it does.

for(var i = 0; i < modelPaths.Count; i++)
        {
            var path = modelPaths[i];
            var x = AssetDatabase.LoadAssetAtPath<GameObject>(path);
            x = null;
            EditorUtility.UnloadUnusedAssetsImmediate();
        }

This piece of code, which runs in OnGUI in a class derived from EditorWindow, will eat all my ram and crash the editor with “out of memory” exception, it doesn’t unload the assets.

After trying to use GC.Collect, Resources.UnloadUnusedAssets it didn’t help, i tried editor coroutines, and if i skip a frame after loading an asset, i don’t even need to call UnloadUnusedAssetsImmediate, they will be unloaded if i yield return null after loading the asset.

My question is, should this method work in my case at all, or is it intended to be used for something else?
Is there an alternative to this method? because i didn’t find any.

I need a solution to unload the loaded asset from memory with a method call, because doing it with editor coroutines is just a nightmare.

3 Likes

Well, either people don’t have this issue or this use case is really rare. Either way i’m in a pickle.

Have you figured this out? I have an editor tool that is generating prefabs, and this method should be releasing the memory (the game objects which are used to generate the prefabs are destroyed immediately using DestroyImmediate before calling this method).

I also tried calling GC.Collect and it doesn’t do anything, the ManagedHeap.UsedSize just grows and grows.

I’m using editor coroutines as described before, didn’t find other solutions.

All right, I’ll give that a try as a last resort. Thanks.

bump, i want to know how to unload asset in editor too

Then please create your own thread rather than necroing old ones.

Thanks.