import { _decorator, Component, Prefab, AudioClip } from 'cc'; import { ViewManager } from './scripts/core/ViewManager'; import { ToastManager } from './scripts/utils/ToastManager'; import { AudioManager } from './scripts/utils/AudioManager'; import { ShareLaunchHandler } from './scripts/utils/ShareLaunchHandler'; const { ccclass, property } = _decorator; /** * 主入口脚本 * 负责初始化 ViewManager 并注册页面 */ @ccclass('main') export class main extends Component { @property({ type: Prefab, tooltip: '首页预制体' }) pageHomePrefab: Prefab | null = null; @property({ type: Prefab, tooltip: '关卡页面预制体' }) pageLevelPrefab: Prefab | null = null; @property({ type: Prefab, tooltip: '写关卡页面预制体' }) pageWriteLevelsPrefab: Prefab | null = null; @property({ type: Prefab, tooltip: '预览试卷页面预制体' }) pagePreviewLevelsPrefab: Prefab | null = null; @property({ type: Prefab, tooltip: '挑战数据页面预制体' }) pagePKDataPrefab: Prefab | null = null; @property({ type: Prefab, tooltip: '挑战详情页面预制体' }) pagePKDetailPrefab: Prefab | null = null; @property({ type: Prefab, tooltip: '挑战结算页面预制体' }) pagePKEndPrefab: Prefab | null = null; @property({ type: Prefab, tooltip: 'Toast 预制体' }) toastPrefab: Prefab | null = null; @property({ type: AudioClip, tooltip: '通用按钮点击音效' }) buttonClickAudio: AudioClip | null = null; /** * onLoad 比 start 更早执行 * 确保 ViewManager 在 PageLoading.start() 之前初始化 */ onLoad() { this._initViewManager(); } /** * 初始化页面管理器 */ private _initViewManager(): void { // 初始化 ViewManager,绑定 Canvas 作为页面容器 ViewManager.instance.init(this.node); // 注册页面配置(通过编辑器属性引用预制体) if (this.pageHomePrefab) { ViewManager.instance.register('PageHome', { prefab: this.pageHomePrefab, cache: true, zIndex: 0 }); } if (this.pageLevelPrefab) { ViewManager.instance.register('PageLevel', { prefab: this.pageLevelPrefab, cache: true, zIndex: 1 }); } if (this.pageWriteLevelsPrefab) { ViewManager.instance.register('PageWriteLevels', { prefab: this.pageWriteLevelsPrefab, cache: true, zIndex: 2 }); } if (this.pagePreviewLevelsPrefab) { ViewManager.instance.register('PagePreviewLevels', { prefab: this.pagePreviewLevelsPrefab, cache: true, zIndex: 3 }); } if (this.pagePKDataPrefab) { ViewManager.instance.register('PagePKData', { prefab: this.pagePKDataPrefab, cache: true, zIndex: 3 }); } if (this.pagePKDetailPrefab) { ViewManager.instance.register('PagePKDetail', { prefab: this.pagePKDetailPrefab, cache: true, zIndex: 4 }); } if (this.pagePKEndPrefab) { ViewManager.instance.register('PagePKEnd', { prefab: this.pagePKEndPrefab, cache: true, zIndex: 3 }); } // 初始化 Toast 管理器 if (this.toastPrefab) { ToastManager.instance.init(this.toastPrefab, this.node); } AudioManager.instance.init(this.buttonClickAudio, this.node); // 注册 wx.onShow / wx.onHide: // 用户把小游戏退到后台后再点击好友分享卡片,能拿到最新的 shareCode 并直达分享挑战关卡。 // 必须在 PageLoading 跑之前注册,这样初始 launch 中的 shareCode 也会被作为种子记下。 ShareLaunchHandler.instance.init(); } }