cocos-code-driven-ui-common-pitfalls
Use when code-driven UI nodes in Cocos Creator 3.x fail to render text or colors.
Codex 또는 Claude로 설치 이 Prompt를 복사해 Codex, Claude 또는 다른 어시스턴트에 붙여 넣으면 Skill 페이지를 검토하고 설치를 진행할 수 있습니다.
메뉴
Use when code-driven UI nodes in Cocos Creator 3.x fail to render text or colors.
Codex 또는 Claude로 설치 이 Prompt를 복사해 Codex, Claude 또는 다른 어시스턴트에 붙여 넣으면 Skill 페이지를 검토하고 설치를 진행할 수 있습니다.
SOC 직업 분류 기준
Use when installing Cocos Creator 3.x and setting up a headless test bootstrap.
Use when setting up a GUI-free CLI build pipeline for Cocos Creator 3.x.
Use when building code-driven scrollable UI panels in Cocos Creator 3.x.
Use when Cocos Creator 3.x CLI build fails with scene or library errors.
Use when installing Cocos Creator 3.x and debugging scene import errors.
Use when creating a runtime test without a .scene file in Cocos Creator 3.x.
| name | cocos-code-driven-ui-common-pitfalls |
| description | Use when code-driven UI nodes in Cocos Creator 3.x fail to render text or colors. |
佛心項目使用全 code-driven 方式建立 UI(無 Editor 綁定),new Node() + addComponent(Label) 動態生成。這種方式容易遇到幾個 Cocos 3.x 特有的坑。
動態 new Node() 預設的 layer 是 DEFAULT(即 3D 層),會被 Main Camera 以 3D 方式渲染,導致 UI 文字變成暗黑/灰色剪影。
const UI_2D_LAYER = 1 << 25; // 33554432
const myNode = new Node('MyText');
myNode.layer = UI_2D_LAYER; // ← 每個動態 UI Node 都必須加!
重要: 子節點不會繼承父節點的 layer!每個 new Node() 都要單獨設置,即使父節點已經是 UI_2D。
動態生成的 Label 如果沒有指定 font asset,可能完全不會渲染文字(顯示為空白或暗黑)。
const label = node.addComponent(Label);
label.string = '文字內容';
label.useSystemFont = true; // ← 必須加!
label.fontSize = 20;
Color.fromHEX() 在 Cocos 3.8.8 中可能有不穩定的問題。最穩妥的寫法是直接使用 new Color() 實例化。
// ✅ 正確 (強烈推薦)
label.color = new Color(255, 255, 255, 255); // 純白
label.color = new Color(255, 215, 0, 255); // 金色
// ❌ 可能失敗
label.color = this._hexToColor('#FFFFFF');
// ❌ 絕對錯誤 — 不能直接賦值 hex string
label.color = '#FFFFFF';
Cocos 3.x 的 .scene JSON 是一個 flat array,使用 array index 作為 implicit __id__。這意味著:
__id__ 值必須為 null(不能用數字)[i] 的 object 就是 __id__: i__id__ 指向另一個 object 時,值是 array index(如 "__id__": 2 表示 array[2])__id__ reference 值必須 < array length極常見錯誤:手寫 custom script component
錯誤寫法(自定義 type name + __scriptAsset__):
{
"__type__": "GameBootstrapper", // ❌ 錯誤!Cocos 不認
"node": { "__id__": 2 },
"__scriptAsset__": { "__uuid__": "..." } // ❌
}
正確寫法(cc.Script + _scriptAsset):
{
"__type__": "cc.Script", // ✅ 必須用 cc.Script
"_name": "GameBootstrapper",
"node": { "__id__": 2 },
"_scriptAsset": { // ✅ 用 _scriptAsset(底線)
"__uuid__": "df04b42a-..."
},
"_id": "zen-bootstrapper-001",
}
Array index (id) 規則(真實案例):
_components[] 指向 __id__: 5 (UITransform)、__id__: 6 (Canvas)__id__: 16(因為 index 16 是最後一個 entry)__id__: 17 會 crash — Cannot read properties of undefined (reading '__type__')正確做法(按優先級):
new Node() + addComponent(GameBootstrapper) 動態掛載(Cocos auto-mount pattern)cc.Script format 同 array index 規則錯誤做法:
patch() 編輯 .scene JSON — escaped quotes (\\\") 會 corrupt 檔案__id__ 錯位__type__ 名稱(非 cc.Script)— deserializer 會 crashjson.dump 保留 __id__ 的 numeric value — 必須設為 null直接修改 .scene 檔案後,Cocos Editor 可能仍使用 cache,導致「Load current scene data failed」錯誤。
解決方法: 關閉並重開 Cocos Creator(File → Quit → 重新打開 project)。
修改 .ts 檔案後如果 Play mode 不生效,可能因為 Editor 未觸發重新編譯。
強制刷新方法:
import { _decorator, Component, Node, Label, Color, UITransform } from 'cc';
const UI_2D_LAYER = 1 << 25;
export class MyUI extends Component {
start() {
const root = new Node('MyRoot');
root.layer = UI_2D_LAYER;
this.node.addChild(root);
const labelNode = new Node('MyLabel');
labelNode.layer = UI_2D_LAYER; // ← 每個子節點都要
root.addChild(labelNode);
const label = labelNode.addComponent(Label);
label.string = '清晰可見的文字';
label.useSystemFont = true; // ← 必須
label.fontSize = 24;
label.color = new Color(255, 255, 255, 255); // ← 用 new Color()
}
}
new Node() 有冇 layer = UI_2D_LAYER?useSystemFont = true?.color = 係咪用 new Color(r, g, b, a)?.scene JSON 的 __id__?#0A0A12(對應 RGBA: new Color(10, 10, 18, 255))#FFD700(對應: new Color(255, 215, 0, 255))#FFFFFF(對應: new Color(255, 255, 255, 255))new Color(40, 40, 80, 220)