Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

[HELP] EditorUtility.UnloadUnusedAssetsImmediate doesn't work

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

  1. iSinner

    iSinner

    Joined:
    Dec 5, 2013
    Posts:
    149
    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:
    149
    Well, either people don't have this issue or this use case is really rare. Either way i'm in a pickle.
     
  3. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    867
    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.
     
  4. iSinner

    iSinner

    Joined:
    Dec 5, 2013
    Posts:
    149
    I'm using editor coroutines as described before, didn't find other solutions.
     
  5. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    867
    All right, I'll give that a try as a last resort. Thanks.
     
unityunity