• Unity3d 枚举某个目录下所有资源


    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;
    		}
    	}
    }
    

      

  • 相关阅读:
    纯JavaScript实现HTML5 Canvas六种特效滤镜
    玩转html5 的 canvas画图
    为什么 ++[[]][+[]]+[+[]] = 10?
    作用域和闭包
    7 个令人惊讶的 JavaScript “特
    60个有用css代码片段
    函数
    数组
    我们为什么要尝试前后端分离
    javascript面向对象——构造函数和原型对象
  • 原文地址:https://www.cnblogs.com/mrblue/p/5099491.html
Copyright © 2020-2023  润新知