Unity提供了某個資源引用了什麼資源,但是沒有提供某個資源被什麼別的資源引用。
可以寫個腳本進行查找。
把下面的代碼保存爲FindReferences.cs
放在Editor
目錄中,然後選中Project中的某個資源,右鍵,點擊菜單Find References
即可在日誌中看到該資源被什麼別的資源引用。
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Collections.Generic;
public class FindReferences
{
[MenuItem("Assets/Find References", false, 10)]
static private void Find()
{
Dictionary<string,string>guidDics =new Dictionary<string, string>();
foreach(Object o in Selection.objects){
string path = AssetDatabase.GetAssetPath(o);
if(!string.IsNullOrEmpty(path)){
string guid = AssetDatabase.AssetPathToGUID(path);
if(!guidDics.ContainsKey(guid)){
guidDics[guid] = o.name;
}
}
}
if(guidDics.Count >0)
{
List<string> withoutExtensions = new List<string>(){".prefab",".unity",".mat",".asset"};
string[] files = Directory.GetFiles(Application.dataPath, "*.*", SearchOption.AllDirectories)
.Where(s => withoutExtensions.Contains(Path.GetExtension(s).ToLower())).ToArray();
for(int i=0; i<files.Length;i++)
{
string file = files[i];
if(i%20==0){
bool isCancel = EditorUtility.DisplayCancelableProgressBar("匹配資源中", file, (float)i / (float)files.Length);
if (isCancel){
break;
}
}
foreach(KeyValuePair<string,string> guidItem in guidDics){
if (Regex.IsMatch(File.ReadAllText(file), guidItem.Key))
{
Debug.Log(string.Format("name: {0} file: {1}",guidItem.Value,file), AssetDatabase.LoadAssetAtPath<Object>(GetRelativeAssetsPath(file)));
}
}
}
EditorUtility.ClearProgressBar();
Debug.Log("匹配結束");
}
}
}