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

本文参考: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('\\', '/');
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值