cocos-ui-schema-generator
cocos-ui-schema-generator
Generate UI as WebUINodeSchema-based TypeScript for the bundled Cocos WebUI runtime.
This skill includes a reusable runtime in assets/webui-runtime/. If the target Cocos project does not already contain these runtime files, copy them into the target project before generating schema or controller code.
Bundled assets
The bundled runtime lives here:
assets/webui-runtime/types.tsassets/webui-runtime/style.tsassets/webui-runtime/layout.tsassets/webui-runtime/WebUIBackground.tsassets/webui-runtime/WebUIRenderer.tsassets/webui-runtime/WebUIExample.tsassets/webui-runtime/uiMeta.tsassets/webui-runtime/examples/withdrawExample.tsassets/webui-runtime/examples/withdrawExample1.ts
Use these files as the source of truth when bootstrapping a new project.
The skill also includes helper scripts:
scripts/install-runtime.ps1scripts/validate-webui-schema.js
Core workflow
- Inspect the target repository.
- Check whether a compatible WebUI runtime already exists.
- If missing, copy the bundled runtime into the target project first.
- Generate a schema as a
.tsfile exportingWebUINodeSchemadata. - If requested, generate a business-facing controller component that uses
WebUIRenderer. - Keep generated output within the capabilities of the bundled runtime.
Runtime bootstrap rule
Before generating new UI files, verify whether the target project already contains equivalent runtime files such as WebUIRenderer.ts, layout.ts, style.ts, and types.ts.
If they do not exist, prefer using scripts/install-runtime.ps1 from this skill to copy the bundled runtime into a suitable location in the target Cocos project. Typical target location:
assets/scripts/webui/- or the repository's existing UI runtime directory
Prefer preserving the bundled file contents exactly unless the target project clearly requires path or naming adjustments.
PowerShell example:
powershell -ExecutionPolicy Bypass -File <skill>/scripts/install-runtime.ps1 -TargetProjectRoot <projectRoot>
File layout and import path rules
Prefer the following project layout when bootstrapping a plain Cocos Creator project:
- runtime:
assets/scripts/webui/ - generated page files:
assets/scripts/
If runtime is installed under assets/scripts/webui/ and generated page files are placed under assets/scripts/, use these relative imports:
- schema ->
import { WebUINodeSchema } from './webui/types'; - meta ->
import { WebUIMeta } from './webui/uiMeta'; - controller ->
import WebUIRenderer from './webui/WebUIRenderer';
Do not guess import paths blindly. Compute imports from the actual generated file location relative to the runtime location.
If the target project uses a different directory layout, adjust imports accordingly, but keep runtime imports consistent within the generated files.
Output format rules
Generate TypeScript, not raw JSON, unless the user explicitly asks for JSON.
Preferred output shape:
- one schema file, e.g.
WithdrawPanel.schema.ts - export a typed constant, e.g.
export const withdrawPanelSchema: WebUINodeSchema = { ... } - one meta object, e.g.
export const withdrawPanelMeta: WebUIMeta = { ... }
If a controller is requested, generate a separate component file, e.g. WithdrawPanel.ts, that:
- extends
cc.Component - gets or adds
WebUIRenderer - renders the schema
- optionally stores references to key nodes after render
- may use
renderer.getNodeById(id)orrenderer.getNodesByIds(ids)when the bundled runtime is present
Do not treat WebUIExample.ts as a required business base class. It is only an example reference.
Allowed node types
Only generate these node types unless the runtime has been explicitly extended:
viewtextimage
Do not invent unsupported nodes such as:
buttoninputscrollrichTextsvgvideo
Represent buttons and clickable areas as view or text nodes with stable ids that business code can bind events to later.
Allowed style rules
Only use style fields supported by the bundled runtime:
displaypositionflexDirectionjustifyContentalignItemsalignSelfflexWrapflexGrowflexShrinkflexBasisgapwidthheightminWidthminHeightmaxWidthmaxHeightpaddingmarginpaddingToppaddingRightpaddingBottompaddingLeftmarginTopmarginRightmarginBottommarginLeftleftrighttopbottomzIndexbackgroundColoropacityborderRadiusfontSizefontWeightcolorlineHeighttextAlignwhiteSpaceoverflowobjectFit
Do not generate unsupported CSS-like fields such as border, boxShadow, transform, grid, filter, letterSpacing, or lineClamp unless the user explicitly asks to extend the runtime too.
Unsupported style fallback rules
When the source design contains unsupported CSS-like effects, do not simply copy those fields into schema. Use the following fallback strategy.
Border and divider fallback
borderTop,borderBottom, or similar single-edge border:- replace with a dedicated thin
viewnode, usually height1, width'100%', and a suitablebackgroundColor
- replace with a dedicated thin
- full
borderaround a block:- prefer a nested two-layer structure:
- outer
viewuses the border color and border radius - inner
viewuses the content background color and inner padding or margin to simulate the border thickness
- outer
- prefer a nested two-layer structure:
- radio or outlined circle:
- prefer nested circular
views rather than unsupported border fields
- prefer nested circular
Shadow fallback
boxShadow:- omit by default
- if the shadow is visually important, approximate it with a softer background block or a larger outer wrapper with a subtle background color
- never generate
boxShadowdirectly unless runtime support is explicitly added
Gradient fallback
linear-gradientor other gradient backgrounds:- replace with a single dominant solid color
- if the gradient is central to the design, tell the user that the runtime must be extended for accurate rendering
Transform fallback
transform,scale,rotate,translate:- avoid generating them
- simplify the structure or use normal layout and positioning instead
Rounded clipping fallback
- rounded image clipping or
overflow: hiddenfor card masking:- do not assume generic clipping is fully supported
- if accuracy depends on clipping, either simplify the design or explicitly note the runtime limitation
Always prefer a visually close, runtime-compatible structure over copying unsupported fields.
Layout generation rules
Prefer flex layout over absolute positioning.
Use position: 'absolute' only for clear overlay cases such as:
- floating close buttons
- top-left back icons over a title area
- badges or corner marks
- decorative overlays
For normal structure, prefer:
- semantic container
views flexDirectiongappaddingmargin
Avoid unnecessary nesting. Keep hierarchy shallow while preserving meaningful groups such as header, content, footer, card, row, and action area.
Id and naming rules
Assign stable ids to all business-relevant nodes.
Ids are required for:
- clickable nodes
- dynamic text nodes
- dynamic image nodes
- nodes that may be shown or hidden
- container nodes that business code may access later
Use lower camel case and semantic prefixes when useful:
btnSubmitbtnBacktxtAmounttxtTitleimgAvatarpanelMainlistRewards
Do not rename existing ids during edits unless the user explicitly requests it.
UI meta rules
Generate a lightweight WebUIMeta object alongside the schema for business-facing pages.
Import source:
assets/webui-runtime/uiMeta.ts
Current supported fields:
interactiveIdsdynamicTextIdsdynamicImageIdscontainerIds
Use interactiveIds for clickable nodes such as back, close, submit, invite, tab, and card entry areas.
Use dynamicTextIds for text nodes whose content may change later, such as title, amount, username, countdown, status text, and button label.
Use dynamicImageIds for images that business code may replace later, such as avatar, banner, product, and reward art.
Use containerIds for nodes that business code may access to inject, replace, or toggle content, such as main panel, content panel, empty panel, loading panel, and list container.
Keep WebUIMeta lightweight. Do not try to encode full business logic into it.
Controller generation rules
If asked to generate a controller component, prefer this structure:
onLoad()orstart()ensures aWebUIRendererexists- render the schema once
cacheNodes()optionally stores important node references after renderbindEvents()attaches click handlers for interactive ids- business update methods such as
updateAmount(),updateTitle(), orsetLoadingVisible()modify UI using ids or cached refs
Additional rules:
- render the schema via
WebUIRenderer - avoid embedding large schema objects directly inside the controller if a separate schema file is feasible
- import and use the generated
WebUIMetawhen present - use ids to find nodes after render
- prefer
renderer.getNodeById('someId')when using the bundled runtime - keep event binding in the controller, not in the schema
- keep business logic separate from runtime files
- if
claimToBagMeta.interactiveIdsor similar meta fields exist, keep controller logic aligned with those ids rather than inventing unrelated names
Editing existing generated UI
When revising an existing schema:
- preserve existing ids whenever possible
- avoid restructuring unrelated branches
- make the smallest requested change
- keep compatibility with the bundled runtime
Practical constraints of the bundled runtime
The bundled runtime is suitable for prototype and moderate business UI, but not full browser compatibility.
Important limitations to respect while generating:
- only
view,text,imageare supported flexWrapis typed but not fully implemented for production usageflexShrinksupport is incomplete- image measuring is basic; prefer explicit image width and height
overflow: hiddenis not fully implemented as a generic container clipping system- border, gradient, and shadow are not implemented
When the design depends on unsupported features, simplify the UI or explicitly tell the user that the runtime must be extended.
Preferred generation strategy
For screenshot-to-UI or mockup-to-UI tasks:
- Infer a semantic container structure.
- Convert the structure into a
WebUINodeSchematree. - Add stable ids to business-critical nodes.
- Keep text nodes coarse-grained unless style differences require splitting.
- Give explicit width and height to images.
- Generate a controller only if the user needs interaction wiring or dynamic updates.
Validation rule
When schema or meta is available in machine-readable JSON form for checking, validate it with scripts/validate-webui-schema.js when practical. The validator checks:
- node type validity
- supported style keys
- duplicate ids
- required
props.textfor text - required
props.srcfor image - meta ids that do not exist in schema
Important behavior
If the current repository does not contain the WebUI runtime, do not stop at schema generation. First copy the bundled runtime assets from this skill into the target project, then generate the schema, meta, and any requested controller files.