TheInitialProject/Assets/Scripts/Editor/AssetRefSearchTool.cs

147 lines
4.0 KiB
C#
Raw Normal View History

2024-10-23 16:59:02 +08:00
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using UnityEditor;
using UnityEngine;
public static class AssetRefSearchTool
{
#if UNITY_EDITOR
static string[] assetGUIDs;
static string[] assetPaths;
static string[] allAssetPaths;
static Thread thread;
[MenuItem("<22><>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD>/<2F><>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD>/<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD><EFBFBD>ã<EFBFBD>Projectѡ<74>еģ<D0B5>", false)]
static void FindAssetRefMenu()
{
if (Selection.assetGUIDs.Length == 0)
{
Debug.Log("<22><><EFBFBD><EFBFBD>ѡ<EFBFBD><D1A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٻ<EFBFBD><D9BB>˲˵<CBB2>");
return;
}
assetGUIDs = Selection.assetGUIDs;
assetPaths = new string[assetGUIDs.Length];
for (int i = 0; i < assetGUIDs.Length; i++)
{
assetPaths[i] = AssetDatabase.GUIDToAssetPath(assetGUIDs[i]);
}
allAssetPaths = AssetDatabase.GetAllAssetPaths();
thread = new Thread(new ThreadStart(FindAssetRef));
thread.Start();
}
[MenuItem("Assets/<2F><>UIPrefabs<62>в<EFBFBD><D0B2>ұ<EFBFBD><D2B1><EFBFBD><EFBFBD>õ<EFBFBD><C3B5><EFBFBD>Դ", false, -99)]
static void FindAssetInUIPrefabs()
{
List<string> logInfo = new List<string>();
string log;
if (Selection.assetGUIDs.Length == 0)
{
Debug.Log("<22><><EFBFBD><EFBFBD>ѡ<EFBFBD><D1A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٻ<EFBFBD><D9BB>˲˵<CBB2>");
return;
}
assetGUIDs = Selection.assetGUIDs;
assetPaths = new string[assetGUIDs.Length];
for (int i = 0; i < assetGUIDs.Length; i++)
{
assetPaths[i] = AssetDatabase.GUIDToAssetPath(assetGUIDs[i]);
}
DirectoryInfo directoryInfo = new DirectoryInfo("Assets/GameAssets/Prefabs/UI");
List<FileInfo> files = new List<FileInfo>();
ListFilesRecursively(directoryInfo, files);
foreach (var fileInfo in files)
{
if (!fileInfo.FullName.EndsWith(".prefab")
&& !fileInfo.FullName.EndsWith(".asset"))
continue;
string content = File.ReadAllText(fileInfo.FullName);
if (content == null)
continue;
for (int j = 0; j < assetGUIDs.Length; j++)
{
if (content.IndexOf(assetGUIDs[j]) > 0)
{
log = string.Format("{0} <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> {1}", fileInfo.FullName, assetPaths[j]);
logInfo.Add(log);
}
}
}
for (int i = 0; i < logInfo.Count; i++)
{
Debug.Log(logInfo[i]);
}
Debug.Log("ѡ<><D1A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>" + logInfo.Count);
Debug.Log("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
}
static void FindAssetRef()
{
Debug.Log(string.Format("<22><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>{0}<7D><><EFBFBD><EFBFBD>Դ<EFBFBD><D4B4>", string.Join(",", assetPaths)));
List<string> logInfo = new List<string>();
string path;
string log;
for (int i = 0; i < allAssetPaths.Length; i++)
{
path = allAssetPaths[i];
if (path.EndsWith(".prefab") || path.EndsWith(".unity"))
{
string content = File.ReadAllText(path);
if (content == null)
{
continue;
}
for (int j = 0; j < assetGUIDs.Length; j++)
{
if (content.IndexOf(assetGUIDs[j]) > 0)
{
log = string.Format("{0} <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> {1}", path, assetPaths[j]);
logInfo.Add(log);
}
}
}
}
for (int i = 0; i < logInfo.Count; i++)
{
Debug.Log(logInfo[i]);
}
Debug.Log("ѡ<><D1A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>" + logInfo.Count);
Debug.Log("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
}
public static void ListFilesRecursively(DirectoryInfo directory, List<FileInfo> files)
{
// <20><>ȡ<EFBFBD><C8A1>ǰĿ¼<C4BF>µ<EFBFBD><C2B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>
files.AddRange(directory.GetFiles().ToArray());
// <20>ݹ鴦<DDB9><E9B4A6><EFBFBD><EFBFBD>Ŀ¼
DirectoryInfo[] subDirectories = directory.GetDirectories();
foreach (var subDirectory in subDirectories)
{
ListFilesRecursively(subDirectory, files);
}
}
#endif
}