123
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
public class TextureReferences : EditorWindow
{
private static Dictionary<string, int> Guid2Count = new Dictionary<string, int>();
private static TextureReferences _myEditorWindow;
private static Object _findFolder;
private string _logMsg;
private string _logPrefabReferences;
private string _logAtlasReferences;
private bool _groupEnabled;
private bool _findAtlas = true;
private string _prefabPath = @"Assets/Package/UI";
private string _atlasPath = @"Assets/Package/Atlas";
TextureReferences()
{
EditorApplication.projectWindowItemOnGUI += ProjectWindowItemOnGUI;
}
[MenuItem("Assets/输出文件夹内图片的信息", true)]
private static bool ValidateLogFolderTextureMsg()
{
bool open = Selection.objects.Length == 1 &&
Selection.GetFiltered(typeof(Object), SelectionMode.Assets)[0].GetType().Name == "DefaultAsset";
if (!open)
open = Selection.activeObject.GetType().Name == "Texture2D";
return open;
}
[MenuItem("Assets/输出文件夹内图片的信息")]
static void ShowMyEditorWindow()
{
if (_myEditorWindow != null)
{
_myEditorWindow.Close();
_myEditorWindow = null;
}
Object[] arrs = Selection.GetFiltered(typeof(Object), SelectionMode.Assets);
_findFolder = arrs[0];
GetWindow<TextureReferences>().Show();
}
private void OnGUI()
{
GUILayout.Label("Base Settings", EditorStyles.boldLabel);
_groupEnabled = EditorGUILayout.BeginToggleGroup("配置路径", _groupEnabled);
_findAtlas = EditorGUILayout.Toggle("检索图集", _findAtlas);
_prefabPath = EditorGUILayout.TextField("prefabPath", _prefabPath);
_atlasPath = EditorGUILayout.TextField("atlasPath", _atlasPath);
EditorGUILayout.EndToggleGroup();
_findFolder = EditorGUILayout.ObjectField("目录或图片", _findFolder, typeof(Object), false);
if (GUILayout.Button("执行"))
{
_logMsg = "";
_logAtlasReferences = "";
_logPrefabReferences = "";
if (_findFolder == null)
_logMsg = "目录不能为空!!!";
else
{
LogTextureMsg(_findFolder);
}
}
GUILayout.Label("对应的预制", EditorStyles.boldLabel);
EditorGUILayout.TextArea(_logPrefabReferences);
GUILayout.Label("对应的图集", EditorStyles.boldLabel);
EditorGUILayout.TextArea(_logAtlasReferences);
GUILayout.Label("错误输出", EditorStyles.boldLabel);
EditorGUILayout.TextArea(_logMsg);
}
private void LogTextureMsg(Object _findFolder)
{
string path = AssetDatabase.GetAssetPath(_findFolder);
string[] files = Directory.GetFiles(_prefabPath, "*.prefab", SearchOption.AllDirectories);
string[] atlas = Directory.GetFiles(_atlasPath, "*.spriteatlas", SearchOption.AllDirectories);
List<Object> fileList = new List<Object>();
List<Object> prefabList = new List<Object>();
List<Object> atlasList = new List<Object>();
if (_findFolder.GetType().Name == "Texture2D")
{
FindReference(path, files, ref fileList, ref prefabList);
if(_findAtlas)
FindReference(path, atlas, ref fileList, ref atlasList, false);
}
else
{
string imgType = "*.PNG|*.JPG";
string[] imgTypes = imgType.Split('|');
for (int i = 0; i < imgTypes.Length; i++)
{
string[] dirs = Directory.GetFiles(@path, imgTypes[i]);
for (int j = 0; j < dirs.Length; j++)
{
string target = dirs[j].Replace(@"\", @"/");
FindReference(target, files, ref fileList, ref prefabList);
if(_findAtlas)
FindReference(target, atlas, ref fileList, ref atlasList, false);
}
}
}
Selection.objects = fileList.ToArray();
foreach (var item in prefabList.ToArray())
_logPrefabReferences = _logPrefabReferences + item.name + "\n";
foreach (var item in atlasList.ToArray())
_logAtlasReferences = _logAtlasReferences + item.name + "\n";
}
private void FindReference(string target, string[] files, ref List<Object> fileList, ref List<Object> references, bool isCalCount = true)
{
int count = 0;
for (int i = 0; i < files.Length; i++)
{
string[] source = AssetDatabase.GetDependencies(new string[] { files[i].Replace(Application.dataPath, "Assets") });
for (int j = 0; j < source.Length; j++)
{
if (source[j] == target)
{
var item = AssetDatabase.LoadMainAssetAtPath(files[i].Replace(Application.dataPath, "Assets"));
if(!fileList.Contains(item))
fileList.Add(item);
if(!references.Contains(item))
references.Add(item);
count++;
}
}
}
if (isCalCount)
{
if(count==0)
_logMsg += "该资源未被引用:" + target + "\n";
var imgGuid = AssetDatabase.AssetPathToGUID(target);
if (Guid2Count.ContainsKey(imgGuid))
Guid2Count.Remove(imgGuid);
Guid2Count.Add(imgGuid, count);
}
else
{
if(count==0)
_logMsg += "该资源未打入图集:" + target + "\n";
}
}
private static void ProjectWindowItemOnGUI(string guid, Rect rect)
{
if (Guid2Count.TryGetValue(guid, out var count))
{
var centeredStyle = GUI.skin.GetStyle("Label");
centeredStyle.alignment = TextAnchor.MiddleRight;
centeredStyle.padding.right = 5;
GUI.Label(rect, count.ToString(), centeredStyle);
EditorApplication.RepaintProjectWindow();
}
}
}