Index of every page on this site: llms.txt
All skills
oahc09 avatar

/cocosair-ui-2d

@ed38648
by oahc09oahc09/cocosairjs3 stars
2
Run it yourself

Build Canvas UI with sprites, Label text, layout and widgets. Use for missing UI, viewport alignment, 2D draw order, BMFont binding or AIR_E_LABEL_WRAP.

  • 10 files
  • 39.1 KB
  • MIT
  • Updated last month
  • GitHub
Use this Skill: https://skilld.dev/gh/oahc09/cocosairjs/cocosair-ui-2d

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

referencessorting.md

≈1.1k tokens on demand. Your agent reads this file only when SKILL.md points to it.

Derived from src/cocos/sorting/sorting-2d.ts, src/cocos/sorting/sorting-layers.ts, src/cocos/physics-2d/builtin/intersection-2d.ts, src/cocos/scene-graph/layers.ts, build/cocosair.module.js - 2026-10-10

2D sorting

Three symbols share the "2D sorting" heading on the export block (build/cocosair.module.js, currently :161574), but they are not one system: SortingLayers and Sorting2D drive draw order, while Intersection2D is an unrelated 2D geometry helper. Note also that Layers (node layers, src/cocos/scene-graph/layers.ts:60) is a different concept from SortingLayers - node layers gate camera visibility, sorting layers gate draw order within a batch.

Sorting2D

Sorting2D extends Component and is annotated @requireComponent(UIRenderer) (src/cocos/sorting/sorting-2d.ts:50-51), so it only works on a node already carrying a UIRenderer subclass - Sprite, Label, Graphics and friends. Its two knobs:

  • sortingLayer - a SortingLayers.Enum id; the setter rejects an id that is not valid (src/cocos/sorting/sorting-2d.ts:64-71), backed by SortingLayers.isLayerValid (src/cocos/sorting/sorting-layers.ts:129-137).
  • sortingOrder - clamped to int16 range [-32768, 32767] (src/cocos/sorting/sorting-2d.ts:33-34, :81-85); smaller renders first under the default rule.

Both setters funnel into _updateSortingPriority(), which, when enabled, writes uiRenderer.priority = SortingLayers.getSortingPriority(getLayerIndex(layer), order) (src/cocos/sorting/sorting-2d.ts:115-126). When the component is disabled it restores the default priority (:122-124). onEnable/onDisable also bump a module-level count reported to the batcher via _setSorting2DCount (src/cocos/sorting/sorting-2d.ts:101-113). The priority write, not a parallel queue, is what reorders draws.

const sorting = panelNode.addComponent(Sorting2D);   // panelNode already has a Sprite (a UIRenderer)
sorting.sortingLayer = SortingLayers.Enum.default;   // the only built-in id
sorting.sortingOrder = -10;                          // render before order-0 siblings

SortingLayers

SortingLayers is a static manager (src/cocos/sorting/sorting-layers.ts:50). Priority packing: getSortingPriority(layer, order) = (((layer + (1<<15)) << 16) | (order + (1<<15))) >>> 0 (src/cocos/sorting/sorting-layers.ts:64-66), so layer dominates order: a higher layer index always sorts later regardless of sortingOrder (src/cocos/sorting/sorting-layers.ts:46-48).

Out of the box SortingLayers.Enum carries only default: 0 (src/cocos/sorting/sorting-layers.ts:36-38), and getBuiltinLayers() returns exactly [{ id: 0, name: 'default', value: 0 }] (src/cocos/sorting/sorting-layers.ts:143-144). Extra layers come from the ENGINE.sortingLayers setting loaded in init() at Game.EVENT_POST_SUBSYSTEM_INIT (src/cocos/sorting/sorting-layers.ts:40-42, :152-154). Query helpers are getLayerIndex (:76-84), getLayerIndexByName (:90-93) and getLayerByName (:113-123); an unknown id logs errorID(2105) and returns 0 (:81, :134). With only the default layer registered, all differentiation happens through sortingOrder.

Intersection2D is not a sorter

Intersection2D is export default class Intersection2D (src/cocos/physics-2d/builtin/intersection-2d.ts:301) holding static 2D shape-intersection tests (segment/segment, rect/polygon and related math, e.g. src/cocos/physics-2d/builtin/intersection-2d.ts:34-40, :60-85). It is used by the 2D physics backend, not the batcher. It sits next to the sorting names on the export block but has no effect on draw order; use it for overlap math, not for stacking sprites.

Layer versus sorting layer

Concept Type Controls Source
Node layer bitmask per node whether a camera renders the node (visibility AND layer) src/cocos/scene-graph/layers.ts:34-46
Sorting layer int id per Sorting2D draw order among batched 2D renderers src/cocos/sorting/sorting-layers.ts:36-38

UI nodes still need a node layer that matches the UI camera visibility before sorting even applies (src/air/ui-kit.ts:38, :61); Sorting2D then orders what the camera already accepts.

Source: SKILL.md on GitHub

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

No third-party reports yet.

Signed by skilld at ed38648. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub yesterday.

Activeupdated last month
layer
subsystem
All 1 allowed tools
Read Edit Write Bash
Other metadata
compatibility
Cocos AIR source checkout or installed cocosair.js package. Browser rendering requires WebGL 2; source tooling requires Node.js >=18. Use the host-provided tools and user-authorized workspace.
when_to_use
[
  "put sprites, labels or controls on screen",
  "UI renders nothing despite visible nodes",
  "widgets must realign after a viewport change",
  "bind an existing bitmap font to Label",
  "2D sprites and labels overlap in the wrong order"
]
owns
[
  "ui-canvas",
  "sprite-label",
  "widget-layout",
  "ui-controls",
  "font-binding",
  "sorting-2d"
]
not_for
[
  "load or parse a bitmap font -> cocosair-assets",
  "sprite masks and DOM overlays -> cocosair-graphics-and-dom",
  "native input and action mapping -> cocosair-input-action"
]
metadata
{
  "author": "oahc09@gmail.com",
  "version": "1.0.0",
  "last-updated": "2026-10-13",
  "description_zh": "使用 Canvas、布局、控件、Label、位图字体和二维排序。用于 UI 构建、文本显示或交互布局问题。"
}

README badge

README badge for oahc09/cocosairjs/cocosair-ui-2d