manage-sprite-atlas

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Unity SpriteAtlas V2

Unity SpriteAtlas V2

Provides editor-safe procedural knowledge for scripting atlases in Unity projects. V2 enforces strict separation between editor authoring and runtime access.
⚠️ Critical V2 Principle:
SpriteAtlas
is runtime-only.
SpriteAtlasAsset
is editor-only. Never mix contexts. Always use V2.
提供适用于Unity项目中图集脚本开发的编辑器安全的流程化知识。V2版本严格区分编辑器创作与运行时访问。
⚠️ V2核心原则:
SpriteAtlas
仅用于运行时。
SpriteAtlasAsset
仅用于编辑器。切勿混用上下文。请始终使用V2版本。

🚨 CRITICAL: Required Checks and User Inputs Before Implementation

🚨 关键:实现前的必要检查与用户输入确认

ALWAYS perform these checks and ask these questions BEFORE generating any code:
在生成任何代码之前,务必执行以下检查并询问用户以下问题:

Read the shipped resources before writing code (REQUIRED)

编写代码前请阅读随附资源(必填)

This skill ships working C# under
resources/
, and the atlas code that goes wrong is almost always code written without reading it first. Open the files for the path you are taking, then write.
Taking this pathRead first
Any atlas work at allresources/authoringvsruntime.cs, references/common-errors.md
Prebuild generation (the default)resources/spriteatlasprebuildgenerator.cs, resources/enablespritepacking.cs, resources/savespriteatlasasset.cs
Option B, Addressables late-bindingresources/buildaddressablespostprocess.cs, resources/spriteatlaslatebinding.cs, resources/handlelatebinding.cs
Anything that might use an older APIresources/deprecatedmethods.cs, resources/dontscriptspriteatlasineditor.cs, resources/dontpackinruntimebuilds.cs
resources/
holds 38 files in all, covering custom packers, variants, platform settings, and runtime access. Browse the directory when your task is not in the table above rather than inventing an approach. Reaching for the API from memory instead of reading these is the single most common cause of an atlas that imports cleanly and then does nothing at runtime.
本技能在
resources/
目录下提供了可运行的C#代码,绝大多数图集代码出错都是因为没有提前阅读这些代码。请先打开你所选方案对应的文件,再开始编写代码。
采用以下方案请先阅读
任何图集相关工作resources/authoringvsruntime.cs、references/common-errors.md
预构建生成(默认方案)resources/spriteatlasprebuildgenerator.cs、resources/enablespritepacking.cs、resources/savespriteatlasasset.cs
选项B:Addressables延迟绑定resources/buildaddressablespostprocess.cs、resources/spriteatlaslatebinding.cs、resources/handlelatebinding.cs
可能使用旧版API的场景resources/deprecatedmethods.cs、resources/dontscriptspriteatlasineditor.cs、resources/dontpackinruntimebuilds.cs
resources/
目录下共有38个文件,涵盖自定义打包器、变体图集、平台设置和运行时访问等内容。如果你的任务不在上表中,请浏览该目录,而不是自行构思方案。不阅读这些资源仅凭记忆调用API,是导致图集导入正常但运行时完全失效的最常见原因。

0. Check for Existing Scripts (REQUIRED FIRST STEP)

0. 检查现有脚本(必填的第一步)

BEFORE generating any code, scan the project for existing SpriteAtlas scripts generated by this skill.
Search for files containing the identifier:
// [UNITY-SKILL:SPRITEATLAS]
If existing scripts are found, ALWAYS ask the user with this format:
"I found existing SpriteAtlas scripts in your project:
Prebuild Generator:
  • Assets/Editor/SpriteAtlas/SpriteAtlasPrebuildGenerator.cs
Addressables Builder:
  • Assets/Editor/SpriteAtlas/BuildAddressablesPostprocess.cs
Runtime Loader:
  • Assets/Scripts/SpriteAtlas/SpriteAtlasLateBinding.cs
What would you like to do?"
Then present options:
  • Option A: Update existing scripts (Recommended if requirements changed)
    • Regenerates scripts at existing paths
    • Preserves file locations
    • Updates to latest version
    • ⚠️ May overwrite custom modifications
  • Option B: Create new scripts with different names
    • Generates alongside existing scripts
    • Allows multiple atlas configurations
    • Original scripts remain unchanged
    • You'll need to specify new names/paths
  • Option C: Abort (keep existing unchanged)
    • No code generation
    • No changes to project
    • Use this if you want to keep current setup
