A game about forced loneliness, made by TACStudios
at master 54 lines 2.3 kB view raw
1using System.Collections.Generic; 2using UnityEngine; 3 4namespace UnityEditor.Rendering 5{ 6 /// <summary> Set of helpers for AssetDatabase operations. </summary> 7 public static class AssetDatabaseHelper 8 { 9 /// <summary> 10 /// Finds all assets of type T in the project. 11 /// </summary> 12 /// <param name="extension">Asset type extension i.e ".mat" for materials. Specifying extension make this faster.</param> 13 /// <typeparam name="T">The type of asset you are looking for</typeparam> 14 /// <returns>An IEnumerable off all assets found.</returns> 15 public static IEnumerable<T> FindAssets<T>(string extension = null) 16 where T : Object 17 { 18 string query = BuildQueryToFindAssets<T>(extension); 19 foreach (var guid in AssetDatabase.FindAssets(query)) 20 { 21 var asset = AssetDatabase.LoadMainAssetAtPath(AssetDatabase.GUIDToAssetPath(guid)); 22 if (asset is T castAsset) 23 yield return castAsset; 24 } 25 } 26 27 /// <summary> 28 /// Finds all assets paths of type T in the project. 29 /// </summary> 30 /// <param name="extension">Asset type extension i.e ".mat" for materials. Specifying extension make this faster.</param> 31 /// <typeparam name="T">The type of asset you are looking for</typeparam> 32 /// <returns>An IEnumerable off all assets paths found.</returns> 33 public static IEnumerable<string> FindAssetPaths<T>(string extension = null) 34 where T : Object 35 { 36 string query = BuildQueryToFindAssets<T>(extension); 37 foreach (var guid in AssetDatabase.FindAssets(query)) 38 yield return AssetDatabase.GUIDToAssetPath(guid); 39 } 40 41 static string BuildQueryToFindAssets<T>(string extension = null) 42 where T : Object 43 { 44 string typeName = typeof(T).ToString(); 45 int i = typeName.LastIndexOf('.'); 46 if (i != -1) 47 { 48 typeName = typeName.Substring(i+1, typeName.Length - i-1); 49 } 50 51 return string.IsNullOrEmpty(extension) ? $"t:{typeName}" : $"t:{typeName} glob:\"**/*{extension}\""; 52 } 53 } 54}