Using Unity Sprite Atlas

1. Why use Sprite Atlas.

        Sprite Atlas has comprehensively improved the performance and usability of the existing atlas packaging system Sprite Packer. In addition, compared to Sprite Packer, Sprite Atlas gives more control over the sprites to the user. The user controls the packaging process and loading timing of the atlas, which is more conducive to the control of system performance.

       When the Sprite used on the UI is entered into the atlas, the draw call of the game will be automatically reduced. For static sprites, it is only necessary to create an atlas and put the Sprite, Sliced ​​Sprite, and folders into the atlas to achieve the effect. Sprite Atlas mainly implements the following three functions.

        a. Create and edit atlas and set atlas parameters

        b. Add atlas Variant (variant)

        c. Runtime access to the atlas

2. Use Sprite Atlas.

        a. Open the Sprite Packe settings

Click Edit-->Project Settings-->Editor-->Sprite Packer in the Unity menu

Set to Enable For Builds or Always Enabled.

b. Create and edit atlas

In order to make sure that Sprite Atlas can reduce Draw Call, first check the Batches that Unity used before, and you can see that the current Batch is 11.

Now the pictures used in our current interface are placed in Speite Atlas.

Click Assets-->Create-->Sprite Atlas-->Sprite in the Unity menu

I named it All_Texture_gt_Test

Click All_Texture_gt_Test to put the picture that needs to be packed into its Object for Packing. Click Pack Preview to see the generated atlas.

Run the project again to view the Batches of Unity statistics

At this time, we can see that the number of Batches counted by Unity is 7, which is indeed less than before.

c. The principle of Sprite Atlas

        Since I have been using TexturePackerGUI before, the method of use is that TexturePackerGUI will pack all the scattered images into an atlas of the nth power of 2. To import this large atlas in the project, no scattered images are needed, and then Divide the atlas into sprites and use them in the UI.

So what is the principle of Sprite Atlas?

When the scatter map we use is packaged into Sprite Atlas, the picture pointed to in the UI editing interface of Unity is still a scatter map. When the program is running, the atlas printed by Sprite Atlas is actually called (it can be verified by reducing Batche at this point).

After the package is released, there will only be atlases printed by Sprite Atlas in the resource list in the package, and the scattered pictures will not be printed in the package.

In order to verify this, I deliberately typed two packages:

Package 1: A package that does not use Sprite Atlas

Package 2: There is a package using Sprite Atlas

Modify the suffix of these two packages to zip, find their resource file data.unity3d, as shown below

Then use AssetStudioGUI to view the resources in these two packages

For the convenience of comparison, I first post the resources in Unity

Pack 1 resource or scatter map

The resources in package 2 are only a complete atlas

This means: After Unity uses Sprite Atlas to export the package, only the atlas printed by Sprite Atlas will be included in the resource list in the package, and the scattered pictures will not be packaged.

Well, the principle is clear. Since the actual operation is to call resources in the atlas, what should I do when I need to dynamically call these resources through code? At this time, we need to dynamically access Sprite Atlas resources. The following is the access code.

SpriteAtlas atlas =Resources.Load<SpriteAtlas>("Texture/All_Texture_gt_Test");

Sprite sprite = atlas.GetSprite("Project Entertainment");

if (sprite != null)

{

    _Icon_5.sprite = sprite;    

} 

Guess you like

Origin blog.csdn.net/gtncwy/article/details/118735792