Unity Atlas

Talk about Unity Atlas OvO

Atlas It is a resource that combines multiple textures into a combined texture. Unity can call this single texture to issue a single draw call instead of issuing multiple draw calls, enabling one-time access to the compressed texture with a small performance overhead

Specifically, if multiple textures are merged into one atlas, when it comes to normal drawing of these textures, DrawCall only needs to be called once, which facilitates rendering batching and reduces rendering consumption.

Unity has two ways to create an atlas without using third-party plug-ins.

1.Sprite Packer (deprecated in Unity2020.1 and later versions)

First, you need to find the Sprite Packer column in Edit>Project Settings>Editor and set it to Always Enabled (Legacy Sprite Packer). Set the
Insert image description here
Packing Tag of the image to classify it into different atlases.
Insert image description here
Then go to Window>2D> Sprite Packer opens the Sprite Packer window and click Pack to create the album.

2.Sprite Atlas

You also need to find the Sprite Packer column in Edit>Project Settings>Editor and set it to Always Enabled to
Insert image description here
create a Sprite Atlas. Specify the texture or folder in Objects for Packing to draw the atlas.
Insert image description here
The new version of SpriteAtlas is compared to the previous SpritePacker. More optimizations have been made, such as being able to view the size of the atlas in real time, the arrangement and layout of the elements in the atlas, and adding features such as LateBinding (that is, unchecking include in build will untie the dependency between Prefab and Atlas).

Compared with the old version of Sprite Packer, when using Sprite Atlas, each atlas is managed with a separate SpriteAtlas. The format of the atlas is also defined in this resource. To preview a single atlas, you don’t need to put it in the file as in the old version. All atlases are previewed in a SpritePacker editor window, but can be visually previewed in the Inspector window in real time. Sprite Packer's album management method is inconvenient and very stuck when there are a large number of albums. The new version's operation method is a divide and conquer concept, which is more convenient and faster.

Note : Images placed in the Resources folder will not be packaged into the atlas. The packaged atlas will be placed in Project/Library/AtlasCache.

3. Atlas organization strategy and resource optimization

Key points for organizing the atlas

  • The atlas should be as compact as possible, and the atlas itself should not be too large or too small.
  • Small pictures on the same interface should be in the same atlas as much as possible to reduce DrawCall and prevent the situation where one interface needs to be updated when more atlases are updated.
  • Pictures referenced by many interfaces are best managed under a common picture collection.

It should be noted that the size of the atlas needs to be 2 to the nth power. This is because when OpenGL loads image textures, the memory used will automatically expand to 2 to the nth power. This automatic conversion process is relatively slow, so the resources are converted into Standard images to increase conversion speed

Texture optimization
The purpose of texture optimization is to make the memory they occupy as small as possible. After the texture is loaded into the memory, the size calculation formula is as follows:

Texture memory size (bytes) = texture width x texture height x pixel bytes
pixel bytes = number of pixel channels (R/G/B/A) x channel size (1 byte/nibble)

Optimization direction:

  • Adjust texture size:
    Reduce the texture size to a suitable size based on the actual situation of the project. The appropriate size here refers to the maximum size that the rendering object cannot reach in most cases in the picture. It is best to keep this size to the Nth power of 2.
  • Adjust the texture compression method:
    In order to make the texture take up as little memory as possible when running on the phone, you need to set the compression format of the texture. Currently, the main compression formats supported by Unity are: ETC/ETC2 on android, PVRTC on iOS, and in the future ASTC may be used. These compression formats have their own characteristics:
    ETC: does not support transparent channels and is supported by all android devices.
    ETC2: Supports transparent channels. The GPU of Android devices must support OpenGL es 3.0 before it can be used. For devices that do not support it, it will be stored in the memory in uncompressed form and occupy more memory.
    PVRTC: Can be used on all Apple devices. The length and width of the compressed texture are required to be equal and to the power of 2 (POT, Power of 2).
    ASTC: high quality and low memory usage, a compression format that may be commonly used in the future. Currently, some models do not support it.
  • Improve texture reuse rate:
    Create a shared library. Put common elements into shared libraries, such as buttons/progress bars/backgrounds/UI common elements, etc.
    Replace large background images with nine-square grid images. Jiugongge is a relatively common UI component in game development.
    Texture elements can be combined into composite textures through transformations. For example, in the picture below, a symmetrical background image can be obtained by rotating/flipping four identical texture instances.

4. The album is packaged as AB

It should be noted that for UGUI atlases, whether old or new, when building an AssetBundle, all graphics elements in the same atlas must be placed in the same AssetBundle. Otherwise, if the graphics elements of the same atlas are scattered to multiple AssetBundle, then each AssetBundle will contain a copy of this atlas. The final result will be problems such as package redundancy, memory expansion, and loading time. When using Sprite Atlas, you can check include in build and add The atlas is also packaged into an AB package to solve this problem.

reference:

[Unity Game Development] The best consumption plan of SpriteAtlas and AssetBundle
[Memory Optimization] Atlas organization strategy
Unity Atlas Atlas usage summary
Unity separation map alpha channel practice

Guess you like

Origin blog.csdn.net/weixin_44276280/article/details/130436213