图集检查工具

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();
        }
    }
    
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Kerven_HKW

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值