Unity finds resource references

When we want to find the reference of a resource, we can use the tool class at the end of the article

Instructions

  1. First put our tool class in the project, it can be placed anywhere.
  2. Select the resource to be searched, right-click the selected resource (the resource type is not limited), and select Find Resource Reference in the pop-up options , as shown in Figure 1
  3. Select the console output panel and wait for the output result. As shown in Figure 2.

(Picture 1)
Please add a picture description
(Picture 2)
Please add a picture description
As can be seen from the output results, Music.prefab and Gamelaungh.prefab reference the music_background.mp3 resource


The complete code of the tool class:

using System.Collections.Generic;
using System.IO;
using System.Threading;
using UnityEditor;
using UnityEngine;
public static class AssetRefSearchTool
{
    
    
    static string[] assetGUIDs;
    static string[] assetPaths;
    static string[] allAssetPaths;
    static Thread thread;

    [MenuItem("Assets/查找资源引用", false)]
    static void FindAssetRefMenu()
    {
    
    
        if (Selection.assetGUIDs.Length == 0)
        {
    
    
            Debug.Log("请先选择任意一个组件,再击此菜单");
            return;
        }

        assetGUIDs = Selection.assetGUIDs;

        assetPaths = new string[assetGUIDs.Length];

        for (int i = 0; i < assetGUIDs.Length; i++)
        {
    
    
            assetPaths[i] = AssetDatabase.GUIDToAssetPath(assetGUIDs[i]);
        }

        allAssetPaths = AssetDatabase.GetAllAssetPaths();

        thread = new Thread(new ThreadStart(FindAssetRef));
        thread.Start();
    }

    static void FindAssetRef()
    {
    
    
        Debug.Log(string.Format("开始查找引用{0}的资源。", string.Join(",", assetPaths)));
        List<string> logInfo = new List<string>();
        string path;
        string log;
        for (int i = 0; i < allAssetPaths.Length; i++)
        {
    
    
            path = allAssetPaths[i];
            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.Log(logInfo[i]);
        }

        Debug.Log("选择对象引用数量:" + logInfo.Count);

        Debug.Log("查找完成");
    }
}

Guess you like

Origin blog.csdn.net/qq_28644183/article/details/125344273