User Choice Handling:
  • If A chosen: Regenerate at existing paths, increment version to 2.0.1+
  • If B chosen: Ask for new script names (e.g., "SpriteAtlasPrebuild_Custom.cs"), then generate
  • If C chosen: Stop immediately, inform user no changes were made
在生成任何代码之前,请扫描项目中是否存在本技能生成的SpriteAtlas脚本。
搜索包含以下标识符的文件:
// [UNITY-SKILL:SPRITEATLAS]
如果找到现有脚本,请务必按照以下格式询问用户:
"我在你的项目中找到了已有的SpriteAtlas脚本:
预构建生成器:
  • Assets/Editor/SpriteAtlas/SpriteAtlasPrebuildGenerator.cs
Addressables构建器:
  • Assets/Editor/SpriteAtlas/BuildAddressablesPostprocess.cs
运行时加载器:
  • Assets/Scripts/SpriteAtlas/SpriteAtlasLateBinding.cs
你希望如何处理?"
然后提供以下选项:
  • 选项A:更新现有脚本(需求变更时推荐)
    • 在原有路径重新生成脚本
    • 保留文件位置不变
    • 更新至最新版本
    • ⚠️ 可能会覆盖自定义修改
  • 选项B:以不同名称创建新脚本
    • 在现有脚本旁生成新脚本
    • 支持多种图集配置
    • 原有脚本保持不变
    • 你需要指定新的名称/路径
  • 选项C:中止操作(保持现有脚本不变)
    • 不生成任何代码
    • 不对项目做任何修改
    • 如果你希望保留当前设置可选择此项
用户选择处理方式:
  • 如果选择A:在原有路径重新生成,版本号升级至2.0.1+
  • 如果选择B:询问新的脚本名称(例如"SpriteAtlasPrebuild_Custom.cs"),然后生成
  • 如果选择C:立即停止操作,告知用户未做任何修改

1. Delivery Mechanism (REQUIRED)

1. 交付机制(必填)

Ask: "How do you want to deliver the sprite atlases?"
Option A: Built-in Data (Immediate Loading)
  • Atlases are included in the build and loaded immediately
  • Set
    includeInBuild = true
  • Suitable for: Core UI, main gameplay sprites, always-needed assets
  • Pros: Simple, no additional packages, instant access
  • Cons: Increases initial build size, cannot update without new build
Option B: Late-Binding via Addressables (On-Demand Loading)
  • Atlases are NOT included in build, loaded on-demand via Addressables
  • Set
    includeInBuild = false
  • Create Addressables entries for each atlas
  • Add addressable setup as prebuild step
  • 🚨 REQUIRED: Build Addressables content as build step
  • 🚨 REQUIRED: Create late-binding runtime loader script
  • Suitable for: DLC content, optional features, large assets, downloadable content
  • Pros: Smaller initial build, can update independently, on-demand loading
  • Cons: Requires Addressables package, async loading, network dependency
Use CaseRecommended
Core UI sprites that are always visibleOption A: Built-in
Tutorial or onboarding spritesOption A: Built-in
Level-specific sprites (100+ levels)Option B: Addressables
DLC or seasonal contentOption B: Addressables
Localized UI sprites (multiple languages)Option B: Addressables
Character skins or cosmeticsOption B: Addressables
询问用户:"你希望如何交付精灵图集?"
选项A:内置数据(即时加载)
  • 图集包含在构建产物中,可立即加载
  • 设置
    includeInBuild = true
  • 适用场景:核心UI、主玩法精灵、始终需要的资源
  • 优点:简单,无需额外包,可即时访问
  • 缺点:会增加初始构建体积,无法在不重新构建的情况下更新
选项B:通过Addressables延迟绑定(按需加载)
  • 图集不包含在构建产物中,通过Addressables按需加载
  • 设置
    includeInBuild = false
  • 为每个图集创建Addressables条目
  • 将Addressables设置添加为预构建步骤
  • 🚨 必要条件:将构建Addressables内容作为构建步骤
  • 🚨 必要条件:创建延迟绑定的运行时加载脚本
  • 适用场景:DLC内容、可选功能、大型资源、可下载内容
  • 优点:初始构建体积更小,可独立更新,按需加载
  • 缺点:需要Addressables包,异步加载,依赖网络
