Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Where do we go when we AssetDatabase.LoadAssetAtPath

Discussion in 'Scripting' started by robrab2000-aa, Jul 21, 2022.

  1. robrab2000-aa

    robrab2000-aa

    Joined:
    Feb 8, 2019
    Posts:
    108
    I have a class (not a monobehaviour) which needs to access a bunch (1000+) assets, fetch a component off each one read some data from them which gets written to a json file.

    My current system does this but its generating a lot of garbage.. Some of the assets are over 1gb (volumetric point cloud captures) and so once the process finishes Unity is holding 20gb+ of memory which it gives back after I restart the editor.

    I'm tracking down spots where I can optimise the process and I was thinking that possibly my use of AssetDatabase.LoadAssetAtPath could be causing issues:

    Code (CSharp):
    1.  
    2. foreach (var prefabPath in prefabPaths)
    3. {
    4.     if (prefabPath.Contains(".meta")) continue;
    5.     var go = AssetDatabase.LoadAssetAtPath(prefabPath, typeof(GameObject)) as GameObject;
    6.  
    7.     var contentData = go.GetComponent<IContentData>();
    8.     if (contentData == null)
    9.     {
    10.         EditorUtility.DisplayDialog("Warning",
    11.             $"Couldn't find a valid ContentID script on {go.name}. Skipping it.", "Ok");
    12.         continue;
    13.     }
    14.    
    15.     AA_Tools.AssignPrefabToAddressableGroup(prefabPath, addressableGroup, false);
    16.  
    17.     m_contentCatalogListings.AddContentToCatalogListing(contentData);
    18. }
    19.  
    When I use this call, I'm loading the prefab into memory (I'm assuming here) but then perhaps it only releases that memory after the for loop is completed? or maybe not until the editor is restarted?

    Is there a way that I can manually release the object (I tried
    GameObject.Destroy(go);
    but this did not work, all I got was 1000+ errors at the end saying I couldn't do that).
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
  3. robrab2000-aa

    robrab2000-aa

    Joined:
    Feb 8, 2019
    Posts:
    108
    Ah thank you!

    Should I be running this after each asset is unneeded? or should I batch it to run after maybe every 10 assets have been processed. The documentation mentions "after walking the whole game object hierarchy", this sounds like a time consuming process...