Cocos Creator 开发助手
使用场景
辅助Cocos Creator游戏开发,提供常用组件和最佳实践。
项目结构
├── assets/
│ ├── scenes/ # 场景文件
│ ├── scripts/ # 脚本文件
│ │ ├── manager/ # 管理器
│ │ ├── ui/ # UI组件
│ │ ├── game/ # 游戏逻辑
│ │ └── utils/ # 工具函数
│ ├── prefabs/ # 预制体
│ ├── textures/ # 纹理
│ ├── audio/ # 音频
│ └── animations/ # 动画
├── settings/ # 项目设置
└── native/ # 原生平台配置
常用组件模板
单例管理器
import { _decorator, Component } from 'cc';
const { ccclass, property } = _decorator;
@ccclass('GameManager')
export class GameManager extends Component {
private static _instance: GameManager = null;
public static get instance(): GameManager {
return this._instance;
}
onLoad() {
if (GameManager._instance) {
this.node.destroy();
return;
}
GameManager._instance = this;
// 不销毁节点
// cc.game.addPersistRootNode(this.node);
}
}
事件管理器
import { EventTarget } from 'cc';
export class EventManager {
private static _eventTarget = new EventTarget();
static on(event: string, callback: Function, target?: any) {
this._eventTarget.on(event, callback, target);
}
static off(event: string, callback: Function, target?: any) {
this._eventTarget.off(event, callback, target);
}
static emit(event: string, ...args: any[]) {
this._eventTarget.emit(event, ...args);
}
}
对象池
import { NodePool, Prefab, instantiate } from 'cc';
export class PoolManager {
private static pools: Map<string, NodePool> = new Map();
static getPool(name: string, prefab: Prefab): NodePool {
if (!this.pools.has(name)) {
this.pools.set(name, new NodePool(name));
}
return this.pools.get(name);
}
static spawn(name: string, prefab: Prefab) {
const pool = this.getPool(name, prefab);
if (pool.size() > 0) {
return pool.get();
}
return instantiate(prefab);
}
static recycle(name: string, node: any) {
const pool = this.pools.get(name);
if (pool) {
pool.put(node);
}
}
}
构建优化
- 开启纹理压缩(ETC2/ASTC)
- 合并图集减少DrawCall
- 使用分包加载
- 音频使用合适的格式和比特率
- 移除未使用的资源
