using UnityEngine; using System.Collections; using UnityEditor; using System.Collections.Generic; using System.ComponentModel; using Object = UnityEngine.Object; //在选中的资源中查找 public static class EnumAssets { //枚举所有的T类型的资源 public static IEnumerable<T> EnumInCurrentSelection<T>() where T : Object { Object[] selectionAsset = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets); foreach (var s in selectionAsset) { var temp = s as T; if (null != temp) { yield return temp; } } } //枚举所有的GameObject类型的资源 public static IEnumerable<GameObject> EnumGameObjectInCurrentSelection() { foreach (var s in EnumInCurrentSelection<GameObject>()) { yield return s; } yield break; } //递归枚举所有GameObject public static IEnumerable<GameObject> EnumGameObjectRecursiveInCurrentSelection() { foreach (var s in EnumInCurrentSelection<GameObject>()) { foreach(var g in EnumGameObjectRecursive(s)) { yield return g; } } } public static IEnumerable<GameObject> EnumGameObjectRecursive(GameObject go) { yield return go; for(int i=0; i<go.transform.childCount; i++) { foreach (var t in EnumGameObjectRecursive(go.transform.GetChild(i).gameObject)) { yield return t; } } } //递归枚举所有Compoent public static IEnumerable<T> EnumComponentRecursiveInCurrentSelection<T>() where T : UnityEngine.Component { foreach (var go in EnumInCurrentSelection<GameObject>()) { var cs = go.GetComponentsInChildren<T>(true); foreach (var c in cs) { yield return c; } } } //枚举所有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; } } } //递归枚举这个目录下的GameObject //path是相对于Application.dataPath的 例如 Assets/Res/UI/ public static IEnumerable<GameObject> EnumGameOjectRecursiveAtPath(string path) { var gos = EnumComponentRecursiveAtPath<Transform>(path); foreach (var go in gos) { yield return go.gameObject; } } }