使用场景推荐方案
始终显示的核心UI精灵选项A:内置
教程或引导精灵选项A:内置
关卡专属精灵(100+关卡)选项B:Addressables
DLC或季节性内容选项B:Addressables
多语言本地化UI精灵选项B:Addressables
角色皮肤或装饰道具选项B:Addressables

2. SpritePacker Mode (REQUIRED)

2. SpritePacker模式(必填)

Enable Sprite Packer mode before creating atlases, then read the setting back and confirm it took. Use the code in resources/enablespritepacking.cs; it sets
EditorSettings.spritePackerMode
and configures the importer's packing settings.
This is the step that decides whether the atlas you produce is real.
Disabled
is the zero value of
SpritePackerMode
, so any project where nobody has set it carries packing Disabled, and an atlas created while it is Disabled still imports, still shows up as an asset, and still looks finished, but can never pack. Unity says so in the Inspector: "Sprite Atlas packing is disabled". Nothing else in the workflow fails, so an atlas shipped this way reads as a success. Do not assume a project is already configured: read the value.
So do not treat "I set it" as done. After setting it, read
EditorSettings.spritePackerMode
back, confirm it is not
Disabled
, and report the value you actually read. If you cannot read it back, say so rather than assuming the write landed.
DO NOT edit meta files DIRECTLY.
在创建图集之前,请先启用Sprite Packer模式,然后回读该设置并确认其已生效。 请使用resources/enablespritepacking.cs中的代码;它会设置
EditorSettings.spritePackerMode
并配置导入器的打包设置。
这一步决定了你生成的图集是否真正有效。
Disabled
是
SpritePackerMode
的零值,因此任何未设置过该选项的项目,打包状态都是Disabled。在禁用状态下创建的图集仍然可以导入,会显示为资源,看起来也很完整,但永远无法完成打包。Unity会在检查器中提示:"Sprite Atlas packing is disabled"。工作流中的其他步骤都不会报错,因此以这种方式生成的图集看起来是成功的。不要假设项目已经配置好了:一定要回读该值。
因此不要以为"我设置过了"就完成了。设置完成后,回读
EditorSettings.spritePackerMode
,确认它不是
Disabled
,并报告你实际读取到的值。如果无法回读,请明确说明,而不是假设设置已经生效。
请勿直接编辑meta文件。

🚨 CRITICAL: Default Approach is Prebuild Generation

🚨 关键:默认方案为预构建生成

ALWAYS use
IPreprocessBuildWithReport
to automatically generate or update SpriteAtlases during the build pipeline.
This is the DEFAULT and REQUIRED approach unless the user EXPLICITLY requests manual authoring.
请务必使用
IPreprocessBuildWithReport
在构建管线中自动生成或更新SpriteAtlas。
这是默认且必要的方案,除非用户明确要求手动创作。

❌ DO NOT Create Manual Menu Item Scripts

❌ 请勿创建手动菜单项脚本

NEVER create scripts with
[MenuItem]
attributes for atlas generation unless explicitly requested.
The prebuild approach eliminates the need for manual clicks. Only use manual authoring for: Hand-optimized layouts, specific sprite arrangements, or editor preview requirements. See Advanced: Manual Authoring.
除非明确要求,否则切勿为图集生成创建带有
[MenuItem]
特性的脚本。
预构建方案无需手动点击操作。仅在以下情况使用手动创作:手动优化的布局、特定的精灵排列,或编辑器预览需求。参见高级:手动创作。

🚨 CRITICAL: Sprite Source Location Restriction

🚨 关键:精灵源位置限制

ONLY add sprites from the project's Assets folder. NEVER add sprites from Unity built-in assets, packages, or external locations. Unity built-in assets cannot be packed into SpriteAtlas, and package assets may cause import/dependency issues.
仅添加项目Assets文件夹中的精灵。切勿添加Unity内置资源、包资源或外部位置的精灵。 Unity内置资源无法打包到SpriteAtlas中,包资源可能会导致导入或依赖问题。

