Search Unity

Best practice for loading assets, t:Object and t:UnityEngine.Object

Discussion in 'Editor & General Support' started by TheHeftyCoder, Apr 17, 2020.

  1. TheHeftyCoder

    TheHeftyCoder

    Joined:
    Oct 29, 2016
    Posts:
    91
    Unity version 2019.3.0f6

    Hello, I am currently making a system that is somewhat vulnerable to change. As such, whenever big changes occur in the system, I have to search the asset database for every asset-object with a string field decorated by an attribute. Posting some of my questions-findings here:

    1) I noticed that searching by t:UnityEngine.Object is essentially the same as searching for t:ScriptableObject. Nothing too big here, but is every asset that can act as a class a ScriptableObject? (i.e I guess scene - script assets etc aren't SOs and therefore can't be loaded as a class-type object).

    2) After doing some testing and loading these assets multiple times, Unity randomly (?) creates a cache. Searching for all SOs in my project takes 5 seconds for the first load and 3.5 seconds for each subsequent load. At some point though, it decides to cache all those SOs and the loading takes 100-200 msecs. That happens randomly, I haven't been able to find a condition on why this happens. It also PERSISTS after reloading the scripts. (I didn't keep a reference to the objects anywhere)

    Leaving that aside, the good thing is that you can keep a reference to the objects so they aren't unloaded. I guess the best one could do is to load all wanted SOs at an InitializeOnLoad (or better yet via a custom editor window) and then use OnWillCreateAsset and OnWillDeleteAsset to add / remove. I don't know if this approach will also work for nested assets. If anyone knows a better way, please do tell.

    Lastly, if anyone has used the same / a similar approach, I'd like to hear how it worked. Even if I have a lot of the assets that fulfill the condition and store them in a list, it shouldn't take that much RAM.