【工作笔记】在Project中查找引用资源,并在Project视图TwoColumn模式下集中显示

本文参考:http://blog.codestage.ru/tag/unity3d/

                 http://www.xuanyusong.com/archives/4207

这里有个很奇怪的问题,必须左键先选中目标资源,再右键查找,不然集中显示是扯淡:)我也不知道为啥子...

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('\\', '/');
	}
}

猜你喜欢

转载自blog.csdn.net/QverOo/article/details/86171112