- 添加 ProgressTips 节点到 PageLoading 展示加载状态消息 - 连接 statusLabel 到 ProgressTips 组件 - LevelDataManager 添加 API 请求重试机制(重试 2 次) - 优化进度消息:正在请求服务端数据、正在加载游戏必备资源等 - 初始化失败时显示"网络异常,请重新打开游戏" Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
88 lines
2.2 KiB
TypeScript
88 lines
2.2 KiB
TypeScript
import { _decorator, Component, ProgressBar, Label } from 'cc';
|
|
import { ViewManager } from './scripts/core/ViewManager';
|
|
import { LevelDataManager } from './scripts/utils/LevelDataManager';
|
|
const { ccclass, property } = _decorator;
|
|
|
|
/**
|
|
* 页面加载组件
|
|
* 负责预加载资源并显示加载进度
|
|
*/
|
|
@ccclass('PageLoading')
|
|
export class PageLoading extends Component {
|
|
@property(ProgressBar)
|
|
progressBar: ProgressBar | null = null;
|
|
|
|
@property(Label)
|
|
statusLabel: Label | null = null;
|
|
|
|
start() {
|
|
this._startPreload();
|
|
}
|
|
|
|
/**
|
|
* 开始预加载
|
|
*/
|
|
private async _startPreload(): Promise<void> {
|
|
// 初始化进度条
|
|
if (this.progressBar) {
|
|
this.progressBar.progress = 0;
|
|
}
|
|
|
|
// 阶段1: 初始化 LevelDataManager (0-80%)
|
|
const success = await LevelDataManager.instance.initialize((progress, message) => {
|
|
this._updateProgress(progress);
|
|
this._updateStatusLabel(message);
|
|
});
|
|
|
|
if (!success) {
|
|
return;
|
|
}
|
|
|
|
// 阶段2: 预加载 PageHome (80-100%)
|
|
ViewManager.instance.preload('PageHome',
|
|
(progress) => {
|
|
this._updateProgress(0.8 + progress * 0.2);
|
|
this._updateStatusLabel('正在加载界面资源...');
|
|
},
|
|
() => {
|
|
this._onPreloadComplete();
|
|
}
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 更新进度条
|
|
*/
|
|
private _updateProgress(progress: number): void {
|
|
if (this.progressBar) {
|
|
this.progressBar.progress = progress;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 更新状态标签
|
|
*/
|
|
private _updateStatusLabel(message: string): void {
|
|
if (this.statusLabel) {
|
|
this.statusLabel.string = message;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 预加载完成回调
|
|
*/
|
|
private _onPreloadComplete(): void {
|
|
// 确保进度条显示完成
|
|
this._updateProgress(1);
|
|
this._updateStatusLabel('加载完成');
|
|
|
|
// 打开 PageHome
|
|
ViewManager.instance.open('PageHome', {
|
|
onComplete: () => {
|
|
// PageHome 打开成功后,销毁自身
|
|
this.node.destroy();
|
|
}
|
|
});
|
|
}
|
|
}
|