cocos creator从零开发简单框架(05)-ViewBase基类

新建framework/scripts/view/ViewBase.ts,内容如下。主要是用作UIPanel的基类,有一些基本的属性和方法。

export default class ViewBase {
    /** 资源 Bundle 名, 如果为空加载 resources 资源 */
    public bundleName: string
    /** 资源路径 */
    public skinPath: string
    /** 资源实例化后的对象 */
    public skin: cc.Node
    /** 是否缓存(缓存为 true 只隐藏 skin 而不销毁) */
    public cache: boolean = false

    protected _args: any[] = []


    // 获取类名(项目发布后类名是混淆的)
    public getClassName() {
        const [className] = this.skinPath.split('/').slice(-1)
        return className
    }


    // 初始化
    public init(args: any[]) {
        this._args = args

        this.onInit()
    }

    // 初始化完成
    public initDone() {
        const buttons = this.skin.getComponentsInChildren(cc.Button)

        for (const button of buttons) {
            if (!button.node.name.startsWith('Btn')) {
                continue
            }

            button.node.on('click', (_event: cc.Button) => {
                this.onButtonClick(button.node)
            })
        }

        this.onInitDone()
    }

    // 重置
    public reset(args: any[]) {
        this._args = args

        this.onReset()
    }

    // 开始显示
    public showing() {
        this.onShowing()
    }

    // 显示完成
    public showed() {
        this.onShowed()
    }

    // 更新 view
    public updateView(args: any[]) {
        this.onUpdateView(args)
    }

    // 开始隐藏
    public hiding() {
        this.onHiding()
    }

    // 隐藏完成
    public hided() {
        this.onHided()
    }

    // 销毁
    public destroy() {
        this.onDestroy()
        this.skin.destroy()
    }


    protected onInit() { }
    protected onInitDone() { }
    protected onReset() { }
    protected onButtonClick(_button: cc.Node) { }
    protected onShowing() { }
    protected onShowed() { }
    protected onUpdateView(_args: any[]) { }
    protected onHiding() { }
    protected onHided() { }
    protected onDestroy() { }
}
本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!