Unity 查找资源引用
有时会有这样的需求,在项目中查找某个资源被其他哪些资源引用了。
现在Unity中只有查找当前场景的引用,没有查找项目中所有的引用的功能。
所以我们自己可以利用一些API:
通过AssetDatabase.GetAllAssetPaths()获取项目中所有的资源路径。
通过AssetDatabase.GetDependencies()获取每一个资源所依赖资源的信息。
完成我们需要的功能。
首先,在Asset文件夹下,新建Editor文件夹,再在Edtior文件夹中新建ReferenceFinder.cs。
using System.Collections.Generic;
using System.IO;
using System.Threading;
using UnityEditor;
using UnityEngine;
public static class ReferenceFinder
{
static string[] assetGUIDs;
static string[] assetPaths;
static string[] allAssetPaths;
static Thread thread;
[MenuItem("Assets/Find References In Project", false)]
[MenuItem("Assets/Find References In Project", false, 25)]
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];
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("查找完成");
}
}
还没有评论,来说两句吧...