Files
mp-xieyingeng/assets/scripts/core/BaseView.ts
richarjiang 0bda6904fa feat: 添加微信SDK和关卡页面,重构预制体结构
- 新增 WxSDK 微信SDK工具类
- 新增 PageLevel 关卡选择页面组件
- 将 prefabs 目录从 resources 移至根目录
- 更新 ViewManager 支持预制体属性引用
- 添加 BaseView 页面基类

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-11 10:35:21 +08:00

132 lines
2.8 KiB
TypeScript

import { _decorator, Component, Prefab } from 'cc';
const { ccclass } = _decorator;
/**
* 页面配置接口
*/
export interface ViewConfig {
prefab: Prefab; // 预制体引用(主包资源)
cache?: boolean; // 是否缓存页面,默认 true
zIndex?: number; // 层级,默认 0
}
/**
* 页面打开选项
*/
export interface ViewOptions {
params?: any; // 传递给页面的参数
onComplete?: (view: BaseView) => void;
onError?: (err: Error) => void;
}
/**
* 页面基类
* 所有页面组件应继承此类,实现生命周期方法
*/
@ccclass('BaseView')
export class BaseView extends Component {
/** 页面唯一标识 */
viewId: string = '';
/** 页面配置 */
config: ViewConfig | null = null;
/** 是否正在显示 */
isShowing: boolean = false;
/** 传递给页面的参数 */
protected _params: any = null;
/**
* 设置页面参数
*/
setParams(params: any): void {
this._params = params;
}
/**
* 获取页面参数
*/
getParams(): any {
return this._params;
}
/**
* 页面加载时调用(首次创建时)
* 子类应重写此方法
*/
onViewLoad(): void {
// 子类实现
}
/**
* 页面显示时调用(每次打开时)
* 子类应重写此方法
*/
onViewShow(): void {
// 子类实现
}
/**
* 页面隐藏时调用(关闭或被其他页面覆盖时)
* 子类应重写此方法
*/
onViewHide(): void {
// 子类实现
}
/**
* 页面销毁时调用
* 子类应重写此方法
*/
onViewDestroy(): void {
// 子类实现
}
// ========== 内部方法,由 ViewManager 调用 ==========
/**
* 内部方法:执行显示逻辑
*/
_doShow(): void {
if (this.isShowing) return;
this.isShowing = true;
this.node.active = true;
this.onViewShow();
}
/**
* 内部方法:执行隐藏逻辑
*/
_doHide(): void {
if (!this.isShowing) return;
this.isShowing = false;
this.onViewHide();
this.node.active = false;
}
/**
* 内部方法:执行销毁逻辑
*/
_doDestroy(): void {
// 标记已销毁,防止 onDestroy 中重复调用
this._destroyed = true;
this.node.destroy();
}
/** 是否已标记销毁 */
private _destroyed: boolean = false;
// ========== Cocos 生命周期 ==========
protected onDestroy(): void {
// 仅在未被 _doDestroy 调用时执行生命周期
if (!this._destroyed) {
if (this.isShowing) {
this.onViewHide();
}
}
this.onViewDestroy();
}
}