效果图:
代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using LuaFramework;
using System.IO;
public class AtlasReferenceTool : EditorWindow
{
[MenuItem("查找/查找所有用到某个图集的预设", false, 130)]
public static void open()
{
var win = GetWindow<AtlasReferenceTool>(false, " AtlasReferenceTool", true);
win.Show();
}
public class PointInfo
{
public string prefabName;
public List<string> pointList;
public void ShowGUI()
{
GUILayout.BeginHorizontal();
if (GUILayout.Button("选中", GUILayout.Width(100)))
{
Selection.activeObject = AssetDatabase.LoadAssetAtPath(prefabName, typeof(GameObject));
}
GUI.color = Color.yellow;
GUILayout.Label(prefabName);
GUI.color = Color.white;
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Space(50);
GUILayout.BeginVertical();
for (int idx = 0; idx < pointList.Count; idx++)
{
GUILayout.Label(pointList[idx]);
}
GUILayout.EndVertical();
GUILayout.EndHorizontal();
}
}
private UIAtlas targetAtlas;
private List<PointInfo> pointInfos;
private Vector2 scrolPos = Vector2.zero;
private void OnGUI()
{
GUILayout.Space(10);
GUILayout.Label("功能说明:用于检查指定图集有没有被UI预设直接引用。");
GUILayout.Space(10);
GUILayout.BeginHorizontal();
GUILayout.Label("查找的图集", GUILayout.Width(60));
targetAtlas = EditorGUILayout.ObjectField("", targetAtlas, typeof(UIAtlas), false, GUILayout.Width(200)) as UIAtlas;
GUILayout.EndHorizontal();
GUILayout.Space(10);
GUILayout.BeginHorizontal();
if (GUILayout.Button("刷新数据"))
{
CheckFontReference();
}
if (GUILayout.Button("清除数据"))
{
if (pointInfos != null && pointInfos.Count > 0)
pointInfos.Clear();
}
GUILayout.EndHorizontal();
GUILayout.Space(10);
if (pointInfos == null || pointInfos.Count <= 0)
{
GUILayout.Label("has nothing to show");
}
else
{
scrolPos = GUILayout.BeginScrollView(scrolPos);
for (int idx = 0; idx < pointInfos.Count; idx++)
{
pointInfos[idx].ShowGUI();
}
GUILayout.EndScrollView();
}
}
private void CheckFontReference()
{
if (targetAtlas == null)
{
Debug.LogError("atlas is null");
return;
}
if (pointInfos == null)
pointInfos = new List<PointInfo>();
else
pointInfos.Clear();
List<string> folders = EditorCommonUtils.NGUIPrefabFoldersList();
string path;
DirectoryInfo dirInfo;
FileInfo[] fileInfos;
string filePath;
for (int idx = 0; idx < folders.Count; idx++)
{
path = folders[idx];
path = string.Format("{0}{1}", Application.dataPath.Substring(0, Application.dataPath.Length - 6), path);
dirInfo = new DirectoryInfo(path);
if (dirInfo.Exists == false)
{
Debug.LogErrorFormat("目录不存在:{0}", path);
continue;
}
fileInfos = dirInfo.GetFiles("*.prefab", SearchOption.AllDirectories);
for (int fileIdx = 0; fileIdx < fileInfos.Length; fileIdx++)
{
filePath = fileInfos[fileIdx].FullName;
filePath = filePath.Replace("\\", "/");
filePath = filePath.Substring(Application.dataPath.Length - 6);
EditorUtility.DisplayProgressBar("进度", filePath, (float)fileIdx / fileInfos.Length);
CheckPrefab(filePath, out bool hasFont, out List<string> points);
if (hasFont)
{
pointInfos.Add(new PointInfo() { prefabName = filePath, pointList = points });
}
}
}
EditorUtility.ClearProgressBar();
pointInfos.Sort(delegate (PointInfo point1, PointInfo point2)
{
return point1.prefabName.CompareTo(point2.prefabName);
});
}
private void CheckPrefab(string assetPath, out bool hasFont, out List<string> points)
{
hasFont = false;
points = new List<string>();
AssetImporter assetImporter = AssetImporter.GetAtPath(assetPath);
if (string.IsNullOrEmpty(assetImporter.assetBundleName))
{
//未设置assetbundle name
Debug.LogError("未设置assetbundle name");
return;
}
GameObject prefabGo = AssetDatabase.LoadAssetAtPath(assetPath, typeof(GameObject)) as GameObject;
string childPath;
UISprite[] uISprites = prefabGo.transform.GetComponentsInChildren<UISprite>(true);
for (int idx = 0; idx < uISprites.Length; idx++)
{
childPath = string.Empty;
if (uISprites[idx].atlas == targetAtlas)
{
GetChildPath(prefabGo.transform, uISprites[idx].transform, ref childPath);
if (points.Contains(childPath) == false)
points.Add(childPath);
hasFont = true;
}
}
points.Sort();
}
private void GetChildPath(Transform root, Transform child, ref string path)
{
if (string.IsNullOrEmpty(path))
path = child.name;
else
path = string.Format("{0}/{1}", child.name, path);
if (child != root && child.parent != null)
{
child = child.parent;
GetChildPath(root, child, ref path);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/// <summary>
/// 编译器辅助器
/// </summary>
class EditorCommonUtils
{
/// <summary>
/// UI预设目录
/// </summary>
/// <returns></returns>
public static List<string> NGUIPrefabFoldersList()
{
List<string> res = new List<string>();
res.Add("Assets");
return res;
}
}