Answers for "unity find all scriptable objects of a type"

C#
0

unity find all scriptable objects of a type

public static T[] GetAllInstances<T>() where T : ScriptableObject
     {
         string[] guids = AssetDatabase.FindAssets("t:"+ typeof(T).Name);  //FindAssets uses tags check documentation for more info
         T[] a = new T[guids.Length];
         for(int i =0;i<guids.Length;i++)         //probably could get optimized 
         {
             string path = AssetDatabase.GUIDToAssetPath(guids[i]);
             a[i] = AssetDatabase.LoadAssetAtPath<T>(path);
         }
 
         return a;
 
     }
Posted by: Guest on June-02-2021
0

unity find all scriptable objects of a type

public class CharacterList
    {
        List<Character> characterList = new List<Character>();

        void PopulateList()
        {
            string[] assetNames = AssetDatabase.FindAssets("Your_Filter", new[] { "Assets/YourFolder" });
            characterList.Clear();
            foreach (string SOName in assetNames)
            {
                var SOpath    = AssetDatabase.GUIDToAssetPath(SOName);
                var character = AssetDatabase.LoadAssetAtPath<Character>(SOpath);
                characterList.Add(character);
            }
        }
    }
Posted by: Guest on June-02-2021

Code answers related to "unity find all scriptable objects of a type"

C# Answers by Framework

Browse Popular Code Answers by Language