0
点赞
收藏
分享

微信扫一扫

Unity中如何查找一个资源的引用

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

public class FindAssetReference : EditorWindow
{
    [MenuItem("Assets/FindReference")]
    static void FindReference()
    {
        List<string> dependencyList = new List<string>();
        string selectAssetPath = AssetDatabase.GetAssetPath(Selection.activeObject);
        if (string.IsNullOrEmpty(selectAssetPath))
            return;
        string[] allAssetPaths = AssetDatabase.GetAllAssetPaths();
        for (int i = 0; i < allAssetPaths.Length; ++i)
        {
            float per = i / allAssetPaths.Length;
            EditorUtility.DisplayProgressBar(string.Format("Searching. {0:P1}", per), "", per);
            string assetPath = allAssetPaths[i];
            if (string.IsNullOrEmpty(assetPath))
                continue;
            var mainAsset = AssetDatabase.LoadMainAssetAtPath(assetPath);
            if (mainAsset == null)
                continue;
            UnityEngine.Object[] dependencies = EditorUtility.CollectDependencies(new UnityEngine.Object[] { mainAsset });
            for (int j = 0; j < dependencies.Length; j++)
            {
                var dependency = dependencies[j];
                if (string.IsNullOrEmpty(dependency.name))
                    continue;
                string dependencyPath = AssetDatabase.GetAssetPath(dependency);
                if (string.IsNullOrEmpty(dependencyPath))
                    continue;
                if (string.Equals(dependencyPath.ToLower(), selectAssetPath.ToLower()))
                {
                    dependencyList.Add(assetPath);
                    break;
                }
            }
        }
        EditorUtility.ClearProgressBar();

        int count = 0;
        foreach (var dependencyPath in dependencyList)
        {
            count += 1;
            Debug.Log($"【{count}】 {dependencyPath}");
        }

    }
}

比如我们查找DrawLine这个脚本在游戏中那个地方有用过,就可以选择DrawLine脚本右键选择FindReference去查找它的引用。

查询后的效果图如下:

Unity中如何查找一个资源的引用_Unity

举报

相关推荐

0 条评论