Unity SpriteAtlas (atlas) automatic generation tool

Unity SpriteAtlas (atlas) automatic generation tool

What is an atlas

An atlas is a resource that combines multiple textures into a combined texture. This single texture can be called to issue a single draw call rather than issuing multiple draw calls, enabling one-time access to the compressed texture with a small performance overhead

Why make an atlas?

  1. Reduce DrawCall, only one DrawCall is needed for an atlas
  2. The atlas combines one or more pictures into a power of 2 picture to reduce resource size.
  3. It is obviously more appropriate to hand over all displayed drawing operations to the GPU, which specializes in image processing, than to hand it over to the CPU, so that the idle CPU can concentrate on processing the logical operations of the game.

Things to note when making an atlas

  1. Do not place the image resources that need to be included in the atlas in the Resources folder, because the resources in this folder will not be included in the atlas, and this will also double the size of the resources.

Automatically generate atlas tool source code

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

Instructions

  1. Select folder
  2. Right click->Create Album
  3. The creation is successful, and all the sprite pictures in the selected folder are entered into the newly created album.
    Insert image description here

Guess you like

Origin blog.csdn.net/weixin_42498461/article/details/128874546