UnityEditor——查找一个美术资源在项目中全部预制的引用并输出预制路径(多线程法)

在项目后期,美术面板换了几波后变得十分庞大,有大量旧的美术资源可能已经被弃用了,需要删掉,可以通过这个方式查找某个资源在全部预制中的引用并输出路径,如果没有引用了可以删掉,代码如下

UnityEditor——查找一个美术资源在项目中全部预制的引用并输出预制路径(多线程法)_第1张图片 

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
using Object = UnityEngine.Object;

namespace Editor
{
	public static class FindReferences
	{
		private static EditorApplication.CallbackFunction _updateDelegate;

		public delegate List ThreadRun(ThreadPars par);
		
		private const int ThreadCount = 4;
		
		public class ThreadPars
		{
			public List CheckList = new List();
			public string AimGuid;
		}

		private static List ThreadFind(ThreadPars par)
		{
			List ret = new List();
			if (par != null)
			{
				foreach (var file in par.CheckList)
				{
					if (Regex.IsMatch(File.ReadAllText(file),par.AimGuid))
					{
						ret.Add(file);
					}
				}
			}

			return ret;
		}

		[MenuItem("Assets/Find References Thread",false,10)]
		private static void FindThread()
		{
			EditorSettings.serializationMode = SerializationMode.ForceText;
			EditorHelper.OpenConsole();
			EditorHelper.ClearConsole();
			AssetDatabase.Refresh();
			string path = AssetDatabase.GetAssetPath(Selection.activeObject);
			if (!string.IsNullOrEmpty(path))
			{
				string guid = AssetDatabase.AssetPathToGUID(path);
				List withoutExtensions = new List() {".prefab", ".unity", ".mat", ".asset"};
				string[] files = Directory.GetFiles(Application.dataPath, "*.*", SearchOption.AllDirectories)
					.Where(s => withoutExtensions.Contains(Path.GetExtension(s).ToLower())).ToArray();

				ThreadPars[] threadParses = new ThreadPars[ThreadCount];
				for (int i = 0; i < ThreadCount; i++)
				{
					threadParses[i] = new ThreadPars();
					threadParses[i].AimGuid = guid;
				}

				for (int i = 0; i < files.Length; i++)
				{
					int index = i % ThreadCount;
					threadParses[index].CheckList.Add(files[i]);
				}

				ThreadRun[] tRun = new ThreadRun[ThreadCount];
				int finishedState = ThreadCount;
				
				IAsyncResult[] results = new IAsyncResult[ThreadCount];
				
				_updateDelegate = delegate
				{
					var finishedCount = 0;
					for (int i = 0; i < ThreadCount; i++)
					{
						if (results[i].IsCompleted) ++finishedCount;
					}
					
					EditorUtility.DisplayProgressBar("匹配资源中",string.Format("进度:{0}",finishedCount),finishedCount *1f/ThreadCount);

					if (finishedCount >= finishedState)
					{
						List re = new List();
						for (int i = 0; i < ThreadCount; i++)
						{
							re.AddRange(tRun[i].EndInvoke(results[i]));
						}

						foreach (var s in re)
						{
							Debug.Log(s, AssetDatabase.LoadAssetAtPath(GetRelativeAssetsPath(s)));
						}
						EditorUtility.ClearProgressBar();
						EditorApplication.update -= _updateDelegate;
					}
				};

				for (int i = 0; i < ThreadCount; i++)
				{
					tRun[i] = ThreadFind;
					results[i] = tRun[i].BeginInvoke(threadParses[i], null, null);
				}

				EditorApplication.update += _updateDelegate;
			}

		}

		private static string GetRelativeAssetsPath(string path)
		{
			return "Assets" + Path.GetFullPath(path).Replace(Path.GetFullPath(Application.dataPath), "").Replace('\\','/');
		}	
	}
}

 

你可能感兴趣的:(UnityEditor)