WhiteEngine-专注图形渲染与游戏引擎技术的网站

新浪微薄腾讯微薄

最新碎语:暂无碎语

您的位置:WhiteEngine-专注图形渲染与游戏引擎技术的网站 >引擎相关> 获取prefab引用的资源信息

获取prefab引用的资源信息

using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class PrefabTools : EditorWindow

{    

    /// </summary>

    public class AudioClipInfo
    {
        public string prefab = "";
        public string audio = "";
    }
    /// 选中预制件(可多选)后按 Alt + G 键获取 <summary>
    [MenuItem("Arthun Tools/Get Select Prefabs Info &G")]
    static void GetSelectPrefab()
    {
        GameObject[] gos = Selection.gameObjects;
 
        Debug.Log("getCount:" + gos.Length.ToString());
 
        if (gos.Length == 0)
            return;
 
        List<AudioClipInfo> audioClipsInfo = new List<AudioClipInfo>();
        foreach (GameObject go in gos)
        {
            #region 依赖关系获取
            List<AudioClip> audioClips = GetPrefabDepe<AudioClip>(go);
 
            foreach (AudioClip ac in audioClips)
            {
                if (ac != null)
                {
                    AudioClipInfo info = new AudioClipInfo();
                    info.prefab = go.name;
                    info.audio = ac.name;
                    audioClipsInfo.Add(info);
                }
            }
            #endregion
        }
 
        foreach (AudioClipInfo info in audioClipsInfo)
        {
            Debug.Log(string.Format("prefab:{0}    audio:{1}", info.prefab, info.audio));
        }
 
        Debug.Log("soundCount:" + audioClipsInfo.Count.ToString());
    }
 
    /// 获取预制件依赖 <summary>
    /// <typeparam name="T">获取的类型</typeparam>
    static List<T> GetPrefabDepe<T>(GameObject go)
    {
        List<T> results = new List<T>();
        Object[] roots = new Object[] { go };
        Object[] dependObjs = EditorUtility.CollectDependencies(roots);
        foreach (Object dependObj in dependObjs)
        {
            if (dependObj != null && dependObj.GetType() == typeof(T))
            {
                results.Add((T)System.Convert.ChangeType(dependObj, typeof(T)));
            }
        }
        return results;
    }
}

---

转载请注明本文标题和链接:《获取prefab引用的资源信息

分享到:

发表评论

路人甲 表情
看不清楚?点图切换 Ctrl+Enter快速提交