[Unity Optimize] Use the Atlas (Sprite Atlas) to optimize the project

Table of Contents

  • 1 Introduction to Sprite Atlas
  • 2 Create and configure Sprite Atlas
    • 2.1 Create Sprite Atlas
      • 2.1.1 Unity2D project
      • 2.1.2 Unity3D project
    • 2.2 Configure Sprite Atlas
    • 2.3 Precautions
  • 3 Interface of Sprite Atlas
  • 4 Optimization suggestions for Sprite Atlas

1 Introduction to Sprite Atlas

The Atlas (Sprite Atlas) in Unity is a texture set used to optimize game performance and memory. Sprite Atlas can be applied to UIs, particle systems, textures, and more in both 2D and 3D projects.

It is very convenient to create a Sprite Atlas with Unity, just create a Sprite Atlas GameObject, and then assign the texture to it. When creating a Sprite Atlas, you need to organize the texture names by role or function, and set the import settings. As textures are added, Unity automatically distributes them across the Sprite Atlas in an optimal layout so that the GPU can process them in batches rather than individually. However, if the texture cannot completely fill the Sprite Atlas, multiple Sprite Atlas can be combined to maximize space utilization.

2 Create and configure Sprite Atlas

2.1 Create Sprite Atlas

2.1.1 Unity2D project

In the Unity 2D project, right-click Create → 2D → Sprite Atlas in the Assets folder in the Project panel to create a Sprite Atlas.

2.1.2 Unity3D project

In the Unity 3D project, you need to open the atlas in the settings, and you can create Sprite Atlas in the Project panel.
In the menu bar, Edit → Project Settings → find Editor on the left side bar → find Sprite Packer on the right side, where the Mode property is selected as required (the default is Disabled).

In addition, you need to install the 2D Sprite package, menu bar Window → Package Manager:

Sprite package

After the package installation is complete, create a Sprite Atlas in the same way:

2.2 Configuring Sprite Atlas

Click the newly created Sprite Atlas in the Project panel to observe the properties of its Inspector panel:

  • Type: Master or Variant. Combining the Master Sprite Atlas and the Variant Sprite Atlas can greatly simplify the management of 2D game assets.

    • Master: The Master Sprite Atlas contains a set of Sprites, as well as texture information common to all Sprites. When creating and editing a Master Sprite Atlas, you don’t need to consider the specific resolution or platform, just add the Sprite and the texture material of the Sprite to it. In Master Sprite Atlas, sprites are packaged into the same texture to increase the rendering efficiency of sprites.

    • Variant: Variant Sprite Atlas takes the Master Sprite Atlas and assigns it to different platforms or resolutions to generate various texture variants. Variant Sprite Atlases are generated from pre-configured Platform-specific Sprite Atlases that may take into account the platform and device on which they will ultimately run. Among them, modifying Scale can change the resolution of Sprite Atlas.

  • Include in Build: Whether to include the Sprite Atlas in the build.

    • If this option is unchecked, the Sprite Atlas will not be included in the build and will need to be manually loaded in-game.
    • If this option is checked, the Sprite Atlas will be automatically included in the build.
  • Allow Rotation: Whether to allow rotation of Sprite.

    • If checked, the packer can rotate the sprite to fill the empty space as much as possible.
    • It is recommended to check to improve the packaging effect.
  • Tight Packing: Whether to enable precise packaging technology.

    • If enabled, the packer calculates sprite bounds more accurately, but also uses more memory.
    • For resource-constrained situations, it is recommended to close it.
  • Alpha Dilation: Whether to apply Alpha Dilation filtering. This is useful for improving the rendering quality of sprites, for example to prevent jagged edges when sprites are scaled up.

  • Padding: Set the amount of padding when packing Sprites, which is used to control the distance between Sprites and avoid overlapping or corner transitions between Sprites in Atlas. It can be set to 2, 4 or 8 pixels.

  • Read/Write: Whether to allow read and write operations of textures in Sprite Atlas.

    • If you do not need to modify the pixel data of the Sprite, it is recommended to turn off this option to improve performance and memory usage.
  • Generate Mip Maps: Whether to generate Mipmaps.

    • If the game requires rendering textures at a distance, it is recommended to enable this option to improve image quality and performance.
  • sRGB: Whether to enable sRGB format.

    • If checked, sRGB format will be used; otherwise, linear format will be used.
  • Filter Mode: Set the texture filtering mode of Sprite Atlas, you can choose Trilinear, Bilinear or Point.

    • In Trilinear mode, three MIPs are used to smooth progressive detail.
    • In Bilinear mode, two MIPs are used to smooth progressive detail.
    • In Point mode, the Texel closest to the target pixel in the Texture is displayed.
  • Show Platform Settings For: Specify the configuration of each platform. You can set the texture resolution, maximum sprite size, etc. supported by each platform.

    • Max Texture Size: The maximum resolution of the texture. When choosing, you need to take into account the differences in the capabilities and supported resolutions of various devices.
    • Format: The format of the texture. Find the best balance between quality, size and runtime performance by choosing from different formats.
      • Automatic: Unity’s default setting. It allows Unity to automatically choose the most suitable pixel format. In most cases, this is the best choice.
      • RGBA 32 bit: This is one of the most common pixel formats and contains four channels of RGBA. Using this format provides the highest image quality, but also requires more memory and bandwidth.
      • RGB 16 bit: This format uses less memory, but also uses less bandwidth. It only contains RGB channels, so it is the format to choose if the image does not require an alpha channel.
      • RGB 24 bit: This format also contains only RGB channels, but each channel is finer than RGB 16 bit, so it takes up more space.
      • R 8: This format contains only one channel, so it is mainly used for textures that store single-channel data such as height maps and light maps.
      • R 16 bit: This format also contains only one channel, but each channel uses more memory to store higher precision.
      • Alpha 8: This format contains only the alpha channel. For situations where only some simple transparency information is required, Alpha 8 is the most suitable choice because it takes up very little memory.
    • Compression: Compression quality. You can choose Low Quality, Normal Quality and High Quality according to your needs. The higher the compression quality, the higher the compression ratio, but at the same time it will have a more obvious impact on the quality of the texture, which may lead to the loss of image details. High quality compression results in larger texture sizes, which can cause performance issues on some platforms.
    • Use Crunch Compression: Whether to choose to use the zero-delay compression (Crunch Compression) algorithm. This algorithm allows textures to be stored in a smaller size while retaining higher quality and faster load times. However, Crunch compression takes some time to compress, so it may increase build times.
  • Objects for Packing: In which Atlas are the Sprites of the Sprite Atlas packed.

    • Folders, textures or individual sprites can be selected.
    • If left blank, Unity will attempt to pack the sprite into all sprite atlases.
  • Pack Preview: Click this button to display a preview of how Unity will lay out the sprite.

