skills/cocos2d-cli/SKILL.md
用户提供 UI 截图并要求生成白盒预制体时,使用此技能
npx skillsauth add 958877748/skills cocos2d-cliInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Cocos Creator CLI 工具,支持通过 JSON 描述生成预制体/场景,以及截图预览。
JSON 描述 → screenshot 预览 → 迭代调整 → create-prefab 生成
# 预览 JSON 效果
npx cocos2d-cli screenshot panel.json
# 生成预制体
npx cocos2d-cli create-prefab panel.json assets/Panel.prefab
当用户输入一张图,并要求“生成白盒预制体”时,执行下面这个闭环流程,而不是直接一次性输出 prefab。
先分析 UI 截图
生成第一版 JSON
sprite 表示背景、卡片、按钮、图片占位、分组块label 或 richText 表示标题、正文、说明文字Root、Header、Panel、Title、Desc、BtnConfirm使用 CLI 对 JSON 截图
npx cocos2d-cli screenshot <json文件> ...对比截图与原图
如果有差异,修改 JSON
anchorX、width、horizontalAlign再次截图,继续对比
从最终 JSON 生成 prefab
npx cocos2d-cli create-prefab <json文件> <输出.prefab>screenshot 预览收到 UI 截图 + “生成白盒预制体”
→ 分析 UI 截图
→ 生成 first-pass JSON
→ 保存为 json 文件
→ 执行 screenshot
→ 对比截图与原图
→ 如果仍有明显差异,则修改 JSON 并再次 screenshot
→ 如果已无明显差异,则执行 create-prefab
{
"name": "节点名称",
"width": 400,
"height": 300,
"x": 0,
"y": 0,
"anchorX": 0.5,
"anchorY": 0.5,
"color": "#336699",
"opacity": 255,
"components": [
"sprite",
{ "type": "label", "string": "文本", "fontSize": 32, "horizontalAlign": "left" },
{ "type": "richText", "string": "普通<color=#ff0000>红色</color>文字", "fontSize": 28 }
],
"children": []
}
| 属性 | 类型 | 说明 | |------|------|------| | name | string | 节点名称,显示在编辑器层级中 | | x, y | number | 相对于父节点中心的偏移(见坐标系说明) | | width, height | number | 节点宽高 | | anchorX, anchorY | number (0-1) | 锚点,默认 0.5 表示中心 | | color | string (#RRGGBB) | 节点颜色,影响 Sprite 填充/Label 文字色 | | opacity | number (0-255) | 透明度 | | rotation | number | 旋转角度(度) | | scaleX, scaleY | number | 缩放 | | active | boolean | 是否激活 | | components | array | 组件列表(见组件说明) | | children | array | 子节点列表(递归) |
Cocos 默认锚点在节点中心(anchorX=0.5, anchorY=0.5),x/y 是相对父节点中心的偏移。
计算示例:父节点宽 660,子节点宽 150,要靠左留 30px 边距
x = -(660/2) + 30 + (150/2) = -225
靠左/靠右布局推荐写法(使用 anchorX + horizontalAlign):
// 靠左对齐文字:节点锚点设为左边,文字对齐设为 left
{
"anchorX": 0,
"x": -330,
"width": 300,
"components": [
{ "type": "label", "string": "打款人", "horizontalAlign": "left" }
]
}
// 靠右对齐文字:节点锚点设为右边,文字对齐设为 right
{
"anchorX": 1,
"x": 330,
"width": 300,
"components": [
{ "type": "label", "string": "¥40.00", "horizontalAlign": "right" }
]
}
这样做的好处:锚点控制节点定位基准,horizontalAlign 控制文字在节点内的排列,语义清晰,不需要心算偏移量。
精灵,显示为纯色方块(颜色由节点 color 控制)
"sprite"
// 或
{ "type": "sprite", "sizeMode": 0 }
文本组件
{
"type": "label",
"string": "文本内容",
"fontSize": 28,
"lineHeight": 40,
"horizontalAlign": "left", // left / center / right,默认 center
"verticalAlign": "center", // top / center / bottom,默认 center
"color": "#ffffff" // 兼容写法,效果等同于节点 color
}
注意:label 的文字颜色实际由节点 color 控制,组件内的 color 是兼容写法。
重要补充:
label 默认按内容自适应显示,不要把 width / height 当成稳定的文本排版手段anchorX / anchorY 锚定的是节点,不是文字笔画本身的视觉中心horizontalAlign / verticalAlign 主要控制文字在节点内的排列,不等于能自动解决跨多个文本节点的组合排版screenshot 的实际渲染结果为准,不要只凭 JSON 参数脑补最终效果富文本,支持 BBCode 局部样式
{
"type": "richText",
"string": "发起<color=#3cb034>准备40元打款</color>的申请<br/>请及时审批!",
"fontSize": 28,
"lineHeight": 40,
"maxWidth": 600,
"horizontalAlign": "left" // left / center / right
}
支持的 BBCode 标签:
<color=#RRGGBB>文字</color> — 局部变色<size=30>文字</size> — 局部字体大小<b>文字</b> — 加粗<i>文字</i> — 斜体<u>文字</u> — 下划线<br/> — 换行重要:richText 的节点 color 和 BBCode color 不要混用,运行时以 BBCode 为准。
对齐组件
{
"type": "widget",
"isAlignLeft": true,
"isAlignRight": true,
"isAlignTop": true,
"isAlignBottom": true,
"left": 0,
"right": 0,
"top": 0,
"bottom": 0
}
按钮组件(通常配合 sprite 使用)
"button"
# 预览 JSON 效果(截图)
npx cocos2d-cli screenshot <json文件> [选项]
-o, --output <目录> 输出目录,默认当前目录
--width <数值> 视口宽度,默认 750
--height <数值> 视口高度,默认 1334
--debug-bounds 叠加节点边界框和名称
# 生成预制体
npx cocos2d-cli create-prefab [JSON文件] <输出.prefab>
# 生成场景
npx cocos2d-cli create-scene [JSON文件] <输出.fire>
# 查看节点树
npx cocos2d-cli tree <场景或预制体文件>
# 获取节点属性
npx cocos2d-cli get <文件> <节点路径> [属性名|组件类型]
# 设置节点属性
npx cocos2d-cli set <文件> <节点路径> <属性名> <值>
# 添加组件
npx cocos2d-cli add-component <文件> <节点路径> <类型>
<color=#xxx> 设置局部颜色,不要依赖节点 coloranchorX=0(靠左)或 anchorX=1(靠右)配合 horizontalAlignlabel 分散硬摆;应先建立父节点,把这组文本作为一个组合块来组织布局,再分别放置子 labelscreenshot 回看实际效果并微调,不能假设一次参数设置就能直接得到正确视觉结果tools
Cocos Creator 2.4.x 场景/预制体命令行工具,支持读写 .fire/.prefab、JSON 生成 UI、截图预览
development
查询公网IP地址的地理位置信息,包括国家、省份、城市、ISP、时区等
tools
Cocos Creator 有限状态机 (FSM) 实现,用于管理游戏对象的状态转换
tools
Cocos Creator 2.4.x 粒子系统修饰器开发框架。用于创建、使用和调试自定义粒子修饰器,支持修改粒子位置、颜色、大小、旋转、速度等属性。当需要在 Cocos Creator 中实现复杂的粒子效果控制时使用此技能。