头图

新建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节点上,然后运行程序,可以看到UIPanelTop这 3 个层级已经创建了。

挂载Main脚本

运行游戏


mirahs
1 声望0 粉丝