[Unity component knowledge] How to package the atlas in Unity2020 and later versions

Article directory

foreword

1. Steps of packing the atlas

1. Import the 2D Sprite package

2. Enable the function of atlas packaging

3. Create a Sprite Atlas file

4. Add pictures or folders to be packaged

2. Use of Atlas

 3. Introduction to Mode of SpritePacker

4. Basic attributes of Sprite Altas

1.Type

2.Master (Main Gallery)

3.Variant (variant)

4.Texture

5.Objects for Pakcing

6.Pack Preview button

Highlights

Precautions


foreword

        In Unity, usually rendering a texture calls DrawCall once. A 2D project may contain a large number of textures. If each texture is drawn and DrawCall is called once, it will take up too many resources and affect the performance of the entire application. In order to reduce performance consumption, we can use the sprite atlas (Sprite Atlas) technology, which can combine multiple textures (textures) into one large texture. When accessing multiple textures in the atlas, it only needs to call DrawCall once.

        For versions after Unity2020, the old version of SpritePacker is abandoned, and the Packing Tag in the image settings is grayed out and cannot be modified. Need to use the new version of Sprite Atlas for atlas packaging.

1. Steps of packing the atlas

1. Import the 2D Sprite package

        Select Window->Package Manager, select Unity Registry, then find 2D Sprite, and import it into the project.

2. Enable the function of atlas packaging

        Select Edit -> Project Settings -> Editor, and switch the Mode of SpritePacker to the mode you need. I chose Sprite Atlas V1 - Always Enabled here.

3. Create a Sprite Atlas file

        Click the right mouse button in the Project view, Create->2D->Sprite Atlas.

4. Add pictures or folders to be packaged

        Put the pictures or folders to be packed into the Objects for Packing of the atlas file. Then click the PackPreview button to see the atlas preview.

2. Use of Atlas

  • Use the original image directly in the scene

  • Get pictures by GetSprite method
using UnityEngine;
using UnityEngine.U2D;
using UnityEngine.UI;

public class UIPanel : MonoBehaviour
{
    public SpriteAtlas atlas;
    public Image img;
    private void Start()
    {
        img.sprite = atlas.GetSprite("Rank");
    }
}

 3. Introduction to Mode of SpritePacker

  • Disabled

        Disable gallery related functions.

  • Sprite Atlas V1 - Enabled For Builds

        Build atlases only when publishing (Build), referencing original textures in Editor and Play mode instead of textures in the atlas.

  • Sprite Atlas V1 - Always Enabled

        Reference textures from atlases at runtime (Build and Play modes), and raw textures in Editor mode.

  • Sprite Atlas V2 (Experimental) - Enabled :

        This feature of the Unity2020.3 version is still in the experiment, and the texture in the atlas is referenced by default at runtime.

        Sprite Atlas V1 does not support cache server (Cache Server), Unity can only store packaged atlas data in Library/AtlasCachefolders, and cannot have dependencies, does not support named objects importer (named objects importer), and Sprite Atlas V2 Support for the above functions is provided. For details, please refer to the Unity Manual: Sprite Atlas version 2 (Experimental)

4. Basic attributes of Sprite Altas

1.Type

        Set the type of sprite atlas, the default is Master (main atlas), and it can also be set to Variant (variant atlas).

2.Master (Main Gallery)

  • Include in Build

        Whether to automatically load the atlas into memory when the sprite in the atlas is used at runtime.

        Unity will include atlases in the packaged project and automatically load them at runtime. Uncheck the Include in Build option of the atlas to disable this behavior.

        If Include in Build is disabled, Unity will still package the atlas into the **.spriteatlas * file in the project Assets folder, but it will not be loaded into memory when running. Therefore, when a sprite references a texture in a disabled atlas, the texture will not be found because the referenced texture is not available or loaded, and the image referencing it will appear blank. To load the sprite atlas at this point, a script must be used to do so via Late Binding. code show as below:

