码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2016-01-04 17:03:13      阅读:403      评论:0      收藏:0      [点我收藏+]

标签:

    //枚举所有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;
            }
        }
    }

 

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

标签:

原文地址:http://www.cnblogs.com/mrblue/p/5099491.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!