squidGame/tgx-games-client/assets/module_basic/scene_loading/LoadingScene.ts

53 lines
1.7 KiB
TypeScript
Raw Permalink Normal View History

2025-03-07 13:33:59 +08:00
import { _decorator, assetManager, Component, director, game, Label, Prefab, Node, profiler, Sprite } from 'cc';
import { PREVIEW } from 'cc/env';
import { SceneDef } from '../../scripts/SceneDef';
import { tgxSceneUtil } from '../../core_tgx/tgx';
const { ccclass, property } = _decorator;
// ========== config begin =================
//the first scene after preloading completes.
const _loadingText = ['Loading.', 'Loading..', 'Loading...'];
@ccclass('LoadingScene')
export class LoadingScene extends Component {
@property(Label)
txtLoading: Label;
@property(Node)
loadingBar: Node;
start() {
// 初始化进度条和文本
// this.loadingBar.fillRange = 0.5;
this.loadingBar.setScale(0, 1, 1)
this.txtLoading.string = 'Loading... 0%';
this.loadTargetScene();
}
private loadTargetScene() {
let targetScene = SceneDef.Next_Scene;
console.log("要切换的场景 === ",targetScene);
let sceneName = typeof targetScene === 'string' ? targetScene : targetScene.name;
// 预加载目标场景
director.preloadScene(sceneName, (completedCount, totalCount) => {
let progress = completedCount / totalCount;
// this.loadingBar.fillRange = progress;
this.loadingBar.setScale(progress, 1, 1);
this.txtLoading.string = `Loading... ${Math.floor(progress * 100)}%`;
}, async () => {
// 预加载完成后,切换到目标场景
if(typeof targetScene === 'string'){
director.loadScene(targetScene);
}else{
await tgxSceneUtil.loadScene(targetScene)
}
});
}
}