using UnityEngine;
using UnityEngine.U2D; //引用SpriteAtlas的命名空间!
public class SpriteAtlasTest : MonoBehaviour 
{
    //如果勾选了Include Build属性,在启动游戏时自动进行以下操作
    //SpriteAtlasManager是在运行期间管理SpriteAtlas的一个类,它包含atlasRequested和atlasRegisterer两个事件
    //atlasRequested事件:当一个Sprite打包进了图集,但是在运行期间无法找到该图集的位置时触发。
    //atlasRegistered事件:SpriteAtlas加载完成后调用

    private void OnEnable()
    {
        SpriteAtlasManager.atlasRegistered += AtlasRegistered;
        SpriteAtlasManager.atlasRequested += AtlasRequest;
    }

    private void OnDisable()
    {
        SpriteAtlasManager.atlasRegistered -= AtlasRegistered;
        SpriteAtlasManager.atlasRequested -= AtlasRequest;
    }

    private void AtlasRegistered(SpriteAtlas spriteAtlas)
    {
        Debug.LogFormat("Registered {0}.", spriteAtlas.name);
        Sprite sprite = spriteAtlas.GetSprite("AnimPic_0");
        Debug.Log("从图集中获取到的Sprite:" + sprite.name);
    }

    private void AtlasRequest(string tag, Action<SpriteAtlas> callback)
    {
        var sa = Resources.Load<SpriteAtlas>(tag);
        callback(sa);
    }
}
  • Allow Rotation

        Rotate the sprite sprite when running the package, so as to maximize the density of the sprite in the atlas.

        This option is checked by default. If it is a UI element in the canvas, it is recommended to turn off this option, because the rotated Sprite in the atlas will also be rotated in the scene.

  • Tight Packing

        Pack according to the sprite's outline rather than the outline of the sprite's outer rectangle. This increases the density of the sprites in the atlas.

        After checking, use the cropping atlas image with higher precision (Mesh cropping), otherwise it is matrix cropping (the picture is a rectangular image).

        If you do not need high-precision cropping, uncheck it, and if you find that the cropped image is not displayed correctly, check it.

  • Padding

        Define the pixel spacing between Sprites in the atlas to prevent overlapping between Sprites. The default value is 4.

3.Variant (variant)

  • Master Atlas

        Sets a copy of the main atlas content as own content.

  • Include in Build
  • Scale

        Set the zoom factor of the variant atlas, ranging from 0.1 to 1.

4.Texture

  • Read/Write Enabled

        Whether to use script functions to access texture data, such as the Texture2D.SetPixels function. If enabled, Unity creates a copy of the texture, which doubles the memory required, and only works with uncompressed or DXT textures.

  • Generate Mip Maps

        Whether to generate MipMaps.

  • sRGB

        Whether to store textures in gamma space.

  • Filter Mode

        Sets the filtering mode for textures in the atlas.

  • Show Platform Settings For

5.Objects for Pakcing

        Add Sprite, pack all Sprites in the list into the atlas. Can be placed in Texture2D, Sprite, folders. When putting into the folder, all Texture2D and Sprite under the folder will be put into the atlas.

6.Pack Preview button

        After clicking, the specific values ​​of the atlas will be displayed on the preview interface, such as the internal performance, space size, size, and compression format of the atlas.

        Note: In Sprite Atlas V2, if you modify the atlas, click this button to save the changes.

Highlights

        The main purpose of variant atlases is to create atlases of different resolutions than the main atlas. The resolution of the Sprite in the main atlas * Scale (scaling factor) is the result of the resolution of the Sprite in the variant atlas, which does not contain the Objects for Packing attribute, so the content in the variant atlas is a copy of the main atlas.

        When there are both a main atlas and a variant atlas of the main atlas in the project, you can use the sprite of either of these two atlases. If you want to automatically load sprites from the variant atlas instead of the main atlas, enable the Include in Build option only for the variant atlas and turn off this option for the main atlas.

Precautions

        When a Sprite is active in the atlas, Unity will load all the textures in the atlas to which the Sprite belongs. If there are many textures in an atlas, but only one texture is referenced in the scene, the entire atlas will be loaded, which will cause large memory consumption.

        The solution to this problem: (may increase DrawCall)

  • Pack all or most textures active in the scene into the same atlas;
  •  Textures are packaged separately into multiple smaller atlases based on their common usage.

おすすめ

転載: blog.csdn.net/yokixiang/article/details/127970560