Critical V2 Architecture

V2核心架构

ContextComponentPurposeAllowed Usage
Editor Authoring
SpriteAtlasAsset
Add/remove sprites/folders; store metadata✅ Editor scripts only
Editor Settings
SpriteAtlasImporter
Configure texture, packing, platform settings✅ Editor scripts only
Editor Packing
SpriteAtlasUtility.PackAtlases()
Optional editor preview packing (not for build)⚠️ Only for preview; atlases auto-pack at build
Runtime
SpriteAtlas
Query packed sprites (read-only)✅ Runtime scripts only
Runtime Loading
SpriteAtlasManager
Dynamic loading callbacks✅ Runtime scripts only
上下文组件用途允许的使用场景
编辑器创作
SpriteAtlasAsset
添加/移除精灵/文件夹;存储元数据✅ 仅编辑器脚本
编辑器设置
SpriteAtlasImporter
配置纹理、打包、平台设置✅ 仅编辑器脚本
编辑器打包
SpriteAtlasUtility.PackAtlases()
可选 编辑器预览打包(不用于构建)⚠️ 仅用于预览;构建时自动打包
运行时
SpriteAtlas
查询已打包的精灵(只读)✅ 仅运行时脚本
运行时加载
SpriteAtlasManager
动态加载回调✅ 仅运行时脚本

Forbidden Cross-Context Usage (Common Error Sources)

禁止的跨上下文使用(常见错误来源)

❌ Invalid Pattern✅ Correct Pattern
new SpriteAtlas()
in editor code
Use
SpriteAtlasAsset
+
SpriteAtlasImporter
AssetDatabase.LoadAssetAtPath<SpriteAtlas>(...)
in editor
Use
SpriteAtlasAsset.Load(...)
SpriteAtlasAsset.GetPackables()
in editor
Use
SpriteAtlas.GetPackables()
Modifying
SpriteAtlas
in editor scripts
Modify
SpriteAtlasAsset
→ reimport → use
SpriteAtlasImporter
Create variants from original packable objects (sprites/folders)Creating variants from a Master runtime
SpriteAtlas
NEVER script against
SpriteAtlas
in editor code
— it is only for runtime use in V2 except for GetPackables.
❌ 错误模式✅ 正确模式
在编辑器代码中使用
new SpriteAtlas()
使用
SpriteAtlasAsset
+
SpriteAtlasImporter
在编辑器中使用
AssetDatabase.LoadAssetAtPath<SpriteAtlas>(...)
使用
SpriteAtlasAsset.Load(...)
在编辑器中使用
SpriteAtlasAsset.GetPackables()
使用
SpriteAtlas.GetPackables()
在编辑器脚本中修改
SpriteAtlas
修改
SpriteAtlasAsset
→ 重新导入 → 使用
SpriteAtlasImporter
从原始可打包对象(精灵/文件夹)创建变体从主运行时
SpriteAtlas
创建变体
切勿在编辑器代码中针对
SpriteAtlas
编写脚本
— 在V2版本中,它仅用于运行时,
GetPackables
实例方法除外。

Core V2 Workflow (Reference Only - Use Prebuild Instead)

V2核心工作流(仅供参考 - 请使用预构建方案)

🚨 IMPORTANT: This workflow is shown for reference only. ALWAYS implement this inside
IPreprocessBuildWithReport.OnPreprocessBuild()
rather than in manual scripts. See Quick Start.
🚨 重要提示:此工作流仅作展示参考。请务必在
IPreprocessBuildWithReport.OnPreprocessBuild()
中实现该逻辑
,而不是在手动脚本中实现。参见快速开始。

Prerequisites

前置条件

  • Unity 6000.3 or later
  • Basic understanding of Unity's asset import pipeline
  • Familiarity with editor scripting for atlas authoring
  • Unity 6000.3或更高版本
  • 对Unity资源导入管线有基本了解
  • 熟悉用于图集创作的编辑器脚本开发

Quick Start: Automated Prebuild Generation (DEFAULT APPROACH)

快速开始:自动化预构建生成(默认方案)

Overview

概述

