zoukankan      html  css  js  c++  java
  • 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;
    		}
    	}
    }
    

      

  • 相关阅读:
    centos7 setfacl权限
    三层交换机做DHCP的一些疑问
    python3 re模块
    python3 的小爬虫
    初学python的一些简单程序(2)
    python3 字典
    python3 元组
    python3列表
    初学python的一些简单程序(1)
    python3的字符串操作
  • 原文地址:https://www.cnblogs.com/mrblue/p/5099491.html
Copyright © 2011-2022 走看看