unity 查找资源被哪里引用了

6 篇文章 0 订阅

方法大量借鉴雨松大神的文章,

附上链接表示敬意

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

好处是自己添加了可以多选操作

不好的是没有“”设置某些文件夹忽略“”的功能,那样可以节省不少时间

而且不能“”选中文件夹查找“,那样有些时候更方便

代码如下:

using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using NUnit.Framework;

public class FindReferences
{

	[MenuItem("Assets/Find References", false, 10)]
	static private void Find()
	{
		EditorSettings.serializationMode = SerializationMode.ForceText;

		Object[] SelectedAsset = Selection.GetFiltered(typeof(Object), SelectionMode.Assets|SelectionMode.ExcludePrefab );
		//此处添加需要命名的资源后缀名,注意大小写。
		string[] Filtersuffix = new string[] { ".prefab", ".mat", ".dds", ".png",".jpg" , ".shader" , ".csv", ".wav",".mp3"};
		if (SelectedAsset.Length == 0) return;
		foreach (Object tmpFolder in SelectedAsset) {
			string path = AssetDatabase.GetAssetPath(tmpFolder);
			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 num = 0;
					for (var i = 0; i < files.Length; ++i)
					{
					
					string file = files [i];
						//显示进度条
					EditorUtility.DisplayProgressBar("匹配资源", "正在匹配资源中...", 1.0f * i / files.Length);
					if (Regex.IsMatch (File.ReadAllText (file), guid)) {
												Debug.Log (file, AssetDatabase.LoadAssetAtPath<Object> (GetRelativeAssetsPath (file)));
						num++;					
					}
				}
				if (num == 0) {
					Debug.LogError (tmpFolder.name + "     匹配到" + num + "个",tmpFolder)  ;
				} else if (num == 1) {
					Debug.Log (tmpFolder.name + "     匹配到" + num + "个",tmpFolder)  ;
				} else {
					Debug.LogWarning (tmpFolder.name + "     匹配到" + num + "个",tmpFolder)  ;
				}
				num = 0;
//				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) {
//						
//						EditorApplication.update = null;
//						startIndex = 0;
//						Debug.Log ("匹配结束" + tmpFolder.name);
//					}
//
//				};
			}
		}
		EditorUtility.ClearProgressBar ();
	}

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

放在“Editor”目录下就可以用了。

有时间再完善吧,现在的也够用了。

最近看到个插件

Unity资源断舍离(资源清理重复以及引用被引用查找)

地址:https//blog.csdn.net/akof1314/article/details/83589111

实现的很好

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值