sprite-editor

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Sprite Editor

Sprite编辑器

Sprite metadata (rects, borders, pivots, outlines) lives inside the importer, not in a file you can edit — reaching it means running C# through a live Editor.
The
unity-cli
skill owns getting you there
— installing the CLI, confirming a connected Editor, adding the project's
com.unity.pipeline
package, telling a genuinely absent Editor apart from one stuck in Safe Mode, and discovering the Editor's command catalog. Follow it first; don't re-derive any of it here.
Two things it can't know for you:
  • You need
    eval
    in particular
    , not just a reachable Editor. Confirm it appears in the catalog — its presence depends on the Pipeline package version, not on the CLI.
  • Never hand-edit a
    .meta
    file to change sprite metadata.
    The importer owns that data and the capability checks below exist to prevent corruption, so an unreachable Editor is a stop, not a cue to improvise.
Run C# through the connected Editor with the
eval
command. Discover its parameter shape from
unity command --format json
rather than assuming one — the inline form is
unity command eval --code '<snippet>'
, and some Pipeline versions also register
eval_file
for running a snippet from a file. Check the catalog before reaching for
eval_file
; it is frequently absent.
unity command
defaults to a 30 second timeout.
Generates C# editor scripts to manipulate Unity sprites using ISpriteEditorDataProvider. Works with TextureImporter, PSBImporter, and custom importers.
精灵元数据(矩形、边框、轴心点、轮廓)存储在导入器中,而非可编辑的文件内——要访问这些数据,需通过运行中的Editor执行C#代码。
unity-cli
技能负责帮你完成前期准备
——安装CLI、确认已连接Editor、添加项目的
com.unity.pipeline
包、区分真正未启动的Editor与陷入安全模式的Editor,以及发现Editor的命令目录。请先遵循该技能的指引;请勿在此处重复推导相关内容。
有两件事它无法为你确定:
  • 你尤其需要
    eval
    命令
    ,而非仅仅是可访问的Editor。请确认它出现在命令目录中——它是否存在取决于Pipeline包的版本,而非CLI。
  • 切勿手动编辑
    .meta
    文件来修改精灵元数据
    。导入器拥有这些数据的所有权,下文的能力检查正是为了防止数据损坏,因此若无法访问Editor,应停止操作,而非自行尝试其他方法。
使用
eval
命令通过已连接的Editor运行C#代码。请通过
unity command --format json
查看其参数格式,而非自行假设——内联形式为
unity command eval --code '<snippet>'
,部分Pipeline版本还注册了
eval_file
用于运行文件中的代码片段。在使用
eval_file
前请先检查命令目录;它通常不存在。
unity command
默认超时时间为30秒。
通过ISpriteEditorDataProvider生成C#编辑器脚本以操作Unity精灵。支持TextureImporter、PSBImporter及自定义导入器。

Passing C# to
eval

eval
传递C#代码

eval
compiles a statement block, not a file. Two consequences, both of which cause a compile error rather than a warning:
  • No
    using
    directives.
    The compiler reads
    using UnityEngine;
    as a resource-disposal statement and rejects it (
    CS0210
    ).
  • Types must be fully qualified. A bare
    AssetDatabase
    or
    Volume
    does not resolve (
    CS0246
    /
    CS0103
    ), and a bare
    Object
    is ambiguous with
    object
    (
    CS0104
    ).
Where a snippet below is written as a file — with usings, for readability, or because it is meant to be saved into the project — qualify the types before passing it to
eval
.
eval
编译的是语句块,而非文件。这会导致两个后果,均会引发编译错误而非警告:
  • 不支持
    using
    指令
    。编译器会将
    using UnityEngine;
    视为资源释放语句并拒绝执行(错误码
    CS0210
    )。
  • 类型必须完全限定。直接使用
    AssetDatabase
    Volume
    无法解析(错误码
    CS0246
    /
    CS0103
    ),直接使用
    Object
    会与
    object
    产生歧义(错误码
    CS0104
    )。
若下方的代码片段是以文件形式编写的——包含using语句以提升可读性,或需要保存到项目中——在传递给
eval
前,请先对类型进行完全限定。

Workflow

工作流程

All generated scripts must follow the Safe Core Pattern in references/templates.md, which includes MANDATORY capability checks. NEVER attempt operations if capability checks fail - this prevents data corruption. After execution, verify results in Unity console and Project window.
所有生成的脚本必须遵循references/templates.md中的安全核心模式,其中包含强制能力检查。若能力检查失败,绝不能执行操作——这是为了防止数据损坏。执行完成后,请在Unity控制台及项目窗口中验证结果。

Common Operations

常见操作

Modify Name/Rect/Border/Pivot: Update corresponding
SpriteRect
fields (see scripts/SetPivotExample.cs for pivot examples)
  • Requires:
    EditSpriteName
    ,
    EditSpriteRect
    ,
    EditBorder
    , or
    EditPivot
Add/Remove/Slice: Create or filter
SpriteRect
array (see references/background.md for Unity 2021.2+ requirements)
  • Requires:
    CreateAndDeleteSprite
Set Outlines: Get
ISpriteOutlineDataProvider
→ Call
SetOutlines()
with GUID + Vector2 arrays
修改名称/矩形/边框/轴心点: 更新对应的
SpriteRect
字段(轴心点示例可查看scripts/SetPivotExample.cs)
  • 所需权限:
    EditSpriteName
    EditSpriteRect
    EditBorder
    EditPivot
添加/删除/切片: 创建或筛选
SpriteRect
数组(Unity 2021.2+的要求请查看references/background.md
  • 所需权限:
    CreateAndDeleteSprite
设置轮廓: 获取
ISpriteOutlineDataProvider
→ 使用GUID和Vector2数组调用
SetOutlines()

Important Notes

重要注意事项

  • Do NOT use AssetPostprocessor or MenuItem patterns
  • Generate standalone snippets only — no
    AssetPostprocessor
    , no
    MenuItem
  • Enum assignments: Always use enum values and cast to numeric types. Never use raw numbers.
    • ✅ Correct:
      (int)SpriteAlignment.Center
    • ❌ Wrong:
      1
      (magic number)
  • 请勿使用AssetPostprocessor或MenuItem模式
  • 仅生成独立代码片段——不使用
    AssetPostprocessor
    ,不使用
    MenuItem
  • 枚举赋值: 始终使用枚举值并转换为数值类型。切勿使用原始数字。
    • ✅ 正确:
      (int)SpriteAlignment.Center
    • ❌ 错误:
      1
      (魔术数字)