Skip to content

Instantly share code, notes, and snippets.

@rittikornt
Created March 3, 2022 07:28
Show Gist options
  • Save rittikornt/caf4624e0dbf21e19a4a23f7092947b8 to your computer and use it in GitHub Desktop.
Save rittikornt/caf4624e0dbf21e19a4a23f7092947b8 to your computer and use it in GitHub Desktop.
Find all ScriptableObject in the Unity Project
public static T[] LoadAllAssetAtPath<T>(string fullFolderPath, string prefix, string postfix, List<T> list = null) where T : UnityEngine.Object
{
string searchPattern = prefix + "*" + postfix;
string[] filePaths = Directory.GetFiles(fullFolderPath
, searchPattern, SearchOption.AllDirectories);
//System.Array.Sort(filePaths);
Debug.Log($"Found {filePaths.Length} paths to check for search pattern: \"{searchPattern}\"");
if (list == null)
{
list = new List<T>();
}
foreach (string path in filePaths)
{
string assetPath = "Assets" + path.Replace(Application.dataPath, "").Replace('\\', '/');
var asset = (T)AssetDatabase.LoadAssetAtPath(assetPath, typeof(T));
if (asset != null)
{
list.Add(asset);
}
//Debug.Log(assetPath);
}
return list.ToArray();
}
// Example Usage: find
// string findPath = Application.dataPath;
// CustomScriptableObjectType[] objs = LoadAllAssetAtPath<CustomScriptableObjectType>(findPath, "", ".asset", null);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment