图集SpriteMetaData数据拷贝

关于SpriteMetaData类:

用于生成精灵的编辑器数据。
官方文档介绍:https://docs.unity3d.com/2017.2/Documentation/ScriptReference/SpriteMetaData.html

Unity编辑器拓展之二十五:图集SpriteMetaData数据拷贝

工具界面

Unity编辑器拓展之二十五:图集SpriteMetaData数据拷贝

工具使用场景

适用于游戏中存在多套皮肤图集

使用方法

拖入数据源图集的texture、克隆图集的texture,然后点击Copy,就会将clone texture 的Sprite data 拷贝到clone texture 中,对应关系是按照sprite name来对比的。

具体代码

using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace Editor.CopyAtlasDataTool
{
    public class CopyAtlasDataTool : EditorWindow
    {
        private static CopyAtlasDataTool window = null;
        
        [MenuItem("Tools/CopyAtlasDataTool")]
        public static CopyAtlasDataTool ShowGameDataEditorWindow()
        {
            if (window == null)
                window = EditorWindow.GetWindow(typeof(CopyAtlasDataTool)) as CopyAtlasDataTool;
            window.titleContent = new GUIContent("CopyAtlasDataTool");
            window.Show();
            return window;
        }

        private UnityEngine.Object _originObject;
        private UnityEngine.Object _cloneObject;
        private void OnGUI()
        {
            _originObject = EditorGUILayout.ObjectField("数据源图集", _originObject,typeof(Texture));
            _cloneObject = EditorGUILayout.ObjectField("克隆图集", _cloneObject, typeof(Texture));
            if (GUILayout.Button("Copy"))
            {
                if (_originObject != null && _cloneObject != null)
                {
                    CopyData(_originObject, _cloneObject);
                }
            }
        }

        public void CopyData(Object origin, Object clone)
        {
            string originAssetPath = AssetDatabase.GetAssetPath(origin);
            string cloneAssetPath = AssetDatabase.GetAssetPath(clone);
            
            TextureImporter originTi = AssetImporter.GetAtPath(originAssetPath) as TextureImporter;
            TextureImporter cloneTi = AssetImporter.GetAtPath(cloneAssetPath) as TextureImporter;

            
            if (originTi != null && cloneTi != null)
            {
            	//打开读写,才能写入数据
                cloneTi.isReadable = true;

                List < SpriteMetaData > newData = new List < SpriteMetaData > ();
                List<string> hasAddData = new List<string>();
                for (int i = 0; i < originTi.spritesheet.Length; i++)
                {
                    for (int j = 0; j < cloneTi.spritesheet.Length; j++)
                    {
                        SpriteMetaData oSm = originTi.spritesheet[i];
                        SpriteMetaData cSm = cloneTi.spritesheet[j];
                        //根绝sprite name进行匹配的
                        if (oSm.name == cSm.name)
                        {
                            cSm.border = oSm.border;
                            cSm.pivot = oSm.pivot;
                            cSm.alignment = oSm.alignment;
                            
                            newData.Add(cSm);
                            hasAddData.Add(cSm.name);
                        }
                    }
                }

                for (int j = 0; j < cloneTi.spritesheet.Length; j++)
                {
                    SpriteMetaData cSm = cloneTi.spritesheet[j];
                    if (!hasAddData.Contains(cSm.name))
                    {
                        newData.Add(cSm);
                    }
                }

                cloneTi.spritesheet = newData.ToArray();
                
                AssetDatabase.ImportAsset(cloneAssetPath, ImportAssetOptions.ForceUpdate);
                
                cloneTi = AssetImporter.GetAtPath(cloneAssetPath) as TextureImporter;
                if(cloneTi != null)
                    cloneTi.isReadable = false;
                
                AssetDatabase.ImportAsset(cloneAssetPath, ImportAssetOptions.ForceUpdate);
            }
        }
    }
}

注意点

(1)克隆图集的TextureImporter的isReadable要在复制前打开,不然不能写入数据
(2)SpriteMetaData是结构体,要注意结构体和类的区别
(3)copy完后,注意检查两个图集的读写是否正确关闭了

以上知识分享,如有错误,欢迎指出,共同学习,共同进步。

最近在用hexo 和 github page搭 个人博客,地址如下:
https://jingfengji.github.io/
欢迎大家关注。

最近的一些博客 还是会更新在 CSDN这边,后续以自己个人的博客站点会主。

相关文章:

  • 2022-12-23
  • 2021-08-08
  • 2022-12-23
  • 2021-04-29
  • 2021-12-02
  • 2022-12-23
  • 2021-11-19
  • 2021-05-28
猜你喜欢
  • 2022-01-06
  • 2021-05-26
  • 2022-12-23
  • 2022-01-10
  • 2021-10-27
  • 2021-04-06
  • 2021-06-06
相关资源
相似解决方案