2.3 Notes

  • UGUI packaging Sprite Atlas cannot check Tight Packing, otherwise the Sprites will overlap.
    • UGUI is based on rectangle graphics rendering, so it uses a rectangle for each sub-picture to render. If the Tight Packing option is checked, UGUI will use a compact stacking method to place rectangles together. However, if the sprites are non-square or rotated, this stacking will cause the rectangles to intersect each other, rendering them incorrectly.
  • And Sprite Renderer can check Tight Packing.
    • When a Sprite Renderer processes a sprite atlas, it generates a mesh based on the non-transparent parts of the sprites, so rotated and/or tightly-packed sprites can be used. The Tight Packing option of the Sprite Renderer component is only to save the space occupied by the texture, it will not affect the display effect of the Sprite.
  • Spine’s atlas PNG is packaged into Unity’s Sprite Atlas, because the memory will become two copies,
    • Spine directly reads the PNG of the atlas, and does not automatically associate it with the pictures in SpriteAtlas. Packing the Spine Atlas PNG into Unity’s Sprite Atlas will cause the memory usage to become two copies, resulting in a waste of resources.

3 Sprite Atlas interface

Sprite Atlas provides some interfaces to easily create, manage and access Sprites. The following are examples of common access interfaces of Sprite Atlas. Refer to API interface – SpriteAtlasExtensions:

using UnityEditor;
using UnityEditor. U2D;
using UnityEngine;
using UnityEngine.U2D;

public class Atlas API : MonoBehaviour
{<!-- -->
    public Texture2D[] textures; // Array to store images

    // Start is called before the first frame update
    void Start()
    {<!-- -->
        // create SpriteAtlas
        SpriteAtlas spriteAtlas = new SpriteAtlas();

        // Add image to SpriteAtlas
        foreach (Texture2D texture in textures)
        {<!-- -->
            // Create a Sprite object
            Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
            // Add to SpriteAtlas
            spriteAtlas. Add(new Sprite[] {<!-- --> sprite });
        }

        // Save the created SpriteAtlas as a .spriteatlas file
        AssetDatabase.CreateAsset(spriteAtlas, "Assets/NewSpriteAtlas.spriteatlas");
        AssetDatabase. Refresh();

        // Get the name of the current platform
        spriteAtlas.GetPlatformSettings(GetPlatformName(EditorUserBuildSettings.activeBuildTarget));
        // Modify the atlas settings on the corresponding platform
        spriteAtlas.SetPlatformSettings(new TextureImporterPlatformSettings
        {<!-- -->
            maxTextureSize = 2048,
            textureCompression = TextureImporterCompression. CompressedHQ,
            format = TextureImporterFormat. Automatic,
        });

    }

    string GetPlatformName(BuildTarget target)
    {<!-- -->
        string platformName = "";
        switch (target)
        {<!-- -->
            case BuildTarget.Android:
                platformName = "Android";
                break;
            case BuildTarget.iOS:
                platformName = "iPhone";
                break;
            case BuildTarget.PS4:
                platformName = "PS4";
                break;
            case BuildTarget.XboxOne:
                platformName = "XboxOne";
                break;
            case BuildTarget.NoTarget:
                platformName = "DefaultTexturePlatform";
                break;
            default:
                platformName = "Standalone";
                break;
        }
        return platformName;
    }
}

4 Optimization suggestions for Sprite Atlas

Here are some suggestions for optimizing your game with Sprite Atlas:

  • Place sprites of the same size and type in the same Sprite Atlas.

  • Compressed sprite pixel format.

  • Allowing rotation of sprites increases the number of sprites that can be packed in the Sprite Atlas.

  • Make sure the Sprite Atlas size does not exceed the maximum texture size supported by the hardware device.

  • Asynchronously load and unload Sprite Atlases on demand at runtime.