在项目后期,美术面板换了几波后变得十分庞大,有大量旧的美术资源可能已经被弃用了,需要删掉,可以通过这个方式查找某个资源在全部预制中的引用并输出路径,如果没有引用了可以删掉,代码如下
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<string> ThreadRun(ThreadPars par);
private const int ThreadCount = 4;
public class ThreadPars
{
public List<string> CheckList = new List<string>();
public string AimGuid;
}
private static List<string> ThreadFind(ThreadPars par)
{
List<string> ret = new List<string>();
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<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();
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<string> re = new List<string>();
for (int i = 0; i < ThreadCount; i++)
{
re.AddRange(tRun[i].EndInvoke(results[i]));
}
foreach (var s in re)
{
Debug.Log(s, AssetDatabase.LoadAssetAtPath<Object>(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中通过多线程查找特定美术资源在项目所有预制体中的引用,并输出预制体的完整路径。当资源不再使用时,此方法可以帮助清理无用的资源。
&spm=1001.2101.3001.5002&articleId=90289448&d=1&t=3&u=0b5494093b5a485c8f041bde03d087fb)
1065

被折叠的 条评论
为什么被折叠?



