unity-2d-rendering
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chineseunity-2d-rendering — draw order is four keys in a fixed priority, and z is the last of them
unity-2d-rendering — 绘制顺序由四个键按固定优先级决定,而 z 是最后一个
Moving a sprite in z is almost never the fix. Two overlapping renderers are compared on sorting layer, then Order in Layer, then render queue, then distance along the sort axis. The first key that differs decides and the rest are never read. World z is only what "distance" happens to mean under the default sort mode — and under a custom axis it means nothing.The firstyou add turns everything it does not light black. With no light in the scene, lit sprites draw at full colour; with one, every pixel outside its shape and every sorting layer it does not target receives darkness. Both halves of this skill are the same question: which asset is the renderer actually reading?Light2D
在 z 轴上移动 Sprite 几乎从来不是解决办法。 两个重叠的 Renderer 会依次比较 Sorting Layer、Order in Layer、Render Queue,以及沿排序轴的距离。第一个 存在差异的键就会决定结果,其余键不再读取。World z 只是默认排序模式下“距离” 所对应的含义——在自定义轴下,它没有任何意义。添加的第一盏 Light2D 会让它未照亮的一切变黑。 场景中没有灯光时,受光 Sprite 会以全彩显示;添加一盏灯后,其范围之外的每个像素,以及它未针对的每个 Sorting Layer,都会陷入黑暗。该 skill 的这两部分本质上都在回答同一个问题:Renderer 实际上读取的是哪个资源?
Start here — symptom → where to look
从这里开始——症状 → 排查位置
First match wins. Stop at the first cause the audit or the frame confirms.
| Symptom | Go to |
|---|---|
| A sprite draws behind one it should be in front of | §1 |
| Changing z or Order in Layer makes no difference | §1 — an earlier key already decided |
| Two sprites flicker or swap as the camera moves | §1 — a tie on all four keys |
| A character's hat, weapon or carried item sorts apart from its body | §2 |
| Something appears between the parts of a character | §2 |
| Seen from above, the runner walks behind a shelf or wall it stands in front of | §3 |
| Isometric or hex pieces draw inside out against the board | §3 |
| The sort axis is set under Project Settings and nothing changed | §0, then §3 |
| The runner cannot walk behind a tall tile, or is always behind the whole Tilemap | §4 |
| Particles or a world canvas draw under the sprites | §4 |
| The Light 2D does nothing | §5 |
| Everything went black after adding one light | §5 — no Global light |
| One sorting layer stays dark, or stays bright, under a light | §5 — target sorting layers |
| No shadow, or a wall that shades itself black | §6 |
| The normal map does nothing | §6 |
| A sprite mask hides nothing, or hides sprites it should leave alone | §7 |
| Tinting or flashing one sprite multiplied the draw calls | §7 |
| A parallax background: layers shudder, slide after a respawn, or run out in a long level | §8 |
| A generated scene to scroll endlessly, or props repeating beside their own mirror image | §8 — |
先匹配者为准。审计结果或帧信息确认首个原因后,即停止排查。
| 症状 | 排查位置 |
|---|---|
| 一个 Sprite 绘制在本应位于它前面的另一个 Sprite 后面 | §1 |
| 修改 z 或 Order in Layer 没有产生任何变化 | §1——更早的键已经决定了结果 |
| 两个 Sprite 随相机移动而闪烁或互换 | §1——四个键全部相同 |
| 角色的帽子、武器或携带物与身体分开排序 | §2 |
| 有东西出现在角色的各个部件之间 | §2 |
| 俯视时,跑步者显示在本应位于其前方的货架或墙后面 | §3 |
| Isometric 或 Hex 地块相对棋盘出现错误的内外绘制顺序 | §3 |
| 排序轴已在 Project Settings 中设置,但没有任何变化 | §0,然后 §3 |
| 跑步者无法走到高地块后方,或始终位于整个 Tilemap 后方 | §4 |
| 粒子或 World Space Canvas 绘制在 Sprite 下方 | §4 |
| Light 2D 无效 | §5 |
| 添加一盏灯后一切都变黑 | §5——没有 Global Light |
| 某个 Sorting Layer 在灯光下始终是暗的或亮的 | §5——目标 Sorting Layer |
| 没有阴影,或墙体将自身阴影化为黑色 | §6 |
| Normal Map 无效 | §6 |
| Sprite Mask 没有隐藏任何内容,或隐藏了本应不受影响的 Sprite | §7 |
| 给某个 Sprite 着色或闪烁时,Draw Call 数量成倍增加 | §7 |
| Parallax 背景:图层抖动、重生后滑动,或在长关卡中内容耗尽 | §8 |
| 生成用于无限滚动的场景,或道具紧挨着自己的镜像重复出现 | §8—— |
0. Before diagnosing
0. 诊断前
Work on a live Editor ( → ) and run two reports first:
— the pipeline in force, its renderers, and the sort mode and axis
actually read (built-in: the camera and Project Settings; URP without a 2D Renderer: the camera's
axis and no lights; URP with one: only the renderer asset's) — and —
every renderer's four keys, group, sort point, lit material and the pairs that tie. Answer from
the Frame Debugger where it disagrees with the audit; change only the key that decided, and
restore anything changed while probing. In full: .
unity-debugreference/editor-control.mdRenderer2DSetup.Report()SortingAudit.Json()reference/before-diagnosing.md在实时运行的 Editor 中操作( → ),并首先运行两个报告:
——当前生效的管线、其 Renderer,以及实际读取的排序模式和轴
(Built-in:相机与 Project Settings;URP 未配置 2D Renderer:相机的轴且没有灯光;
URP 配置了 2D Renderer:只使用 Renderer 资源的设置)——以及
——每个 Renderer 的四个键、group、sort point、受光材质,以及
出现并列的配对。如果审计结果与 Frame Debugger 不一致,以 Frame Debugger 为准;只修改
实际决定结果的键,并恢复探测期间的任何更改。完整内容:
。
unity-debugreference/editor-control.mdRenderer2DSetup.Report()SortingAudit.Json()reference/before-diagnosing.md1. The four keys
1. 四个键
| # | Key | Set on | Decides |
|---|---|---|---|
| 1 | Sorting layer | Renderer; order from the list in Tags and Layers | Higher in the list draws first |
| 2 | Order in Layer | Renderer, −32768 to 32767 | Higher draws later, on top |
| 3 | Render queue | Material | Higher draws later. Both default sprite shaders are 3000 |
| 4 | Distance along the sort axis | The renderer's sort point and the sort mode | Further draws first |
- Compare in order. Find the two renderers in the audit and read ,
layerValue,order,queue. The first difference is the whole explanation.distance - A tie on all four has no defined order and can swap from frame to frame. The audit's
array lists every overlapping pair. Fix a tie with a key, never with a hope that hierarchy order holds.
ties - is not an order. Compare
SortingLayer.id. A misspeltSortingLayer.GetLayerValueFromID(id)lands on Default without an error.sortingLayerName - z is key 4 only, and only under ,
DefaultorOrthographic. UnderPerspectivewithCustomAxisz is ignored completely.(0, 1, 0) - Key 3 behaves differently outside the 2D Renderer. The 2D Renderer sorts every queue in
one back-to-front list per batch of layers. The Built-in pipeline and URP's Universal
Renderer draw opaque-queue materials in an earlier pass with depth writing: an opaque
there ignores keys 1 and 2 against sprites and hides them by z.
MeshRenderer
Each key in full, the three places a sort axis can live, and the fix per deciding key:
.
reference/sort-order.md| # | 键 | 设置位置 | 决定规则 |
|---|---|---|---|
| 1 | Sorting Layer | Renderer;顺序来自 Tags and Layers 中的列表 | 列表中位置越高,绘制越早 |
| 2 | Order in Layer | Renderer,−32768 到 32767 | 值越高,绘制越晚,即位于上层 |
| 3 | Render Queue | Material | 值越高,绘制越晚。两个默认 Sprite Shader 的值都是 3000 |
| 4 | 沿排序轴的距离 | Renderer 的 sort point 和排序模式 | 距离越远,绘制越早 |
- 按顺序比较。 在审计中找到两个 Renderer,读取 、
layerValue、order、queue。第一个差异就是完整原因。distance - 四个键全部相同时,顺序未定义,可能会逐帧互换。审计中的 数组会列出 每一对发生重叠并列的 Renderer。使用某个键解决并列,绝不能指望 Hierarchy 顺序生效。
ties - 不是顺序值。 应比较
SortingLayer.id。 拼错SortingLayer.GetLayerValueFromID(id)会落到 Default,而不会报错。sortingLayerName - z 仅是第 4 个键,并且只在 、
Default或Orthographic模式下生效。 在Perspective且轴为CustomAxis时,z 会被完全忽略。(0, 1, 0) - 在 2D Renderer 之外,第 3 个键的行为有所不同。 2D Renderer 会按每组 Layer
将所有 Queue 放入同一个从后到前的排序列表。Built-in 管线和 URP 的 Universal Renderer
会在更早的 Pass 中绘制 Opaque Queue 材质并写入深度:在这些管线中,Opaque
与 Sprite 比较时会忽略第 1、2 个键,并通过 z 遮挡 Sprite。
MeshRenderer
每个键的完整说明、排序轴可能存在的三个位置,以及针对每个决定键的修复方法:
。
reference/sort-order.md2. SortingGroup — a character is one thing
2. SortingGroup——角色是一个整体
A on a root makes everything below it sort as one unit against the outside,
using the group's own layer and order and the root's position. Inside, the children order
themselves with their own keys.
SortingGroup- A multi-sprite or skeletal character gets one group on its root. Parts are ordered inside with Order in Layer: body 0, satchel 1, cap 2.
- Nothing outside can be drawn between two members. To put a pickup between body and cap, make it a child inside the group.
- A child's Order in Layer cannot lift it over an outside object; raise the group's.
- on a nested group takes it out of its parent.
sortAtRoot
What a group costs and what it isolates: .
reference/sort-order.md在根对象上添加 ,会让其下方所有内容作为一个整体与外部对象排序,
并使用该 Group 自身的 Layer、Order 和根对象的位置。在 Group 内部,子对象使用各自的键排序。
SortingGroup- 多 Sprite 或骨骼角色应在根对象上添加一个 Group。各部件在内部通过 Order in Layer 排序:身体 0、背包 1、帽子 2。
- 外部对象不能绘制在两个成员之间。若要使拾取物位于身体和帽子之间,应将其作为 Group 内的子对象。
- 子对象的 Order in Layer 无法使其越过外部对象;应提高 Group 的 Order。
- 嵌套 Group 上的 会使其脱离父 Group,独立参与排序。
sortAtRoot
Group 的性能开销及其隔离范围:。
reference/sort-order.md3. Sorting from above
3. 俯视排序
Depth in a top-down, isometric or hex view is key 4 with a custom axis. Four settings, and it
fails if any one is missing:
- Sort mode , axis
CustomAxis— on the asset §0 named.(0, 1, 0) - Pivot at the feet on every sprite that takes part — for setting pivots.
unity-2d-sprites - . The default is
SpriteRenderer.spriteSortPoint = SpriteSortPoint.Pivot, which swaps a tall shelf and the runner half a shelf late.Center - One sorting layer and one Order in Layer for everything that Y-sorts. Keys 1 and 2 beat the axis.
Isometric Z as Y starts from . A board with rows is often better served by an
Order in Layer per row than by the axis. Layer table, split tall objects, the isometric and hex
cases, and the row formula: .
(0, 1, -0.26)reference/y-sort.md在俯视、Isometric 或 Hex 视角中,深度由第 4 个键和自定义轴决定。以下四项设置缺一不可:
- Sort mode 为 ,轴为
CustomAxis——设置在 §0 指明的资源上。(0, 1, 0) - 每个参与排序的 Sprite 的 Pivot 都位于脚部——通过 设置 Pivot。
unity-2d-sprites - 。默认值是
SpriteRenderer.spriteSortPoint = SpriteSortPoint.Pivot,这会让高货架 和跑步者直到半个货架之后才发生互换。Center - 所有参与 Y-sort 的对象使用同一个 Sorting Layer 和同一个 Order in Layer。第 1、2 个键 的优先级高于排序轴。
在等距视角中把 Z 当作 Y 时,轴从 开始。对于带行结构的棋盘,按每行设置
Order in Layer 通常比依赖排序轴更合适。Layer 表、高物体拆分、Isometric 和 Hex 情况,
以及行计算公式:。
(0, 1, -0.26)reference/y-sort.md4. Tilemaps, particles and UI
4. Tilemap、粒子和 UI
- .
TilemapRenderer.modesorts the map as chunks: the runner is in front of all of it or behind all of it.Chunksorts each tile at its own position against other renderers on the same layer and order — required for tall tiles the runner walks behind, and it gives up chunk batching. Keep floors onIndividual, put tall tiles on a second Tilemap.Chunk - is the corner the renderer starts from among its own tiles.
TilemapRenderer.sortOrder - A particle system is one unit with its own Sorting Layer and Order in Layer.
- A Screen Space – Overlay canvas draws after the scene and takes no part. Camera and World
Space canvases sort with sprites by and
Canvas.sortingLayerID.sortingOrder
Values and checks: .
reference/sort-order.md- 。
TilemapRenderer.mode会按 Chunk 对地图排序:跑步者要么位于整个地图前面, 要么位于整个地图后面。Chunk会让每个 Tile 根据自身位置与相同 Layer 和 Order 上的其他 Renderer 排序——要让跑步者能够走到高 Tile 后面,就必须使用此模式,但这会放弃 Chunk 批处理。地面应保留为Individual,高 Tile 应放在第二个 Tilemap 中。Chunk - 是 Renderer 在自身 Tile 之间开始排序时采用的角点位置。
TilemapRenderer.sortOrder - 粒子系统是一个整体,拥有自己的 Sorting Layer 和 Order in Layer。
- Screen Space – Overlay Canvas 会在场景之后绘制,不参与场景排序。Camera 和
World Space Canvas 通过 和
Canvas.sortingLayerID与 Sprite 一起排序。sortingOrder
相关数值和检查项:。
reference/sort-order.md5. The light does nothing
5. 灯光无效
Three conditions, all required. First failure wins.
- The 2D Renderer is in force — in the §0 report.
renderer2DInForce - The sprite's material is lit — in the audit.
litand the Built-inSprite-Unlit-Defaultnever react.Sprites/Default - The light targets the sprite's sorting layer. A light targets the layers that existed when it was created; a layer added later is missing from lights in prefabs and unopened scenes.
And the inversion: no at all means full colour; one means everything it
does not reach is black. Make the first light in a scene a Global light on every layer.
Light2DLight2DLight types and defaults, blend styles, the cost per light, and why lights that skip a layer
in the middle of the stack multiply the light textures:
.
reference/lights-2d.md以下三个条件必须全部满足,任意一个首先不满足都会导致问题。
- 2D Renderer 已生效——查看 §0 报告中的 。
renderer2DInForce - Sprite 的材质支持受光——查看审计中的 。
lit和 Built-inSprite-Unlit-Default永远不会响应灯光。Sprites/Default - 灯光的目标包含 Sprite 的 Sorting Layer。 灯光只会针对创建时已经存在的 Layer; 后来添加的 Layer 不会出现在 Prefab 和尚未打开的场景中的灯光目标里。
反过来看:完全没有 时对象显示为全彩;存在一盏 时,它未覆盖的一切
都会变黑。 场景中的第一盏灯应设置为覆盖每个 Layer 的 Global Light。
Light2DLight2D灯光类型和默认值、混合风格、每盏灯的成本,以及为什么跳过堆叠中间某个 Layer 的灯光
会成倍增加光照纹理:。
reference/lights-2d.md6. Shadows and normal maps
6. 阴影和 Normal Map
Each needs a setting on the light and a piece on the sprite.
- Shadow: the light has shadows enabled and is not Global; the object has a
; both target the layer the shadow falls on.
ShadowCaster2Dleaves the caster itself lit;CastShadowdarkens it. A Tilemap casts through its composite collider.CastAndSelfShadow - Normal map: the sprite carries a secondary texture named —
_NormalMap→unity-2d-sprites— and the light's Normal Maps quality isreference/data-provider.mdorFast. The default isAccurate, and the property is read-only from script.Disabled - Mask map: on the sprite plus a blend style that reads a mask channel.
_MaskTex
reference/shadows-and-normals.md两者都需要在灯光上设置相应选项,并在 Sprite 上提供相应配置。
- 阴影: 灯光已启用阴影且不是 Global;对象具有 ;两者都针对阴影 所在 Layer。
ShadowCaster2D会让投射者自身继续受光;CastShadow会让投射者自身 也变暗。Tilemap 可以通过其复合 Collider 投射阴影。CastAndSelfShadow - Normal Map: Sprite 携带名为 的辅助纹理——
_NormalMap→unity-2d-sprites——并且灯光的 Normal Maps 质量设置为reference/data-provider.md或Fast。 默认值是Accurate,并且该属性无法通过脚本修改。Disabled - Mask Map: Sprite 上存在 ,并搭配会读取 Mask Channel 的 Blend Style。
_MaskTex
reference/shadows-and-normals.md7. Masks, materials and batches
7. Mask、材质和批处理
- A reaches only renderers whose
SpriteMaskis notmaskInteraction— the default isNone. Narrow it with a custom range in sorting-layer and order units, or by placing it inside aNone.SortingGroup - Tint with : it is vertex colour and keeps the batch. A
SpriteRenderer.colororMaterialPropertyBlocktakes the renderer out of it.renderer.material - Two sprites batch only when consecutive in sorted order with the same material and texture. Y-sorted scenes interleave textures constantly; one atlas for the Y-sorted layer restores it.
reference/masks-and-materials.md- 只会影响
SpriteMask不为maskInteraction的 Renderer,默认值是None。 可以使用以 Sorting Layer 和 Order 为单位的自定义范围来缩小作用范围,也可以将其放入None中。SortingGroup - 使用 进行着色:它使用 Vertex Color,不会破坏批处理。使用
SpriteRenderer.color或MaterialPropertyBlock会使 Renderer 脱离原有批次。renderer.material - 只有当两个 Sprite 在排序结果中相邻,并且材质和纹理相同时,才能合批。Y-sort 场景中 纹理会不断交错;为参与 Y-sort 的 Layer 使用单一 Atlas 可以恢复合批。
reference/masks-and-materials.md8. Parallax
8. Parallax
One factor per layer — moves with the world, is locked to the camera — applied
after the camera's final position for the frame. A layer placed before the camera trails
by a frame and shudders on every acceleration. runs after the
Cinemachine Brain, wraps endlessly for a runner, snaps to the pixel grid, and has for
cuts and respawns. Camera timing itself is → .
01resources/ParallaxLayer.csSnap()unity-camerareference/update-order-and-jitter.mdA scene split into depth layers ( → ) becomes one rig
with : mirrors continuous strips and shuffles props one per
tile with seeded gaps, so no prop stands beside its own copy; tile i always holds the same art.
comfyui-asset-generationscene_layers.pyParallaxRigBuilderParallaxTilesreference/parallax.mdreference/parallax-layers.md每个 Layer 使用一个系数—— 表示随世界移动, 表示锁定相机——并在相机完成本帧的
最终位置计算后应用。放在相机之前执行的 Layer 会落后一帧,并在每次加速时发生抖动。
会在 Cinemachine Brain 之后运行,为跑步者提供无限循环,
对齐像素网格,并提供用于镜头切换和重生的 。相机时序本身参见
→ 。
01resources/ParallaxLayer.csSnap()unity-camerareference/update-order-and-jitter.md按深度拆分的场景( → )会通过
组装为一个 Rig: 会镜像连续条带,并使用带种子的
间隔逐 Tile 随机排列道具,避免道具紧挨着自己的副本;第 i 个 Tile 始终包含相同的美术资源。
comfyui-asset-generationscene_layers.pyParallaxRigBuilderParallaxTilesreference/parallax.mdreference/parallax-layers.mdThe tools
工具
| File | Does |
|---|---|
| Read-only. Every renderer's four keys, group, sort point, lit or unlit, and tied pairs, as JSON. |
| |
| One parallax layer: factor, wrap, pixel snap, |
| Endless tiles on a layer: Mirror, Repeat, Shuffle with gaps; deterministic per tile |
| |
Menu root: Tools → Unity Dev → 2D Rendering.
| 文件 | 功能 |
|---|---|
| 只读。以 JSON 输出每个 Renderer 的四个键、group、sort point、受光或不受光状态,以及并列的配对。 |
| 通过 |
| 单个 Parallax Layer:系数、循环、像素对齐、 |
| Layer 上的无限 Tile:Mirror、Repeat、带间隔的 Shuffle;每个 Tile 保持确定性 |
| 将 |
菜单根路径:Tools → Unity Dev → 2D Rendering。
Scope — what this skill does NOT do
范围——此 skill 不处理的内容
Pixel-art filtering, the pixel-perfect camera and seams between tiles are
. Post-processing Volumes, bloom on lit sprites and renderer features
are ; moving a Built-in project onto URP is . Sprite
pivots, slicing and secondary textures are ; packing is .
Tile palettes, their own sort setting and rule tiles are ; which Tilemap goes on
which sorting layer is → . Skeletal sprite meshes
and bones are . Colliders, including the Tilemap collider a shadow caster
reads, are . Camera follow, confiners and shake are . Draw
calls and frame time as numbers are .
unity-2d-pixel-perfectunity-render-urpunity-urp-migrationunity-2d-spritesunity-sprite-atlasunity-tilemapunity-2d-levelreference/layer-stack.mdunity-2d-animationunity-physics-2dunity-cameraunity-profiling像素艺术过滤、Pixel Perfect Camera 以及 Tile 之间的接缝属于 。
Post-processing Volume、受光 Sprite 上的 Bloom 和 Renderer Feature 属于
;将 Built-in 项目迁移到 URP 属于 。
Sprite Pivot、切片和辅助纹理属于 ;打包属于 。
Tile Palette、其自身的排序设置和 Rule Tile 属于 ;哪个 Tilemap 应放在
哪个 Sorting Layer 上属于 → 。
骨骼 Sprite Mesh 和 Bone 属于 。Collider,包括 Shadow Caster 读取的
Tilemap Collider,属于 。相机跟随、边界限制和震动属于 。
以数值形式呈现的 Draw Call 和 Frame Time 属于 。
unity-2d-pixel-perfectunity-render-urpunity-urp-migrationunity-2d-spritesunity-sprite-atlasunity-tilemapunity-2d-levelreference/layer-stack.mdunity-2d-animationunity-physics-2dunity-cameraunity-profiling