This is the PRIMARY and DEFAULT way to create SpriteAtlases. Implement
IPreprocessBuildWithReport
to automatically generate or update SpriteAtlases before each build based on categorization rules. No manual menu clicks required.
这是创建SpriteAtlas的主要且默认的方式。 实现
IPreprocessBuildWithReport
接口,在每次构建前根据分类规则自动生成或更新SpriteAtlas,无需手动点击菜单。

Workflow Steps

工作流步骤

Step 1: Ask User for Delivery Mechanism
Before generating code, ask: "How do you want to deliver the sprite atlases: (A) Built-in data or (B) Late-binding via Addressables?"
Step 2: Create Prebuild Script
Create this script in an Editor folder. Customize based on user's delivery choice:
步骤1:询问用户交付机制
在生成代码前,询问用户:"你希望如何交付精灵图集:(A) 内置数据 还是 (B) 通过Addressables延迟绑定?"
步骤2:创建预构建脚本
在Editor文件夹中创建此脚本。根据用户的交付选择进行自定义:

Option A: Built-in Data (Immediate Loading)

选项A:内置数据(即时加载)

Built-in Data (Immediate Loading)
内置数据(即时加载)

Option B: Late-Binding via Addressables (On-Demand Loading)

选项B:通过Addressables延迟绑定(按需加载)

🚨 CRITICAL ENFORCEMENT: When user chooses Addressables, you MUST generate ALL THREE scripts below. Never generate just one or two - all three are required for Addressables delivery to work.
Required Scripts (ALL THREE MANDATORY):
  1. Prebuild script (IPreprocessBuildWithReport) - Generates atlases, creates Addressables entries
  2. Build Addressables script (IPostprocessBuildWithReport) - Builds Addressables content bundles
  3. Late-binding runtime loader (MonoBehaviour) - Handles on-demand loading at runtime
See references/addressables-delivery.md for complete implementation.
🚨 强制要求:当用户选择Addressables时,你必须生成以下全部三个脚本。切勿只生成一两个 — Addressables交付需要这三个脚本全部才能正常工作。
所需脚本(全部三个为必填项):
  1. 预构建脚本(IPreprocessBuildWithReport)- 生成图集,创建Addressables条目
  2. Addressables构建脚本(IPostprocessBuildWithReport)- 构建Addressables内容包
  3. 延迟绑定运行时加载器(MonoBehaviour)- 处理运行时按需加载
完整实现请参见references/addressables-delivery.md。

Complete Workflow

完整工作流

User Request
    ↓
Step 0: Check for existing scripts with [UNITY-SKILL:SPRITEATLAS] identifier
    ↓
    ├─ Found existing scripts?
    │   ↓ YES
    │   Ask user: Update existing / Create new / Abort
    │   ↓
    │   Handle user choice
    │
    └─ NO existing scripts or user chose "Create new"
        ↓
        Ask "Built-in data or Addressables?"
        ↓
        ├─ Option A: Built-in
        │   ↓
        │   Generate 1 script: Prebuild generator (includeInBuild=true)
        │
        └─ Option B: Addressables
            ↓
            Generate 3 scripts:
              1. Prebuild: Generate atlases + create Addressables entries (includeInBuild=false)
              2. Postprocess: Build Addressables bundles
              3. Runtime: Late-binding loader component
Step 3: Customize Categorization Rules
Edit the
OnPreprocessBuild
method to match your project's sprite organization. Choose one or combine multiple strategies:
StrategyWhen to UseImplementation
Folder-basedSprites organized by folder structure
GenerateAtlasByFolder("Assets/Art/UI", "Assets/Atlases/UI.spriteatlasv2")
Naming conventionSprites follow naming patterns
GenerateAtlasByNaming("Assets/Art", "icon_", "Assets/Atlases/Icons.spriteatlasv2")
Asset labelsSprites tagged with labels
Scene-basedSprites used in specific scenesQuery scene references
Step 4: Build Your Project
Atlases are automatically generated/updated during build. No manual menu clicks required. This is why prebuild is the default approach.
For Built-in Data (Option A):
  • Atlases are included in build
  • Ready for immediate use at runtime
For Addressables (Option B) - Additional Required Steps:
You MUST generate THREE scripts (not just one):
  1. Prebuild script (IPreprocessBuildWithReport) - Generates atlases and creates Addressables entries automatically
  2. Build Addressables script (IPostprocessBuildWithReport) - Builds Addressables content bundles
  3. Late-binding runtime loader (MonoBehaviour) - Handles on-demand loading via SpriteAtlasManager
