zoukankan      html  css  js  c++  java
  • Unity 查找资源引用工具

    Unity 查找资源引用工具

    问题由来

    • 想查看组件被那些预设场景使用
    • 想查看图片被那些预设场景使用
    • 想查看资源被那些预设场景使用

    解决方法

    • 遍历项目中所有资源判断是否引用

    解决流程

    • 获取选中对象GUID
    • 使用Thread遍历项目中所有的资源
    • 判断资源是否包含GUID

    使用方式

    • 选中脚本
    • 右键
    • ZQ -> 工具 -> 查找资源引用

    源码

    using System.Collections.Generic;
    using System.IO;
    using System.Threading;
    using UnityEditor;
    using UnityEngine;
    
    public static class FindreAssetFerencesTool
    {
        static string[] assetGUIDs;
        static string[] assetPaths;
        static string[] allAssetPaths;
        static Thread thread;
    
        [MenuItem("ZQ/工具/查找资源引用", false)]
        [MenuItem("Assets/ZQ/工具/查找资源引用", false, 1)]
        static void FindreAssetFerencesMenu()
        {
            Debug.LogError("查找资源引用");
    
            if (Selection.assetGUIDs.Length == 0)
            {
                Debug.LogError("请先选择任意一个组件,再右键点击此菜单");
                return;
            }
    
            assetGUIDs = Selection.assetGUIDs;
    
            assetPaths = new string[assetGUIDs.Length];
    
            for (int i = 0; i < assetGUIDs.Length; i++)
            {
                assetPaths[i] = AssetDatabase.GUIDToAssetPath(assetGUIDs[0]);
            }
    
            allAssetPaths = AssetDatabase.GetAllAssetPaths();
    
            thread = new Thread(new ThreadStart(FindreAssetFerences));
            thread.Start();
        }
    
    
        static void FindreAssetFerences()
        {
            List<string> logInfo = new List<string>();
            string path;
            string log;
            for (int i = 0; i < allAssetPaths.Length; i++)
            {
                path = allAssetPaths[i];
    
                Debug.Log("正在查找文件:" + path);
    
                if (path.EndsWith(".prefab") || path.EndsWith(".unity"))
                {
                    string content = File.ReadAllText(path);
                    if (content == null)
                    {
                        continue;
                    }
    
                    for (int j = 0; j < assetGUIDs.Length; j++)
                    {
                        if (content.IndexOf(assetGUIDs[j]) > 0)
                        {
                            log = string.Format("{0} 引用了 {1}", path, assetPaths[j]);
                            logInfo.Add(log);
                        }
                    }
                }
            }
    
            for (int i = 0; i < logInfo.Count; i++)
            {
                Debug.LogError(logInfo[i]);
            }
    
            Debug.LogError("选择对象引用数量:" + logInfo.Count);
    
            Debug.LogError("查找完成");
        }
    }
    
  • 相关阅读:
    《构建之法》1.2.3章读后感
    0302思考并回答一些问题
    1231 实验四 递归下降语法分析程序设计
    1211 有穷自动机的构造与识别
    1112-评论
    C语言文法定义及C程序的推导过程
    词法分析
    0909对编译原理的理解
    团队合作2.0
    "数学口袋精灵"bug的发现及单元测试
  • 原文地址:https://www.cnblogs.com/zouqiang/p/12023787.html
Copyright © 2011-2022 走看看