Unity3D performance optimization: the use of Sprite Atlas and its advantages and disadvantages

Table of contents

Gallery introduction

principle:

shortcoming:

Instructions: 

1. Packaging album

Packages and related settings

SpriteAtlas panel explained 

2. Dynamically load and use in code 


Gallery introduction

principle:

Merge multiple scattered small pictures into one large picture to reduce DrallCall and improve performance

Before using the gallery

 

After using the gallery

Batches dropped from 5 to 2, you can see that all 4 pictures are batched together 

shortcoming:

When using the atlas, the entire atlas will be loaded into the memory, so the picture materials that are frequently needed to be displayed should be put into the same atlas, and those that are not frequently used should also be put into the same atlas, even if the picture is not If it needs to be displayed, it will also be loaded into the memory summary. At the same time, the size of the atlas is fixed at POT (Power of Two). If the size difference between the elements in the atlas is too large, it will also lead to a waste of space.


Instructions: 

1. Packaging album

Packages and related settings

Make sure you have the 2D package installed, otherwise the Sprite Atlas cannot be created

 Select Sprite Atlas Always Enabled in ProjectSettings->Editor->Sprite Packer Mode

Other option meanings:

1.Sprite Atlas V1/V2:Atlas type, both V1 and V2 are acceptable

2.Enable For Builds:After the game is released, the reference of the image is adjusted to the element in the atlas, and the original image is still used in the editor, so Batches in Stats will not Change

3.Always Enabled:After the game is released or after entering PlayMode in the editor mode, the picture is referenced as an element in the atlas, and you can see that the Batches decrease in Stats after running

 Right-click in the Project window to create a Sprite Atlas

Select the created album and put the pictures that need to be packed into Objects for Packing 

SpriteAtlas panel key settings explained 

Include in Build:Whether to be built into the game after it is released

Allow Rotation:Whether to allow image rotation. If checked, the corresponding image may be rotated when building the atlas.It is recommended to disable< /span>

Tight Packing:Select to pack sprites based on the outline of the image instead of the default rectangular outline, so that the images are arranged more closely.It is recommended to disable< /span>, otherwise the UI element may overlap with other images when displayed, because the corresponding Sprite is obtained according to the rectangular outline

Padding:The interval between different UI elements, in pixels, to avoid display problems caused by images being too close to each other

Objects for Packing: The UI elements that need to be packed can be placed in a folder or a single picture. Putting a folder can pack all the pictures in the folder into this album. The pictures The format needs to be set to Sprite (2D and UI). The maximum size of an atlas is 2048*2048

Click Pack Preview to preview, if it does not appear, drag the window below 

2. Dynamically load and use in code 

SpriteAtlas atlas = Resources.Load<SpriteAtlas>("UIPass");
Images[0].sprite = atlas.GetSprite("Icon_ImageIcon_GemGold_0");
Images[1].sprite = atlas.GetSprite("Icon_ImageIcon_Gift_Blue");
Images[2].sprite = atlas.GetSprite("Icon_ImageIcon_Glove");
Images[3].sprite = atlas.GetSprite("Icon_ImageIcon_Pass01");

Requires using UnityEngine.U2D;

You can use Resources or AssetBundle to load, the type is SpriteAtlas

The GetSprite method passes in the corresponding Sprite name, which is the file name when packaging. If the Sprite mode is Single, directly pass in the name of the image. If it is Multiple, it is required Add an underscore and the corresponding index

 

Guess you like

Origin blog.csdn.net/m0_72922928/article/details/134442451