Search Unity

How do I find assets within an editor script?

Discussion in 'Editor & General Support' started by Precache, May 24, 2019.

  1. Precache

    Precache

    Joined:
    Jul 30, 2015
    Posts:
    47
    So I've got a bunch of prefabs sitting in the assets folder. They are all monotype structures that represent dialog trees (I use monotypes so they can have a hierarchical structure for branching.)

    At run-time, I require a list of all of them, so up until now whenever I create a new one, I drag it on a public list in the manager and access it from there.

    This works fine, and the only change I want to make is to add a button to the manager that automatically fills in the list for me.

    So from an editor class on the manager try the following:

    Code (CSharp):
    1. string[] interactions = AssetDatabase.FindAssets("t:Interaction", new[] { "Assets/Prefabs/Interactions" });
    2.         foreach(string interactionName in interactions)
    3.         {
    4.             Debug.Log("Interaction Found:" + interactionName);
    5.         }
    But that comes up empty. When I switch to "t: object" it pulls in every file in that folder, and I'd like to limit it to objects of type "interaction"

    Also at some point I'm going to need to code:

    foreach(interaction someInteraction in foundInteractions)
    {
    interactionList.add(someInteraction);
    }

    but AssetDatabase.FindAssets returns an array of strings, and I need something that points to the Interactions themselves, so any help there would be apreciated.
     
  2. Precache

    Precache

    Joined:
    Jul 30, 2015
    Posts:
    47
    It's been a while, but if you're interested in how to do this here's how:
    (You need to grab a GUID for each asset in the folder, load it, and test to see if the main object contains the data type you're looking for.)


    Code (CSharp):
    1.     public void FillInFields()
    2.     {
    3.         var t = (target as LocalizerData);
    4.         t.interactionPrefabs.Clear();
    5.  
    6.         Debug.Log("Searching For Fields");
    7.         string[] interactionGUIDs = AssetDatabase.FindAssets("t:object", new[] { "Assets/Prefabs/Interactions" });
    8.  
    9.  
    10.         foreach (string guid2 in interactionGUIDs)
    11.         {
    12.             Debug.Log(AssetDatabase.GUIDToAssetPath(guid2));
    13.             GameObject someGameObject = AssetDatabase.LoadMainAssetAtPath(AssetDatabase.GUIDToAssetPath(guid2)) as GameObject;
    14.             if(someGameObject)
    15.             {
    16.                 Interaction someInteraction = someGameObject.GetComponent<Interaction>();
    17.                 if(someInteraction)
    18.                 {
    19.                     t.interactionPrefabs.Add(someInteraction);
    20.                     Debug.Log("Interaction:" + someInteraction.interactionTitle);
    21.                 }
    22.              
    23.             }
    24.         }
    25. }
     
    PepperPaige, Entagon and Shuyin76 like this.