The build process will:
  • Generate atlases (prebuild step)
  • Create addressable entries automatically
  • Build addressables content bundles (postprocess step)
  • At runtime: Late-binding loader automatically loads atlases when sprites are first accessed
用户请求
    ↓
步骤0:检查带有[UNITY-SKILL:SPRITEATLAS]标识符的现有脚本
    ↓
    ├─ 找到现有脚本?
    │   ↓ 是
    │   询问用户:更新现有 / 创建新的 / 中止
    │   ↓
    │   处理用户选择
    │
    └─ 无现有脚本或用户选择"创建新的"
        ↓
        询问"内置数据还是Addressables?"
        ↓
        ├─ 选项A:内置
        │   ↓
        │   生成1个脚本:预构建生成器(includeInBuild=true)
        │
        └─ 选项B:Addressables
            ↓
            生成3个脚本:
              1. 预构建:生成图集 + 创建Addressables条目(includeInBuild=false)
              2. 后处理:构建Addressables包
              3. 运行时:延迟绑定加载器组件
步骤3:自定义分类规则
编辑
OnPreprocessBuild
方法以匹配你项目的精灵组织方式。可选择一种或组合多种策略:
策略使用场景实现方式
基于文件夹按文件夹结构组织的精灵
GenerateAtlasByFolder("Assets/Art/UI", "Assets/Atlases/UI.spriteatlasv2")
命名约定遵循命名模式的精灵
GenerateAtlasByNaming("Assets/Art", "icon_", "Assets/Atlases/Icons.spriteatlasv2")
资源标签带有标签的精灵
基于场景特定场景中使用的精灵查询场景引用
步骤4:构建你的项目
图集在构建过程中自动生成/更新。无需手动点击菜单。 这就是预构建作为默认方案的原因。
对于内置数据(选项A):
  • 图集包含在构建产物中
  • 运行时可立即使用
对于Addressables(选项B)- 额外的必要步骤:
你必须生成三个脚本(而不仅仅是一个):
  1. 预构建脚本(IPreprocessBuildWithReport)- 自动生成图集并创建Addressables条目
  2. Addressables构建脚本(IPostprocessBuildWithReport)- 构建Addressables内容包
  3. 延迟绑定运行时加载器(MonoBehaviour)- 通过SpriteAtlasManager处理按需加载
构建流程将:
  • 生成图集(预构建步骤)
  • 自动创建Addressables条目
  • 构建Addressables内容包(后处理步骤)
  • 运行时:当精灵首次被访问时,延迟绑定加载器自动加载图集

Common Categorization Patterns

常见分类模式

By Folder Structure:
csharp
GenerateAtlasByFolder("Assets/Art/UI/Buttons", "Assets/Atlases/UI_Buttons.spriteatlasv2");
GenerateAtlasByFolder("Assets/Art/UI/Icons", "Assets/Atlases/UI_Icons.spriteatlasv2");
GenerateAtlasByFolder("Assets/Art/Characters/Player", "Assets/Atlases/Player.spriteatlasv2");
By Naming Convention:
csharp
GenerateAtlasByNaming("Assets/Art", "icon_", "Assets/Atlases/Icons.spriteatlasv2");
GenerateAtlasByNaming("Assets/Art", "bg_", "Assets/Atlases/Backgrounds.spriteatlasv2");
按文件夹结构:
csharp
GenerateAtlasByFolder("Assets/Art/UI/Buttons", "Assets/Atlases/UI_Buttons.spriteatlasv2");
GenerateAtlasByFolder("Assets/Art/UI/Icons", "Assets/Atlases/UI_Icons.spriteatlasv2");
GenerateAtlasByFolder("Assets/Art/Characters/Player", "Assets/Atlases/Player.spriteatlasv2");
按命名约定:
csharp
GenerateAtlasByNaming("Assets/Art", "icon_", "Assets/Atlases/Icons.spriteatlasv2");
GenerateAtlasByNaming("Assets/Art", "bg_", "Assets/Atlases/Backgrounds.spriteatlasv2");

Variant Generation in Prebuild

