一、查找引用
1.主要思路:
1)使用Directory.GetFiles(Application.dataPath, "*.*", SearchOption.AllDirectories) 获取所有资源。
2)通过File.ReadAllText() 获取资源的详细信息。
3)最后通过正则表达式Regex.IsMatch(), 判断得到的详细信息中是否包含选中资源的guid。
2.代码
[MenuItem("Assets/Find References", false, 10)]
static private void FindReferences()
{
EditorSettings.serializationMode = SerializationMode.ForceText;
string path = AssetDatabase.GetAssetPath(Selection.activeObject);
if (!string.IsNullOrEmpty(path))
{
string guid = AssetDatabase.AssetPathToGUID(path);
var 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();
int startIndex = 0;
EditorApplication.update = delegate ()
{
string file = files[startIndex];
bool isCancel = EditorUtility.DisplayCancelableProgressBar("匹配资源中", file, (float)startIndex / (float)files.Length);
if (Regex.IsMatch(File.ReadAllText(file), guid))
{
Debug.Log(file, AssetDatabase.LoadAssetAtPath<Object>(GetRelativeAssetsPath(file)));
}
startIndex++;
if (isCancel || startIndex >= files.Length)
{
EditorUtility.ClearProgressBar();
EditorApplication.update = null;
startIndex = 0;
Debug.Log("匹配结束");
}
};
}
}
static private string GetRelativeAssetsPath(string path)
{
return "Assets" + Path.GetFullPath(path).Replace(Path.GetFullPath(Application.dataPath), "").Replace('\\', '/');
}
二、查找依赖
1.主要思路
这个相对就比较简单,Unity官方给出了查找依赖的方法函数AssetDatabase.GetDependencies()。
查找依赖很快,就不用加进度条了。
2.代码
static private string GetRelativeAssetsPath(string path)
{
return "Assets" + Path.GetFullPath(path).Replace(Path.GetFullPath(Application.dataPath), "").Replace('\\', '/');
}
[MenuItem("Assets/Find Dependencies", false, 10)]
static private void FindDependencies()
{
string path = AssetDatabase.GetAssetPath(Selection.activeObject);
string[] files = AssetDatabase.GetDependencies(path);
for (int i = 0; i < files.Length; i++)
{
if (files[i].Equals(path))
{
continue; //排除自身
}
Debug.Log(files[i], AssetDatabase.LoadAssetAtPath<Object>(GetRelativeAssetsPath(files[i])));
}
}
三、完整代码
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Collections.Generic;
public class ResourcesUtil
{
[MenuItem("Assets/Find References", false, 10)]
static private void FindReferences()
{
Debug.Log("匹配开始");
EditorSettings.serializationMode = SerializationMode.ForceText;
string path = AssetDatabase.GetAssetPath(Selection.activeObject);
if (!string.IsNullOrEmpty(path))
{
string guid = AssetDatabase.AssetPathToGUID(path);
var 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();
int startIndex = 0;
EditorApplication.update = delegate ()
{
string file = files[startIndex];
bool isCancel = EditorUtility.DisplayCancelableProgressBar("匹配资源中", file, (float)startIndex / (float)files.Length);
if (Regex.IsMatch(File.ReadAllText(file), guid))
{
Debug.Log(file, AssetDatabase.LoadAssetAtPath<Object>(GetRelativeAssetsPath(file)));
}
startIndex++;
if (isCancel || startIndex >= files.Length)
{
EditorUtility.ClearProgressBar();
EditorApplication.update = null;
startIndex = 0;
Debug.Log("匹配结束");
}
};
}
}
static private string GetRelativeAssetsPath(string path)
{
return "Assets" + Path.GetFullPath(path).Replace(Path.GetFullPath(Application.dataPath), "").Replace('\\', '/');
}
[MenuItem("Assets/Find Dependencies", false, 10)]
static private void FindDependencies()
{
Debug.Log("查找开始");
string path = AssetDatabase.GetAssetPath(Selection.activeObject);
string[] files = AssetDatabase.GetDependencies(path);
for (int i = 0; i < files.Length; i++)
{
if (files[i].Equals(path))
{
continue; //排除自身
}
Debug.Log(files[i], AssetDatabase.LoadAssetAtPath<Object>(GetRelativeAssetsPath(files[i])));
}
Debug.Log("查找结束");
}
}