Search Unity

[HELP] EditorUtility.UnloadUnusedAssetsImmediate doesn't work

Discussion in 'Scripting' started by iSinner, Oct 18, 2019.

  1. iSinner

    iSinner

    Joined:
    Dec 5, 2013
    Posts:
    137
    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
    Code (CSharp):
    1. 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.


    Code (CSharp):
    1. for(var i = 0; i < modelPaths.Count; i++)
    2.         {
    3.             var path = modelPaths[i];
    4.             var x = AssetDatabase.LoadAssetAtPath<GameObject>(path);
    5.             x = null;
    6.             EditorUtility.UnloadUnusedAssetsImmediate();
    7.         }
    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.
     
    ROBYER1 likes this.
  2. iSinner

    iSinner

    Joined:
    Dec 5, 2013
    Posts:
    137
    Well, either people don't have this issue or this use case is really rare. Either way i'm in a pickle.