预构建中的变体图集生成

Generate variant atlases for different resolutions.
为不同分辨率生成变体图集。

Advanced: Manual Authoring (NOT Default - Use Only When Explicitly Requested)

高级:手动创作(非默认方案 - 仅在明确要求时使用)

⚠️ WARNING: Manual authoring is NOT the default approach. Only use these patterns when the user EXPLICITLY requests manual control or editor preview during development.
🚨 DEFAULT APPROACH: Use prebuild generation with
IPreprocessBuildWithReport
instead. See Quick Start.
Manual authoring is appropriate ONLY for:
  • Hand-optimized sprite layouts where exact positioning matters
  • Custom sprite ordering requirements
  • Editor preview during authoring workflow
  • Explicitly requested by user
DO NOT use manual authoring when:
  • User asks to "create sprite atlas" (use prebuild)
  • User asks to "optimize sprites" (use prebuild)
  • User asks for automated workflow (use prebuild)
  • No specific manual control requirement mentioned
For complete manual authoring patterns including master atlas creation, variant creation, and runtime loading, see references/manual-authoring.md.
⚠️ 警告:手动创作不是默认方案。仅当用户明确要求手动控制或开发过程中的编辑器预览时,才使用这些模式。
🚨 默认方案:请改用基于
IPreprocessBuildWithReport
的预构建生成。参见快速开始。
手动创作仅适用于以下场景:
  • 需要精确定位的手动优化精灵布局
  • 自定义精灵排序要求
  • 创作工作流中的编辑器预览
  • 用户明确要求
以下场景请勿使用手动创作:
  • 用户要求"创建精灵图集"(使用预构建)
  • 用户要求"优化精灵"(使用预构建)
  • 用户要求自动化工作流(使用预构建)
  • 未提及具体的手动控制需求
完整的手动创作模式(包括主图集创建、变体创建和运行时加载)请参见references/manual-authoring.md。

Key Requirements (V2-Specific)

关键要求(V2专属)

  1. 🚨 ALWAYS check for existing scripts FIRST: Before generating code, scan for scripts with
    [UNITY-SKILL:SPRITEATLAS]
    identifier and prompt user to update or create new
  2. 🚨 ALWAYS tag generated scripts: Include skill identifier at the top of every generated script for future detection
  3. 🚨 ALWAYS ask for delivery mechanism: Ask user "Built-in data or Addressables?" before generating code
  4. 🚨 ALWAYS enable Sprite Packer mode: Set
    EditorSettings.spritePackerMode = SpritePackerMode.SpriteAtlasV2
    in prebuild script
  5. 🚨 ALWAYS use prebuild generation: Implement
    IPreprocessBuildWithReport
    as the DEFAULT approach for creating atlases
  6. 🚨 ONLY add sprites from Assets/ folder: Never add sprites from Packages, built-in assets, or external locations
  7. 🚨 For Addressables delivery, ALWAYS generate THREE scripts:
    • Prebuild atlas generator with Addressables setup
    • IPostprocessBuildWithReport
      to build Addressables content
    • Late-binding runtime loader script (extends
      SpriteAtlasManager
      )
  8. Set includeInBuild correctly:
    true
    for built-in data,
    false
    for Addressables
  9. Two-Step Editor Pattern: Create with
    SpriteAtlasAsset
    → Save → Import → Configure via
    SpriteAtlasImporter
    → SaveAndReimport()
  10. Never use
    SpriteAtlas
    in editor code
    — except
    SpriteAtlas.GetPackables()
    instance method on a loaded runtime atlas
  11. Variants reference master: Use
    SetMasterAtlas(SpriteAtlas)
    with runtime instance loaded via
    AssetDatabase.LoadAssetAtPath<SpriteAtlas>()
  12. Platform format assignment: Use
    format = TextureImporterFormat.ASTC_6x6
    directly (no cast)
  13. File extension: V2 atlases use
    .spriteatlasv2
    (not
    .spriteatlas
    )
  14. SpriteAtlasUtility.PackAtlases()
    is optional
    : Only for editor preview; build-time packing is automatic
