批量将精灵Sprite打包为同一图集tag

    //批量将精灵Sprite打包为同一图集tag
    //节省资源
    public class PackingSpriteToAtlas : EditorWindow
    {
        private string atlasName;//图集名称
        private static PackingSpriteToAtlas instance;//实例

        [MenuItem("Tool/ConvertSpriteToAtlas")]
        static void ShowWindow()//显示窗口
        {
            instance = EditorWindow.GetWindow<PackingSpriteToAtlas>();
            instance.Show();
        }

        static void CreateAtlas(string atlasName)
        {
            Object[] selection = (Object[])Selection.objects;//获取所有精灵
            if (selection.Length == 0)
                return;

            foreach(Object obj in selection)
            {
                Texture texture = (Texture)obj;
                string localPath = AssetDatabase.GetAssetPath(texture);
                TextureImporter ti = (TextureImporter)AssetImporter.GetAtPath(localPath);
                if(ti.textureType == TextureImporterType.Sprite)
                {
                    ti.spritePackingTag = atlasName;
                    AssetDatabase.ImportAsset(localPath);
                }
            }

            AssetDatabase.Refresh();//刷新本地资源
        }

        void OnInspectorUpdate()
        {
            Repaint();//重绘
        }

        private void OnGUI()//绘制界面
        {
            atlasName = EditorGUILayout.TextField("AtlasName:", atlasName);
            if(GUILayout.Button("PackToAtlas",GUILayout.Height(20)))
            {
                if(atlasName != string.Empty)
                {
                    CreateAtlas(atlasName);
                }
            }
        }
    }

猜你喜欢

转载自blog.csdn.net/itsxwz/article/details/81273456