//枚举所有GameObject在这个目录 //path是相对于Application.dataPath的 例如 Assets/Res/UI/ public static IEnumerable<GameObject> EnumGameObjectAtPath(string path) { var guids = AssetDatabase.FindAssets("t:GameObject", new string[] { path }); foreach (var guid in guids) { var p = AssetDatabase.GUIDToAssetPath(guid); var go = AssetDatabase.LoadAssetAtPath(p, typeof(GameObject)) as GameObject; if (null != go) { yield return go; } } } //枚举所有资源 //path是相对于Application.dataPath的 例如 Assets/Res/UI/ public static IEnumerable<T> EnumAssetAtPath<T>(string path) where T : Object { var guids = AssetDatabase.FindAssets("t:Object", new string[] { path }); foreach (var guid in guids) { var p = AssetDatabase.GUIDToAssetPath(guid); var go = AssetDatabase.LoadAssetAtPath(p, typeof(System.Object)) as T; if (null != go) { yield return go; } } } //递归枚举这个目录下的GameObject的所有T类型组件 //path是相对于Application.dataPath的 例如 Assets/Res/UI/ public static IEnumerable<T> EnumComponentRecursiveAtPath<T>(string path) where T : UnityEngine.Component { var gos= EnumGameObjectAtPath(path); foreach (var go in gos) { var cs = go.GetComponentsInChildren<T>(true); foreach(var c in cs) { yield return c; } } }
时间: 2024-10-20 11:25:16