遇到问题: material不知有无被引用,无引用的material留着占用磁盘,也会被UPR提示过多而影响性能考评
从而扩展出问题,特效的png文件夹贼大,也不知有无被引用过,若无,就太浪费了
此为文件夹的操作,时长因项目的大小而定的
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;
using Path = System.IO.Path;
//作 用: FindFolderChildRef
//作 者: zsx
//创建时间: 2023/06/26 09:02
public class FindFolderChildRef : EditorWindow
{
string mInputTxt = @"Assets\_Resources\Effects\Skill\Material\WM";
[MenuItem("Tools/辅助工具/查引用文件夹(子文件的引用)")]
public static void CreateUsageCheckerWindow()
{
EditorWindow.GetWindow<FindFolderChildRef>("文件夹(子文件的引用)");
}
void OnGUI()
{
EditorGUILayout.LabelField("输入的最小文件夹,查询的是子文件引用,\r\n因项目资源多,查询耗时长,请在饭点操作吧", GUILayout.MinHeight(50));
EditorGUILayout.Space(5);
mInputTxt = EditorGUILayout.TextField(mInputTxt, GUILayout.MinWidth(200), GUILayout.MinHeight(22));
EditorGUILayout.Space(5);
if (GUILayout.Button("查询文件夹的子文件引用_耗时长", GUILayout.MinHeight(30)))
{
CheckFolderUsage(mInputTxt); // 替换为要检查的文件夹路径
}
}
void CheckFolderUsage(string folderPath)
{
string[] assetPaths = AssetDatabase.FindAssets("", new[] { folderPath });
var guidDics = new Dictionary<string, string>();
var hasRefDic = new Dictionary<string, bool>();
var count = 0;
foreach (string guid in assetPaths)
{
string path = AssetDatabase.GUIDToAssetPath(guid);
string fileName = Path.GetFileNameWithoutExtension(path);
if (guidDics.ContainsKey(guid) == false)
{
guidDics[guid] = fileName;
hasRefDic[fileName] = false;
count++;
}
}
var bottomLog = "{0}在{1}查找中";
var topLog= "查找引用中(*.prefab,*.unity,*.mat,*.asset)_{0}/{1}_结果看Console";
var indexFind = 0;
var files = GetAllDic();
foreach (var guidItem in guidDics)
{
for (int i = 0; i < files.Length; i++)
{
if (hasRefDic[guidItem.Value] ==true)
{
break;
}
string file = files[i];
if (i % 20 == 0)
{
bool isCancel = EditorUtility.DisplayCancelableProgressBar(string.Format(topLog, indexFind,count), string.Format(bottomLog, guidItem.Value, Path.GetFileNameWithoutExtension(file)), (float)i / (float)files.Length);
if (isCancel)
{
break;
}
}
if (hasRefDic[guidItem.Value] == false && Regex.IsMatch(File.ReadAllText(file), guidItem.Key))
{
hasRefDic[guidItem.Value] = true;
}
}
indexFind++;
}
var noneRef = 0;
StringBuilder sbItem = new StringBuilder();
foreach (var item in hasRefDic)
{
if (item.Value == false)
{
sbItem.Append(item.Key + " ; ");
noneRef++;
}
}
var strLog = mInputTxt + "目录下,共计未引用有" + noneRef + "个,分别是:" + sbItem.ToString();
EditorGUIUtility.systemCopyBuffer = strLog;
Debug.LogError(strLog);
EditorUtility.ClearProgressBar();
ShowNotification(new GUIContent("查找的结果,已copy到剪切板了,请看console_Log"));
}
static string[] allDic;
static string[] GetAllDic()
{
if (allDic == null || allDic.Length <= 0)
{
var withoutExtentsions = new List<string>() { ".prefab", ".unity", ".mat", ".asset" };
allDic = Directory.GetFiles(Application.dataPath "*.*", SearchOption.AllDirectories).Where(s => withoutExtentsions.Contains(Path.GetExtension(s).ToLower())).ToArray();
//allDic = Directory.GetFiles(Application.dataPath+ "/_Resources/Effects", "*.*", SearchOption.AllDirectories).Where(s => withoutExtentsions.Contains(Path.GetExtension(s).ToLower())).ToArray();//若自己知道,则指定文件夹搜索,省时
}
return allDic;
}
}
此为单独文件操作
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;
#region << 脚 本 注 释 >>
//作 用: FindFileRef
//作 者: zsx
//创建时间: 2023/06/25 17:02
#endregion
public class FindFileRef
{
static bool mIsHasRef = false;
[MenuItem("Assets/Find References(查引用)", false, 10)]
static void Find()
{
var guidDics = new Dictionary<string, string>();
foreach (Object item in Selection.objects)
{
string path = AssetDatabase.GetAssetPath(item);
if (string.IsNullOrEmpty(path) == false)
{
string guid = AssetDatabase.AssetPathToGUID(path);
if (guidDics.ContainsKey(guid) == false)
{
guidDics[guid] = item.name;
}
}
}
if (guidDics.Count > 0)
{
Debug.LogError("查找引用 开始");
mIsHasRef = false;
var withoutExtentsions = new List<string>() { ".prefab", ".unity", ".mat", ".asset" };
string[] files = Directory.GetFiles(Application.dataPath, "*.*", SearchOption.AllDirectories).Where(s => withoutExtentsions.Contains(Path.GetExtension(s).ToLower())).ToArray();
for (int i = 0; i < files.Length; i++)
{
string file = files[i];
if (i % 20 == 0)
{
bool isCancel = EditorUtility.DisplayCancelableProgressBar("查找引用中(*.prefab,*.unity,*.mat,*.asset)__结果看Console", file, (float)i / (float)files.Length);
if (isCancel)
{
break;
}
}
foreach (var guidItem in guidDics)
{
if (Regex.IsMatch(File.ReadAllText(file), guidItem.Key))
{
mIsHasRef = true;
Debug.Log("查找的是=" + guidItem.Value + ",引用者=" + AssetDatabase.LoadAssetAtPath<Object>(GetRelativeAssetsPath(file)) + ",文件路径=" + file);
}
}
}
EditorUtility.ClearProgressBar();
if (mIsHasRef)
{
Debug.LogError("查找引用 结束");
}
else
{
Debug.LogError("查找引用 结束,并无引用,请考虑删除否");
}
}
}
[MenuItem("Assets/Find References(查引用)", true)]
static bool VFind()
{
string path = AssetDatabase.GetAssetPath(Selection.activeObject);
return (string.IsNullOrEmpty(path) == false);
}
private static string GetRelativeAssetsPath(string file)
{
return "Assets" + Path.GetFullPath(file).Replace(Path.GetFullPath(Application.dataPath), "").Replace("\\", "/");
}
}
单独查询 选中资源 右键