随着需求的迭代,会发现项目中存在很多无用资源,这些无用的资源导致包体增大。想去删除这些资源,但是害怕有其他资源引用这些文件,所以做了这个工具来检索资源的被引用情况文章来源:https://www.toymoban.com/news/detail-718989.html
直接上源码吧
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;
namespace S
{
public class DependAnalysis : EditorWindow
{
private static Object[] targetObjects;
private bool[] foldoutArr;
private Object[][] beDependArr;
private static int targetCount;
private Vector2 scrollPos;
string[] withoutExtensions = new string[]{".prefab",".unity",".mat",".asset",".controller"};
[MenuItem("Assets/查找被引用", false, 19)]
static void FindReferences()
{
targetObjects=Selection.GetFiltered<Object>(SelectionMode.Assets);
targetCount=targetObjects == null ? 0 : targetObjects.Length;
if (targetCount == 0) return;
DependAnalysis window = GetWindow<DependAnalysis>("依赖分析");
window.Init();
window.Show();
}
void Init()
{
beDependArr=new Object[targetCount][];
foldoutArr=new bool[targetCount];
EditorStyles.foldout.richText = true;
for (int i = 0; i < targetCount; i++)beDependArr[i] = GetBeDepend(targetObjects[i]);
}
private void OnGUI()
{
if (beDependArr.Length != targetCount) return;
scrollPos=EditorGUILayout.BeginScrollView(scrollPos);
Object[] objArr;
int count;
string objName;
for (int i = 0; i < targetCount; i++)
{
objArr = beDependArr[i];
count = objArr == null ? 0 : objArr.Length;
objName = Path.GetFileName(AssetDatabase.GetAssetPath(targetObjects[i]));
string info = count == 0
? $"<color=yellow>{objName}【{count}】</color>"
: $"{objName}【{count}】";
foldoutArr[i] = EditorGUILayout.Foldout(foldoutArr[i], info);
if (foldoutArr[i])
{
if (count>0)
{
foreach (var obj in objArr)
{
EditorGUILayout.BeginHorizontal();
GUILayout.Space(15);
EditorGUILayout.ObjectField(obj,typeof(Object));
EditorGUILayout.EndHorizontal();
}
}
else
{
EditorGUILayout.BeginHorizontal();
GUILayout.Space(15);
EditorGUILayout.LabelField("【Null】");
EditorGUILayout.EndHorizontal();
}
}
}
EditorGUILayout.EndScrollView();
}
/// <summary>
/// 查找所有引用目标资源的物体
/// </summary>
/// <param name="target">目标资源</param>
/// <returns></returns>
private Object[] GetBeDepend(Object target)
{
if (target == null) return null;
string path = AssetDatabase.GetAssetPath(target);
if (string.IsNullOrEmpty(path)) return null;
string guid = AssetDatabase.AssetPathToGUID(path);
string[] files = Directory.GetFiles(Application.dataPath, "*",
SearchOption.AllDirectories).Where(s => withoutExtensions.Contains(Path.GetExtension(s).ToLower())).ToArray();
List<Object> objects= new List<Object>();
foreach (var file in files)
{
string assetPath = file.Replace(Application.dataPath,"");
assetPath = "Assets"+assetPath;
string readText = File.ReadAllText(file);
if (!readText.StartsWith("%YAML"))
{
var depends = AssetDatabase.GetDependencies(assetPath, false);
if (depends!=null)
{
foreach (var dep in depends)
{
if (dep==path)
{
objects.Add(AssetDatabase.LoadAssetAtPath<Object>(assetPath));
break;
}
}
}
}else if (Regex.IsMatch(readText,guid))objects.Add(AssetDatabase.LoadAssetAtPath<Object>(assetPath));
}
return objects.ToArray();
}
private void OnDestroy()
{
targetObjects = null;
beDependArr = null;
foldoutArr = null;
}
}
}
工具截图
文章来源地址https://www.toymoban.com/news/detail-718989.html
使用方法
- 选中需要检测的一个或多个资源
- 右键–>查找被引用
- 打开分析面板,黄色字体显示的部分为无用资源(有可能是代码引用查不出来,需要根据工程特殊分析)
到了这里,关于Unity 资源被引用查找(资源清理)工具的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!