Unity 3D - 编辑器扩展之列出Prefab使用的资源

Unity 3D - 编辑器扩展之列出Prefab使用的资源 :

这里写图片描述

C#代码 :

using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Linq;

namespace CZGame
{
    public class CheckAllUIPrefabResource : EditorWindow
    {
        private List<string> checkType = new List<string>();
        Dictionary<string, List<string>> mapReferenceOtherModulePref = new Dictionary<string, List<string>>();
        Vector2 scrollPos = Vector2.zero;
        private void StartCheck()
        {
            List<string> withoutExtensions = new List<string>() { ".prefab" };
            string[] files = Directory.GetFiles(Application.dataPath + "/GameRes/UIPrefab", "*.*", SearchOption.AllDirectories)
                .Where(s => withoutExtensions.Contains(Path.GetExtension(s).ToLower())).ToArray();

            mapReferenceOtherModulePref.Clear();
            float count = 0;
            foreach (string file in files)
            {
                count++;
                EditorUtility.DisplayProgressBar("Processing...", "检索中....", count / files.Length);

                string strFile = file.Substring(file.IndexOf("Asset")).Replace('\\', '/');
                string temp = strFile.Substring(strFile.IndexOf("/", strFile.IndexOf("UIPrefab")) + 1);
                temp = "GameRes/UI/" + temp.Substring(0, temp.IndexOf("/"));
                string[] dependenciesFiles = AssetDatabase.GetDependencies(strFile, false);

                List<string> OtherFiles = null;
                string txt = null;
                foreach (string depFile in dependenciesFiles)
                {
                    bool isNeedShow = false;
                    foreach (string type in checkType)
                    {
                        //存在设置类型  需要显示
                        if (depFile.IndexOf(type) > -1)
                        {
                            isNeedShow = true;
                            break;  
                        }
                    }

                    if (isNeedShow == false)
                    {
                        continue;
                    }

                    if (OtherFiles == null)
                    {
                        if (!mapReferenceOtherModulePref.TryGetValue(strFile, out OtherFiles))
                        {
                            OtherFiles = new List<string>();
                            mapReferenceOtherModulePref.Add(strFile, OtherFiles);
                        }
                    }
                    if (txt == null)
                    {
                        string fullPath = Path.GetFullPath(file);
                        txt = File.ReadAllText(fullPath);
                    }

                    string ugui = AssetDatabase.AssetPathToGUID(depFile);
                    //8是 "m_Name: "的长度.

                    int fileIDIndex = 8 + txt.IndexOf("fileID: ", txt.LastIndexOf("m_GameObject:", txt.IndexOf(ugui)));
                    int fileIDendIndex = txt.IndexOf("}", fileIDIndex);
                    string fileID = txt.Substring(fileIDIndex, fileIDendIndex - fileIDIndex);
                    int nameIndex = 8 + txt.IndexOf("m_Name: ", txt.IndexOf("&" + fileID));
                    int endNameIndex = txt.IndexOf("\n", nameIndex);
                    string gameObjectName = txt.Substring(nameIndex, endNameIndex - nameIndex);
                    string totalStr = gameObjectName + "#" + depFile;

                    string[] totalStrArr = totalStr.Split('/');

                    int maxCount = 0;
                    int index = -1;
                    int targetIndex = 0;
                    foreach (string nowFile in OtherFiles)
                    {
                        index++;
                        string[] splitArr = nowFile.Split('/');
                        int sameCount = 0;
                        for (var i = 0; i < splitArr.Length; i++)
                        {
                            if (i == 0) continue;
                            for (int j = 0; j < totalStrArr.Length; j++)
                            {
                                if (j == 0) continue;
                                if (splitArr[i] == totalStrArr[j])
                                {
                                    sameCount++;
                                }
                            }
                        }

                        if (sameCount > maxCount)
                        {
                            targetIndex = index;
                            maxCount = sameCount;
                        }
                    }
                    OtherFiles.Insert(targetIndex, totalStr);
                }
            }

            EditorUtility.ClearProgressBar();
        }

        void OnGUI()
        {
            EditorGUI.BeginChangeCheck();
            if (GUILayout.Button("[Png 和 JPG]", GUILayout.Width(100)))
            {
                checkType.Clear();
                checkType.Add(".jpg");
                checkType.Add(".png");
                StartCheck();
            }

            if (GUILayout.Button("[字体]", GUILayout.Width(100)))
            {
                checkType.Clear();
                checkType.Add(".fontsettings");
                checkType.Add(".ttf");
                StartCheck();
            }

            if (GUILayout.Button("[C#脚本文件]", GUILayout.Width(100)))
            {
                checkType.Clear();
                checkType.Add(".cs");
                StartCheck();
            }

            EditorGUI.EndChangeCheck();

            scrollPos = EditorGUILayout.BeginScrollView(scrollPos);

            int count = 0;
            foreach (var kv in mapReferenceOtherModulePref)
            {
                count++;
                string prefabName = kv.Key;
                EditorGUILayout.LabelField("" + count + ": " + prefabName);
                foreach (string strIllegalRes in kv.Value)
                {
                    int spliterIndex = strIllegalRes.IndexOf("#");
                    string gameObjectName = strIllegalRes.Substring(0, spliterIndex);
                    string resPath = strIllegalRes.Substring(spliterIndex + 1);

                    var obj = AssetDatabase.GetMainAssetTypeAtPath(resPath);

                    EditorGUILayout.BeginHorizontal();
                    EditorGUILayout.LabelField(gameObjectName, GUILayout.Width(200));
                    EditorGUILayout.TextField(resPath);

                    //打开资源索引
                    /*
                    if (resPath.IndexOf(".png") > -1 || resPath.IndexOf(".png") > -1)
                    {
                        Texture2D t = (Texture2D)AssetDatabase.LoadAssetAtPath(resPath, typeof(Texture2D));
                        EditorGUILayout.ObjectField(t, typeof(Texture2D), false);
                    }
                    else if(resPath.IndexOf(".fontsettings") > -1 || resPath.IndexOf(".ttf") > -1)
                    {
                        Font t = (Font)AssetDatabase.LoadAssetAtPath(resPath, typeof(Font));
                        EditorGUILayout.ObjectField(t, typeof(Font), false);
                    }
                    */
                    EditorGUILayout.EndHorizontal();
                }
                EditorGUILayout.Space();
                EditorGUILayout.Space();
            }
            EditorGUILayout.EndScrollView();



        }
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值