Unity SpriteAtlas(图集)自动生成工具

Unity SpriteAtlas(图集)自动生成工具

什么是图集

图集是一种将多个纹理合并为一个组合纹理的资源。 可以调用此单个纹理来发出单个绘制调用而不是发出多个绘制调用,能够以较小的性能开销一次性访问压缩的纹理

为什么要打图集

  1. 减少DrawCall,一张图集只需要一次DrawCall
  2. 图集将一张或者多张图片合成一张2的幂次方的图片,减少资源大小
  3. 把所有显示的绘制操作交给专门处理图像的GPU显然比交给CPU更合适,这样空闲下来的CPU可以集中力量处理游戏的逻辑运算

打图集的注意事项

  1. 需要打图集的图片资源不要放在Resources文件夹下,因为该文件夹中的资源不会被打入图集,而且这样也会导致资源大小翻倍

自动生成图集工具源码

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

namespace S
{
    public class AutoAtlas
    {
        static SpriteAtlasPackingSettings packSetting = new SpriteAtlasPackingSettings()
        {
            blockOffset = 1,
            padding = 2,
            enableRotation = false,
            enableTightPacking = false
        };

        private static SpriteAtlasTextureSettings textureSetting = new SpriteAtlasTextureSettings()
        {
            sRGB = true,
            filterMode = FilterMode.Bilinear,
        };

        private static TextureImporterPlatformSettings importerSetting = new TextureImporterPlatformSettings()
        {
            maxTextureSize = 2048,
            compressionQuality = 50,
        };

        [MenuItem("Assets/创建图集", false, 19)]
        static void CreateAtlas()
        {
            var select = Selection.activeObject;
            var path = AssetDatabase.GetAssetPath(select);
            CreateAtlasOfAssetDir(path);
        }

        public static void CreateAtlasOfAssetDir(string dirAssetPath)
        {
            if (string.IsNullOrEmpty(dirAssetPath) || Path.HasExtension(dirAssetPath))
            {
                Debug.LogError("当前选中对象不是文件夹,请选择对应文件夹重新创建图集");
                return;
            }
            
            SpriteAtlas atlas = new SpriteAtlas();
            atlas.SetPackingSettings(packSetting);
            atlas.SetTextureSettings(textureSetting);
            atlas.SetPlatformSettings(importerSetting);

            var atlasPath = $"{dirAssetPath}/New Atlas.spriteatlas";
            TryAddSprites(atlas, dirAssetPath);
            AssetDatabase.CreateAsset(atlas, atlasPath);
            AssetDatabase.SaveAssets();
            Selection.activeObject = AssetDatabase.LoadAssetAtPath<Object>(atlasPath);
        }

        static void TryAddSprites(SpriteAtlas atlas, string dirPath)
        {
            string[] files = Directory.GetFiles(dirPath);
            if (files == null || files.Length == 0) return;

            Sprite sprite;
            List<Sprite> spriteList = new List<Sprite>();
            foreach (var file in files)
            {
                if (file.EndsWith(".meta")) continue;
                sprite = AssetDatabase.LoadAssetAtPath<Sprite>(file);
                if (sprite == null) continue;
                spriteList.Add(sprite);
            }

            if (spriteList.Count > 0) atlas.Add(spriteList.ToArray());
        }
    }
}

使用方法

  1. 选中文件夹
  2. 右键–>创建图集
  3. 创建成功,选中文件夹中的所有精灵图片都被打入新建的图集中
    在这里插入图片描述
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

司军礼

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

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

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

打赏作者

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

抵扣说明:

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

余额充值