本文章可以幫你
- 了解 Cocos Creator 3.x 團隊開發的基本規範
- 統一命名、Log、Git commit 與開發流程
- 作為 Code Review 的檢查依據
Basic Convention
Cocos Creator 3.8.3
TypeScript only (Type & Object-Oriented)
SOLID / DRY / KISS
appropriate design patterns are encouraged
“coder-oriented” mode, most things will be done by code, Cocos Creator takes minimal job
using eslint and prettier
naming rule
No Abbreviated / Ambiguous Naming,id,msg,initare accepted- typescript variable, typescript method using camel case
testName - class using pascal case
TestName - interface using “I” as a prefix
ITestName - enum members using UPPER_CASE
- image file name using snake case
test_name - folder name using kebab case
test-name
line break using LF (
\n), NOT CRLF (\r\n)Log
log level
log: show information.warn: errors that willNOTcause the system to break down.error: errors that will cause the system to break down, user can not continue.
using
[className]as title1
2log(`[PlatformWeb] deviceId = ${this.deviceId}`);
log(`[WebSocketWrapper] ${this.host}: new socket`);
group cocos lifecycle, private, protected, public methods (using
#region)order by
importfirst, thenconst
Cocos Convention
- ESM first, not
export default class(CommonJS),cocos 3.x 版推崇 ESM,或許之後會支援 tree shaking - binding events in code (on/off),綁定在 cocos creator 的事件太隱含,對於 trace code 時較麻煩
- clarify Event Type,
button.on(Button.EventType.CLICK, ...) - get child (dynamic render) or add reference (
@property) for node getComponent(ClassName)notgetComponent('string'),避免更名的 side effect- component class script 在 inspector 中的順序放在最後
- typescript 使用 cocos template 建立
- creator 上數值盡量不要使用浮點數,一般浮點數效能較不好
- node 命名:一般使用小駝峰,掛有 class script 的使用大駝峰,以「元件 + 類型」命名,ex:
userAmountLabel,userAmountContainer - 避免濫用 Widget、Layout,過多位置計算有效能疑慮
- 避免過多的層級,Label 類元件可層級攤平,可以有效降低 drawcall,例如
userNameLabel、userAmountLabel可直接放在同一層 Node - 保持 prefab 修改最小單元化,可盡量避免 prefab 衝突
- 使用 table inject node 只封裝需要的元件,可減少遞迴次數
- 大圖或需要動態載入的圖檔,不需加入合圖內,不然一律會被載下來
- spine 圖檔或設定為不需合圖的圖檔,需要另外開啟「壓縮紋理」
Code Review Key Point
- Bugs and Side Effect
- 可維護性、可讀性
- Coding Style
- 設計要點
- 設計保持簡單,避免 over design
- 元件需抽離業務邏輯
- 清晰各類功能職責
- 元件抽離 base
Code Template
- 將團隊的 ts template 取代以下路徑的檔案,之後在 Creator 新增 TypeScript 即會套用:
1 | /Applications/Cocos/Creator/3.8.3/CocosCreator.app/Contents/Resources/resources/3d/engine/editor/assets/default_file_content/typescript/ts |
TypeScript Style Guidelines
- major reference to Google TypeScript Style Guide
- minor reference to Cocos 官方規範
Commit Rule & Git Branch Grouping
feat: new feature or change requestfix: bug fixeddocs: document or remarkstyle: coding style (no side effect)refactor: refactoring or structureperf: performance or fine tune parameterstest: test case or unit testbuild: build parameters, ex: path change etc…ci: CI/CD, ex: gitlab-cichore: tools or delete files etc…
Git branch grouping
1 | feat/xxx |
Commit rule
1 | feat: new game rule UI |
Develop Flow
- Create feature branch
- Bug fix or feature
- Run local client for QA testing
- MR to pre-release done
1 | feat/[feature] -> pre-release // MR |
TODO
- pre-commit hook (husky / lint-staged)
- commitlint / auto CHANGELOG
- Gitlab CI