Unity 检测UI预制体使用了哪些图集

写unity工具的时候发现不太友好的地方
比如想获得图集的某些属性 需要手动引入包 否则就没有自动提示 比如SpriteAtlasExtensions这个类
GetPackables方法就是这个类里的
虽然下面这个语法非常方便 但是感觉会有点乱

public static Object[] GetPackables([NotNullAttribute("NullExceptionObject")] this SpriteAtlas spriteAtlas);

事实上只需要注意几个类即可
Selection
AssetDatabase
SpriteAtlasExtensions
Path
在这里插入图片描述
基本上都是这里的类

using System.Reflection;
using System;
using System.Globalization;
using System.Net.Mime;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEngine.UI;
using System.IO;
using UnityEngine.U2D;
using UnityEditor.U2D;

public static class CheckAtlasReferences
{
    
    
    [MenuItem("Assets/Check Atlas References", false, 11)]
    static void FindreAssetFerencesMenu()
    {
    
    
        if (Selection.assetGUIDs.Length == 0 || Selection.activeGameObject == null)
        {
    
    
            Debug.Log("请先选择prefab,再右键点击此菜单");
            return;
        }
        GameObject gameObject = Selection.activeGameObject;
        // Debug.Log(gameObject);
        var atlasList = AssetDatabase.FindAssets("t:SpriteAtlas");
        // Debug.Log("一共" + atlasList.Length + "个图集");
        Dictionary<string, string> path2atlas = new Dictionary<string, string>();
        for (var i = 0; i < atlasList.Length; i++)
        {
    
    
            string str = AssetDatabase.GUIDToAssetPath(atlasList[i]);
            // Debug.Log("第" + i + "个图集地址是:" + str);
            AssetImporter sai = SpriteAtlasImporter.GetAtPath(str);
            SpriteAtlas spriteAtlas = AssetDatabase.LoadAssetAtPath<SpriteAtlas>(str);
            UnityEngine.Object[] AssetList = spriteAtlas.GetPackables();
            for (var k = 0; k < AssetList.Length; k++)
            {
    
    
                string assetPath = AssetDatabase.GetAssetPath(AssetList[k]);
                string tempPath = assetPath + "/a.txt";
                assetPath = Path.GetDirectoryName(tempPath);
                if (path2atlas.ContainsKey(assetPath) == false)
                {
    
    
                    // Debug.Log("设置了Dictionary:" + assetPath);
                    path2atlas.Add(assetPath, str);
                    // Debug.Log("aaa" + Path.GetDirectoryName(assetPath));
                }
                else
                {
    
    
                    var str1 = path2atlas[assetPath];
                    Debug.Log("图集" + str + "和图集" + str1 + "都引用了同一个文件夹" + assetPath);
                    Debug.Log("请检查!!!");
                }
            }
        }

        string[] pathList = GetPathsByGameObject(gameObject);
        HashSet<string> hashSet1 = new HashSet<string>();
        for (var i = 0; i < pathList.Length; i++)
        {
    
    
            string directory = Path.GetDirectoryName(pathList[i]);
            hashSet1.Add(directory);
        }
        Debug.Log(gameObject.name + "使用的图集是");
        foreach (var item in hashSet1)
        {
    
    
            if (path2atlas.ContainsKey(item))
            {
    
    
                // Debug.Log(item);
                Debug.Log(path2atlas[item]);
            }
            else
            {
    
    
                // Debug.Log("这个没用图集:" + item);
            }
        }
    }

    static string[] GetPathsByPath(string path)
    {
    
    
        GameObject gameObject = AssetDatabase.LoadAssetAtPath<GameObject>(path);
        return GetPathsByGameObject(gameObject);
    }

    static string[] GetPathsByGameObject(GameObject gameObject)
    {
    
    
        Image[] img = gameObject.transform.GetComponentsInChildren<Image>(true);
        HashSet<string> hashSet = new HashSet<string>();
        for (var i = 0; i < img.Length; i++)
        {
    
    
            Image image = img[i];
            if (image.sprite != null)
            {
    
    
                string imgPath = AssetDatabase.GetAssetPath(image.sprite);
                hashSet.Add(imgPath);
            }
        }
        foreach (var item in hashSet)
        {
    
    
            string directory = Path.GetDirectoryName(item);//如果只要目录就用这个
        }
        string[] copyArray = new string[hashSet.Count];
        hashSet.CopyTo(copyArray);
        return copyArray;
    }

猜你喜欢

转载自blog.csdn.net/qq_38913715/article/details/124435349