manage-sprite-atlas
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseUnity 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:is runtime-only.SpriteAtlasis editor-only. Never mix contexts. Always use V2.SpriteAtlasAsset
提供适用于Unity项目中图集脚本开发的编辑器安全的流程化知识。V2版本严格区分编辑器创作与运行时访问。
⚠️ V2核心原则:仅用于运行时。SpriteAtlas仅用于编辑器。切勿混用上下文。请始终使用V2版本。SpriteAtlasAsset
🚨 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 , 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.
resources/| Taking this path | Read first |
|---|---|
| Any atlas work at all | resources/authoringvsruntime.cs, references/common-errors.md |
| Prebuild generation (the default) | resources/spriteatlasprebuildgenerator.cs, resources/enablespritepacking.cs, resources/savespriteatlasasset.cs |
| Option B, Addressables late-binding | resources/buildaddressablespostprocess.cs, resources/spriteatlaslatebinding.cs, resources/handlelatebinding.cs |
| Anything that might use an older API | resources/deprecatedmethods.cs, resources/dontscriptspriteatlasineditor.cs, resources/dontpackinruntimebuilds.cs |
resources/本技能在目录下提供了可运行的C#代码,绝大多数图集代码出错都是因为没有提前阅读这些代码。请先打开你所选方案对应的文件,再开始编写代码。
resources/| 采用以下方案 | 请先阅读 |
|---|---|
| 任何图集相关工作 | 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/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.csAddressables Builder:
Assets/Editor/SpriteAtlas/BuildAddressablesPostprocess.csRuntime Loader:
Assets/Scripts/SpriteAtlas/SpriteAtlasLateBinding.csWhat 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.csAddressables构建器:
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 Case | Recommended |
|---|---|
| Core UI sprites that are always visible | Option A: Built-in |
| Tutorial or onboarding sprites | Option A: Built-in |
| Level-specific sprites (100+ levels) | Option B: Addressables |
| DLC or seasonal content | Option B: Addressables |
| Localized UI sprites (multiple languages) | Option B: Addressables |
| Character skins or cosmetics | Option 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
and configures the importer's packing settings.
EditorSettings.spritePackerModeThis is the step that decides whether the atlas you produce is real. is the zero value of
, 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.
DisabledSpritePackerModeSo do not treat "I set it" as done. After setting it, read back,
confirm it is not , and report the value you actually read. If you cannot read it back,
say so rather than assuming the write landed.
EditorSettings.spritePackerModeDisabledDO NOT edit meta files DIRECTLY.
在创建图集之前,请先启用Sprite Packer模式,然后回读该设置并确认其已生效。 请使用resources/enablespritepacking.cs中的代码;它会设置并配置导入器的打包设置。
EditorSettings.spritePackerMode这一步决定了你生成的图集是否真正有效。是的零值,因此任何未设置过该选项的项目,打包状态都是Disabled。在禁用状态下创建的图集仍然可以导入,会显示为资源,看起来也很完整,但永远无法完成打包。Unity会在检查器中提示:"Sprite Atlas packing is disabled"。工作流中的其他步骤都不会报错,因此以这种方式生成的图集看起来是成功的。不要假设项目已经配置好了:一定要回读该值。
DisabledSpritePackerMode因此不要以为"我设置过了"就完成了。设置完成后,回读,确认它不是,并报告你实际读取到的值。如果无法回读,请明确说明,而不是假设设置已经生效。
EditorSettings.spritePackerModeDisabled请勿直接编辑meta文件。
🚨 CRITICAL: Default Approach is Prebuild Generation
🚨 关键:默认方案为预构建生成
ALWAYS use 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。 这是默认且必要的方案,除非用户明确要求手动创作。
IPreprocessBuildWithReport❌ DO NOT Create Manual Menu Item Scripts
❌ 请勿创建手动菜单项脚本
NEVER create scripts with 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]除非明确要求,否则切勿为图集生成创建带有特性的脚本。 预构建方案无需手动点击操作。仅在以下情况使用手动创作:手动优化的布局、特定的精灵排列,或编辑器预览需求。参见高级:手动创作。
[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核心架构
| Context | Component | Purpose | Allowed Usage |
|---|---|---|---|
| Editor Authoring | | Add/remove sprites/folders; store metadata | ✅ Editor scripts only |
| Editor Settings | | Configure texture, packing, platform settings | ✅ Editor scripts only |
| Editor Packing | | Optional editor preview packing (not for build) | ⚠️ Only for preview; atlases auto-pack at build |
| Runtime | | Query packed sprites (read-only) | ✅ Runtime scripts only |
| Runtime Loading | | Dynamic loading callbacks | ✅ Runtime scripts only |
| 上下文 | 组件 | 用途 | 允许的使用场景 |
|---|---|---|---|
| 编辑器创作 | | 添加/移除精灵/文件夹;存储元数据 | ✅ 仅编辑器脚本 |
| 编辑器设置 | | 配置纹理、打包、平台设置 | ✅ 仅编辑器脚本 |
| 编辑器打包 | | 可选 编辑器预览打包(不用于构建) | ⚠️ 仅用于预览;构建时自动打包 |
| 运行时 | | 查询已打包的精灵(只读) | ✅ 仅运行时脚本 |
| 运行时加载 | | 动态加载回调 | ✅ 仅运行时脚本 |
Forbidden Cross-Context Usage (Common Error Sources)
禁止的跨上下文使用(常见错误来源)
| ❌ Invalid Pattern | ✅ Correct Pattern |
|---|---|
| Use |
| Use |
| Use |
Modifying | Modify |
| Create variants from original packable objects (sprites/folders) | Creating variants from a Master runtime |
NEVER script against in editor code — it is only for runtime use in V2 except for GetPackables.
SpriteAtlas| ❌ 错误模式 | ✅ 正确模式 |
|---|---|
在编辑器代码中使用 | 使用 |
在编辑器中使用 | 使用 |
在编辑器中使用 | 使用 |
在编辑器脚本中修改 | 修改 |
| 从原始可打包对象(精灵/文件夹)创建变体 | 从主运行时 |
切勿在编辑器代码中针对编写脚本 — 在V2版本中,它仅用于运行时,实例方法除外。
SpriteAtlasGetPackablesCore V2 Workflow (Reference Only - Use Prebuild Instead)
V2核心工作流(仅供参考 - 请使用预构建方案)
🚨 IMPORTANT: This workflow is shown for reference only. ALWAYS implement this insiderather than in manual scripts. See Quick Start.IPreprocessBuildWithReport.OnPreprocessBuild()
🚨 重要提示:此工作流仅作展示参考。请务必在中实现该逻辑,而不是在手动脚本中实现。参见快速开始。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 to automatically generate or update SpriteAtlases before each build based on categorization rules. No manual menu clicks required.
IPreprocessBuildWithReport这是创建SpriteAtlas的主要且默认的方式。 实现接口,在每次构建前根据分类规则自动生成或更新SpriteAtlas,无需手动点击菜单。
IPreprocessBuildWithReportWorkflow 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):
- Prebuild script (IPreprocessBuildWithReport) - Generates atlases, creates Addressables entries
- Build Addressables script (IPostprocessBuildWithReport) - Builds Addressables content bundles
- Late-binding runtime loader (MonoBehaviour) - Handles on-demand loading at runtime
See references/addressables-delivery.md for complete implementation.
🚨 强制要求:当用户选择Addressables时,你必须生成以下全部三个脚本。切勿只生成一两个 — Addressables交付需要这三个脚本全部才能正常工作。
所需脚本(全部三个为必填项):
- 预构建脚本(IPreprocessBuildWithReport)- 生成图集,创建Addressables条目
- Addressables构建脚本(IPostprocessBuildWithReport)- 构建Addressables内容包
- 延迟绑定运行时加载器(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 componentStep 3: Customize Categorization Rules
Edit the method to match your project's sprite organization. Choose one or combine multiple strategies:
OnPreprocessBuild| Strategy | When to Use | Implementation |
|---|---|---|
| Folder-based | Sprites organized by folder structure | |
| Naming convention | Sprites follow naming patterns | |
| Asset labels | Sprites tagged with labels | |
| Scene-based | Sprites used in specific scenes | Query 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):
- Prebuild script (IPreprocessBuildWithReport) - Generates atlases and creates Addressables entries automatically
- Build Addressables script (IPostprocessBuildWithReport) - Builds Addressables content bundles
- 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| 策略 | 使用场景 | 实现方式 |
|---|---|---|
| 基于文件夹 | 按文件夹结构组织的精灵 | |
| 命名约定 | 遵循命名模式的精灵 | |
| 资源标签 | 带有标签的精灵 | |
| 基于场景 | 特定场景中使用的精灵 | 查询场景引用 |
步骤4:构建你的项目
图集在构建过程中自动生成/更新。无需手动点击菜单。 这就是预构建作为默认方案的原因。
对于内置数据(选项A):
- 图集包含在构建产物中
- 运行时可立即使用
对于Addressables(选项B)- 额外的必要步骤:
你必须生成三个脚本(而不仅仅是一个):
- 预构建脚本(IPreprocessBuildWithReport)- 自动生成图集并创建Addressables条目
- Addressables构建脚本(IPostprocessBuildWithReport)- 构建Addressables内容包
- 延迟绑定运行时加载器(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 withinstead. See Quick Start.IPreprocessBuildWithReport
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专属)
- 🚨 ALWAYS check for existing scripts FIRST: Before generating code, scan for scripts with identifier and prompt user to update or create new
[UNITY-SKILL:SPRITEATLAS] - 🚨 ALWAYS tag generated scripts: Include skill identifier at the top of every generated script for future detection
- 🚨 ALWAYS ask for delivery mechanism: Ask user "Built-in data or Addressables?" before generating code
- 🚨 ALWAYS enable Sprite Packer mode: Set in prebuild script
EditorSettings.spritePackerMode = SpritePackerMode.SpriteAtlasV2 - 🚨 ALWAYS use prebuild generation: Implement as the DEFAULT approach for creating atlases
IPreprocessBuildWithReport - 🚨 ONLY add sprites from Assets/ folder: Never add sprites from Packages, built-in assets, or external locations
- 🚨 For Addressables delivery, ALWAYS generate THREE scripts:
- Prebuild atlas generator with Addressables setup
- to build Addressables content
IPostprocessBuildWithReport - Late-binding runtime loader script (extends )
SpriteAtlasManager
- Set includeInBuild correctly: for built-in data,
truefor Addressablesfalse - Two-Step Editor Pattern: Create with → Save → Import → Configure via
SpriteAtlasAsset→ SaveAndReimport()SpriteAtlasImporter - Never use in editor code — except
SpriteAtlasinstance method on a loaded runtime atlasSpriteAtlas.GetPackables() - Variants reference master: Use with runtime instance loaded via
SetMasterAtlas(SpriteAtlas)AssetDatabase.LoadAssetAtPath<SpriteAtlas>() - Platform format assignment: Use directly (no cast)
format = TextureImporterFormat.ASTC_6x6 - File extension: V2 atlases use (not
.spriteatlasv2).spriteatlas - is optional: Only for editor preview; build-time packing is automatic
SpriteAtlasUtility.PackAtlases()
For common errors and invalid patterns, see references/common-errors.md.
- 🚨 务必首先检查现有脚本:生成代码前,扫描带有标识符的脚本,并提示用户选择更新或创建新脚本
[UNITY-SKILL:SPRITEATLAS] - 🚨 务必为生成的脚本添加标签:在每个生成的脚本顶部包含技能标识符,以便后续检测
- 🚨 务必询问交付机制:生成代码前,询问用户"内置数据还是Addressables?"
- 🚨 务必启用Sprite Packer模式:在预构建脚本中设置
EditorSettings.spritePackerMode = SpritePackerMode.SpriteAtlasV2 - 🚨 务必使用预构建生成:将实现作为创建图集的默认方案
IPreprocessBuildWithReport - 🚨 仅添加Assets/文件夹中的精灵:切勿添加来自Packages、内置资源或外部位置的精灵
- 🚨 对于Addressables交付,务必生成三个脚本:
- 带有Addressables设置的预构建图集生成器
- 用于构建Addressables内容的
IPostprocessBuildWithReport - 延迟绑定运行时加载器脚本(继承自)
SpriteAtlasManager
- 正确设置includeInBuild:内置数据设为,Addressables设为
truefalse - 两步编辑器模式:使用创建 → 保存 → 导入 → 通过
SpriteAtlasAsset配置 → SaveAndReimport()SpriteAtlasImporter - 切勿在编辑器代码中使用— 已加载的运行时图集上的
SpriteAtlas实例方法除外SpriteAtlas.GetPackables() - 变体引用主图集:对通过加载的运行时实例使用
AssetDatabase.LoadAssetAtPath<SpriteAtlas>()SetMasterAtlas(SpriteAtlas) - 平台格式赋值:直接使用(无需强制转换)
format = TextureImporterFormat.ASTC_6x6 - 文件扩展名:V2图集使用(而非
.spriteatlasv2).spriteatlas - 是可选的:仅用于编辑器预览;构建时打包是自动进行的
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 - implementation
ScriptablePacker - 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; // SpriteAtlasRuntime 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