本文参考:http://blog.codestage.ru/tag/unity3d/
http://www.xuanyusong.com/archives/4207
---------------------------------2019.03.26 补充---------------------------------
使用GUID查找引用,对图集里的单个Sprite查找相当不友好,所以可以使用Local Identifier In File这个值去匹配,这个值可以在Inspector面板Debug模式下看到。
if (AssetDatabase.TryGetGUIDAndLocalFileIdentifier(Selection.activeObject, out string _guid, out long _localId))
{
string localId = _localId.ToString();
string guid = _guid.ToString();
...
if (Regex.IsMatch(File.ReadAllText(file), guid) &&
Regex.IsMatch(File.ReadAllText(file), localId)) { ... }
}
------------------------------------------end------------------------------------------
这里有个很奇怪的问题,必须左键先选中目标资源,再右键查找,不然集中显示是扯淡:)我也不知道为啥子...
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;
public class FindReferences
{
[MenuItem("Assets/Find References In Project", false, 30)]
static private void Find()
{
EditorSettings.serializationMode = SerializationMode.ForceText;
string path = AssetDatabase.GetAssetPath(Selection.activeObject);
if (!string.IsNullOrEmpty(path))
{
string guid = AssetDatabase.AssetPathToGUID(path);
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();
int startIndex = 0;
List<int> select = new List<int>();
select.Add(Selection.activeObject.GetInstanceID());
EditorApplication.update = delegate ()
{
string file = files[startIndex];
bool isCancel = EditorUtility.DisplayCancelableProgressBar("匹配资源中", file, (float)startIndex / (float)files.Length);
if (Regex.IsMatch(File.ReadAllText(file), guid))
{
select.Add(AssetDatabase.LoadAssetAtPath<Object>(GetRelativeAssetsPath(file)).GetInstanceID());
}
startIndex++;
if (isCancel || startIndex >= files.Length)
{
EditorUtility.ClearProgressBar();
EditorApplication.update = null;
startIndex = 0;
Type projectBrowserType = Type.GetType("UnityEditor.ProjectBrowser,UnityEditor");
if (projectBrowserType != null)
{
FieldInfo lastProjectBrowser = projectBrowserType.GetField("s_LastInteractedProjectBrowser", BindingFlags.Static | BindingFlags.Public);
if (lastProjectBrowser != null)
{
object lastProjectBrowserInstance = lastProjectBrowser.GetValue(null);
FieldInfo projectBrowserViewMode = projectBrowserType.GetField("m_ViewMode", BindingFlags.Instance | BindingFlags.NonPublic);
if (projectBrowserViewMode != null)
{
// 0 - one column, 1 - two column
int viewMode = (int)projectBrowserViewMode.GetValue(lastProjectBrowserInstance);
if (viewMode == 1)
{
MethodInfo showFolderContents = projectBrowserType.GetMethod("ShowObjectsInList", BindingFlags.NonPublic | BindingFlags.Instance);
if (showFolderContents != null)
{
showFolderContents.Invoke(lastProjectBrowserInstance, new object[] { select.ToArray()});
}
else
{
Debug.LogError("Can't find ShowFolderContents method!");
}
}
}
else
{
Debug.LogError("Can't find m_ViewMode field!");
}
}
else
{
Debug.LogError("Can't find s_LastInteractedProjectBrowser field!");
}
}
Selection.instanceIDs = select.ToArray();
}
};
}
}
[MenuItem("Assets/Find References", true)]
static private bool VFind()
{
string path = AssetDatabase.GetAssetPath(Selection.activeObject);
return (!string.IsNullOrEmpty(path));
}
static private string GetRelativeAssetsPath(string path)
{
return "Assets" + Path.GetFullPath(path).Replace(Path.GetFullPath(Application.dataPath), "").Replace('\\', '/');
}
}