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("查找完成");
}
}