Version: 2022.2
变体精灵图集
准备要分发的精灵图集

精灵图集工作流程

按照以下步骤创建精灵图集:

  1. 创建精灵图集资源。
  2. 选择一组要打包到精灵图集中的对象 (Objects for Packing)。
  3. 在精灵图集上启用 Include in Build 以便将精灵图集包含在项目中。
  4. (可选)创建变体精灵图集
  5. (可选)准备要分发的精灵图集。
    • 准备要分发的精灵需要使用脚本通过后期绑定进行加载。
  6. (可选)优化项目中的精灵图集的使用和大小以提高性能。

创建精灵图集资源

To create the Sprite Atlas Asset, go to Assets > Create > 2D > Sprite Atlas. Unity creates the Sprite Atlas in the Asset folder, with the file extension * .spriteatlas.

选择要添加到“Objects for Packing”列表的项

The Objects for Packing list displays the currently selected items that are in the Sprite Atlas. You can place Texture2D and Sprite Assets in this list. Drag and drop a folder directly onto the list to automatically add all valid content within the folder to the list (only the folder appears on the list).

要选择列表的特定项,请执行以下操作:

  1. 选择精灵图集资源,并在其 Inspector 窗口中找到 Objects For Packing 列表。此列表显示 Unity 在构建精灵图集时要打包的一组项。

  2. To add items, select the plus (+) icon at the lower right of the list to create a new row. Drag a valid Asset onto an empty row to add it to the list, or select the circle icon to the right of the row to open the Select Object window. Select an Asset from the Object Picker window to add it to the list:

    To remove an item from the list, click the minus (-) icon at the lower right of the Objects For Packing list. To replace an item, drag another item to the row it occupies to replace it on the list.

  3. Select Pack Preview to preview the combined Atlas Texture in the Asset Preview window.

Include in Build

All Sprite Atlases have Include in Build enabled by default when created. This means that Unity includes the Sprite Atlases with the Project’s build and automatically loads them at the build’s run time. Enable or disable the ‘Include in Build’ property to control which Sprite Atlases are included in the Project build.

取消选中“Include in Build”会禁用此行为,并在准备要分发的精灵图集时完成。

Optimizing Sprite Atlas usage and size for improved performance

当精灵在场景中处于活动状态时,Unity 会加载该精灵所属的精灵图集以及该精灵包含的所有纹理。如果 Unity 加载具有巨大纹理的精灵图集,而场景中没有任何对象使用大多数的这些纹理时,这样做会导致过高的性能开销。

为了优化精灵图集的使用,理想情况下,场景中激活的所有或大多数精灵都应属于同一个图集。最好根据精灵纹理的常见用途将精灵纹理分割成多个较小的图集。

降低性能开销的另一种方法是减少精灵图集中打包纹理之间的空白空间。此做法可减小精灵图集的大小。要执行此操作,请选择精灵图集,并检查其 Inspector 设置底部的 Pack Preview 窗口中打包的图集纹理。如果没有可用的预览,请在 Objects for Packing 列表下选择 Pack Preview 按钮以生成打包的纹理。

Sprite Atlas with excess empty space.
Sprite Atlas with excess empty space.

如果可见的空白空间过多,可以手动减小打包纹理的大小,从而减少空白空间量并优化图集的大小。为此,请前往 Inspector 窗口底部的 Platform-specific overrides 面板。从 Max Texture Size 设置的下拉菜单中选择一个较小的值,然后选择 Pack Preview 以重新生成打包的纹理。

Set the Max Texture Size.
Set the Max Texture Size.

Max Texture Size 值小于精灵图集纹理的当前尺寸时,Unity 会减小打包纹理尺寸以尽可能与设置的 Max Texture Size 匹配,并自动修剪掉任何多余的空白空间。如果选择的一些精灵纹理超过精灵图集的 Max Texture Size 设置,则精灵图集将忽略 Max Texture Size 设置,并保持为了以原始尺寸包含精灵纹理而需要的最小尺寸。

Textures in a Sprite Atlas remain in their original dimensions.
Textures in a Sprite Atlas remain in their original dimensions.

Note: When using Variant Sprite Atlases, selecting a very low scale value (less than 0.25) may result in visual artifacts, depending on the compression format used and original resolution of the Sprite. It is recommended to use high padding values and better compression formats when using Variant Atlases.

变体精灵图集
准备要分发的精灵图集