新建framework/scripts/App.ts
,用于初始化框架,内容如下。因为Cocos Creator 2.x
版本不能直接在编辑器运行,导致不能实时看到运行中的对象节点信息,所以这里写了个showNode
函数用来打印Canvas
节点信息。
import LayerMgr from "./view/LayerMgr"
export default class App {
// 初始化
public static init() {
this.initFramework()
this.showNode(cc.Canvas.instance.node)
}
// 递归显示节点信息
public static showNode(node: cc.Node, sep: string = '', depth: number = 0, depthMax: number = 4) {
console.log(sep + node.name + ':' + node.zIndex + ':' + node.active)
if (node.childrenCount <= 0) return
depth++
if (depth >= depthMax) return
node.children.forEach(child => this.showNode(child, sep + '\t', depth, depthMax))
}
// 初始化框架
private static initFramework() {
LayerMgr.init()
}
}
新建scripts/Main.ts
,用于启动程序,内容如下。
import App from "../framework/scripts/App"
const { ccclass } = cc._decorator
@ccclass
export default class Main extends cc.Component {
protected onLoad(): void {
App.init()
}
}
把Main.ts
节本挂载到场景Canvas
节点上,然后运行程序,可以看到UI
、Panel
、Top
这 3 个层级已经创建了。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。