这个方法主要是发现资源乱用的情况,对应的逻辑可能要改一个才能用到自己的项目里面
[MenuItem("Tools/Prefab/查找选中资源依赖关系", false, 0)]
public static void FindDependencies()
{
foreach (var guid in Selection.assetGUIDs)
{
string lpath = AssetDatabase.GUIDToAssetPath(guid);
string dirName = Path.GetDirectoryName(lpath);
string folderName = dirName.Substring(dirName.LastIndexOf("\\") + 1);
Debug.Log("----------------------------------------------------------------");
GameObject go = Selection.activeObject as GameObject;
if (go == null)
{
Debug.Log("选取对象出错");
return;
}
string assetPath = AssetDatabase.GetAssetPath(go);
Dictionary<string, List<ObjStruct>> dePathDic = new Dictionary<string, List<ObjStruct>> ();
if (string.IsNullOrEmpty(assetPath))
{
Debug.Log("注意,这不是预制体");
return;
}
//获取预制体内所有Image组件
Image[] imgArr = go.GetComponentsInChildren<Image>();
if (imgArr == null || imgArr.Length == 0)
{
Debug.Log("预制体里没有Image组件");
}
else
{
foreach (var item in imgArr)
{
string path = AssetDatabase.GetAssetPath(item.sprite.GetInstanceID());
if (!dePathDic.ContainsKey(path))
{
dePathDic.Add(path, new List<ObjStruct> ());
}
ObjStruct objStruct = new ObjStruct();
if (item.sprite != null)
{
objStruct.spriteName = item.sprite.name;
objStruct.objName = item.transform.name;
dePathDic[path].Add(objStruct);
}
}
}
foreach (var dependenciePath in AssetDatabase.GetDependencies(lpath, false))
{
if (lpath != dependenciePath && !dependenciePath.Contains("cs"))
{
if (!dependenciePath.Contains(folderName) && !dependenciePath.Contains("UICommonRes") && !dependenciePath.Contains("Font"))
{
Debug.LogError(string.Format("资源使用不当 path = {0} 依赖 > {1}", lpath, dependenciePath));
if (dePathDic.TryGetValue(dependenciePath,out List<ObjStruct> list))
{
for (int i = 0; i < list.Count; i++)
{
Debug.LogError(string.Format("错误详情:对象名:{0} , Sprite名:{1}", list[i].objName, list[i].spriteName));
}
}
}
else
{
Debug.Log(string.Format("lpath = {0} 依赖 > {1}", lpath, dependenciePath));
}
}
}
}
Debug.Log("查找依赖结束");
}