新建framework/scripts/view/ViewBase.ts
,内容如下。主要是用作UI
和Panel
的基类,有一些基本的属性和方法。
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() { }
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。