隨著需求的迭代,會發(fā)現(xiàn)項目中存在很多無用資源,這些無用的資源導致包體增大。想去刪除這些資源,但是害怕有其他資源引用這些文件,所以做了這個工具來檢索資源的被引用情況文章來源:http://www.zghlxwxcb.cn/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;
}
}
}
工具截圖
文章來源地址http://www.zghlxwxcb.cn/news/detail-718989.html
使用方法
- 選中需要檢測的一個或多個資源
- 右鍵–>查找被引用
- 打開分析面板,黃色字體顯示的部分為無用資源(有可能是代碼引用查不出來,需要根據(jù)工程特殊分析)
到了這里,關于Unity 資源被引用查找(資源清理)工具的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!