For common errors and invalid patterns, see references/common-errors.md.
  1. 🚨 务必首先检查现有脚本:生成代码前,扫描带有
    [UNITY-SKILL:SPRITEATLAS]
    标识符的脚本,并提示用户选择更新或创建新脚本
  2. 🚨 务必为生成的脚本添加标签:在每个生成的脚本顶部包含技能标识符,以便后续检测
  3. 🚨 务必询问交付机制:生成代码前,询问用户"内置数据还是Addressables?"
  4. 🚨 务必启用Sprite Packer模式:在预构建脚本中设置
    EditorSettings.spritePackerMode = SpritePackerMode.SpriteAtlasV2
  5. 🚨 务必使用预构建生成:将实现
    IPreprocessBuildWithReport
    作为创建图集的默认方案
  6. 🚨 仅添加Assets/文件夹中的精灵:切勿添加来自Packages、内置资源或外部位置的精灵
  7. 🚨 对于Addressables交付,务必生成三个脚本:
    • 带有Addressables设置的预构建图集生成器
    • 用于构建Addressables内容的
      IPostprocessBuildWithReport
    • 延迟绑定运行时加载器脚本(继承自
      SpriteAtlasManager
      )
  8. 正确设置includeInBuild:内置数据设为
    true
    ,Addressables设为
    false
  9. 两步编辑器模式:使用
    SpriteAtlasAsset
    创建 → 保存 → 导入 → 通过
    SpriteAtlasImporter
    配置 → SaveAndReimport()
  10. 切勿在编辑器代码中使用
    SpriteAtlas
    — 已加载的运行时图集上的
    SpriteAtlas.GetPackables()
    实例方法除外
  11. 变体引用主图集:对通过
    AssetDatabase.LoadAssetAtPath<SpriteAtlas>()
    加载的运行时实例使用
    SetMasterAtlas(SpriteAtlas)
  12. 平台格式赋值:直接使用
    format = TextureImporterFormat.ASTC_6x6
    (无需强制转换)
  13. 文件扩展名:V2图集使用
    .spriteatlasv2
    (而非
    .spriteatlas
    )
  14. SpriteAtlasUtility.PackAtlases()
    是可选的
    :仅用于编辑器预览;构建时打包是自动进行的
常见错误和无效模式请参见references/common-errors.md。

Detailed Reference

详细参考

  • Addressables Delivery: references/addressables-delivery.md - Complete late-binding setup with Addressables
  • Manual Authoring: references/manual-authoring.md - Manual patterns (NOT default - use only when explicitly requested)
  • Common Errors: references/common-errors.md - Invalid patterns and API corrections
  • API Reference: references/api.md - Complete V2 class documentation
  • Custom Packing: references/custom-packing.md -
    ScriptablePacker
    implementation
  • Best Practices: references/best-practices.md - Optimization and common pitfalls
  • Addressables交付:references/addressables-delivery.md - 完整的Addressables延迟绑定设置
  • 手动创作:references/manual-authoring.md - 手动模式(非默认 - 仅在明确要求时使用)
  • 常见错误:references/common-errors.md - 无效模式与API修正
  • API参考:references/api.md - 完整的V2类文档
  • 自定义打包:references/custom-packing.md -
    ScriptablePacker
    实现
  • 最佳实践:references/best-practices.md - 优化与常见陷阱

Namespaces

命名空间

Editor Scripts:
csharp
using UnityEditor;            // AssetImporter, AssetDatabase
using UnityEditor.U2D;        // SpriteAtlasAsset, SpriteAtlasImporter, SpriteAtlasUtility
using UnityEngine;            // Runtime types (e.g., TextureImporterFormat)
using UnityEngine.U2D;        // SpriteAtlas
Runtime Scripts:
csharp
using UnityEngine;            // Core Unity types
using UnityEngine.U2D;        // SpriteAtlas, SpriteAtlasManager
编辑器脚本:
csharp
using UnityEditor;            // AssetImporter, AssetDatabase
using UnityEditor.U2D;        // SpriteAtlasAsset, SpriteAtlasImporter, SpriteAtlasUtility
using UnityEngine;            // Runtime types (e.g., TextureImporterFormat)
using UnityEngine.U2D;        // SpriteAtlas
运行时脚本:
csharp
using UnityEngine;            // Core Unity types
using UnityEngine.U2D;        // SpriteAtlas, SpriteAtlasManager