133 lines
3.8 KiB
TypeScript
133 lines
3.8 KiB
TypeScript
import { _decorator, assetManager, Component, director, game, Label, Prefab, Node, profiler } from 'cc';
|
|
import { tgxModuleContext, tgxUIMgr } from '../core_tgx/tgx';
|
|
import { GameUILayers, GameUILayerNames } from '../scripts/GameUILayers';
|
|
|
|
import { ModuleDef } from '../scripts/ModuleDef';
|
|
import { SceneDef } from '../scripts/SceneDef';
|
|
|
|
import { PREVIEW } from 'cc/env';
|
|
|
|
const { ccclass, property } = _decorator;
|
|
|
|
// ========== config begin =================
|
|
//the first scene after preloading completes.
|
|
const _FPS = 61;
|
|
const _defaultModule = ModuleDef.BASIC;
|
|
const _firstScene = SceneDef.LOGIN;
|
|
const _preloadBundles = [ModuleDef.BASIC, ModuleDef.Arean];
|
|
const _preloadScenes = [];
|
|
|
|
const _preloadRes = [
|
|
{ bundle: ModuleDef.BASIC, url: 'ui_alert/UI_Alert', type: 'prefab' },
|
|
{ bundle: ModuleDef.BASIC, url: 'ui_waiting/UI_Waiting', type: 'prefab' },
|
|
// { bundle: ModuleDef.BASIC, url: 'ui_login/ui_login', type: 'prefab' },
|
|
];
|
|
|
|
// ========= config end =====================
|
|
|
|
// if (_preloadScenes.indexOf(_firstScene) == -1) {
|
|
// _preloadScenes.push(_firstScene);
|
|
// }
|
|
|
|
for (let i = 0; i < _preloadScenes.length; ++i) {
|
|
let sceneInfo = _preloadScenes[i];
|
|
let idx = _preloadBundles.indexOf(sceneInfo.bundle);
|
|
if (idx == -1) {
|
|
_preloadBundles.push(sceneInfo.bundle);
|
|
}
|
|
_preloadRes.push({ bundle: sceneInfo.bundle, url: sceneInfo.name, type: 'scene' });
|
|
}
|
|
|
|
const _loadingText = ['Loading.', 'Loading..', 'Loading...'];
|
|
const _totalNum = _preloadBundles.length + _preloadRes.length;
|
|
|
|
@ccclass('Start')
|
|
export class Start extends Component {
|
|
@property(Label)
|
|
txtLoading: Label;
|
|
|
|
@property(Prefab)
|
|
uiCanvasPrefab: Prefab;
|
|
|
|
@property(Node)
|
|
loadingBar: Node;
|
|
|
|
private _percent: string = '';
|
|
private _numCurrentLoaded = 0;
|
|
start() {
|
|
/**
|
|
* @en display stats in preview mode
|
|
* @zh 预览调试时,默认显示性能统计面板
|
|
*/
|
|
if(PREVIEW){
|
|
profiler.showStats();
|
|
}
|
|
tgxModuleContext.setDefaultModule(_defaultModule);
|
|
|
|
game.frameRate = _FPS;
|
|
tgxUIMgr.inst.setup(this.uiCanvasPrefab, GameUILayers.NUM, GameUILayerNames);
|
|
|
|
this.preloadBundle(0);
|
|
}
|
|
|
|
onResLoaded() {
|
|
this._numCurrentLoaded++;
|
|
this._percent = ~~(this._numCurrentLoaded / _totalNum * 100) + '%';
|
|
}
|
|
|
|
preloadBundle(idx: number) {
|
|
assetManager.loadBundle(_preloadBundles[idx], null, (err, bundle) => {
|
|
idx++;
|
|
this.onResLoaded();
|
|
if (idx < _preloadBundles.length) {
|
|
this.preloadBundle(idx);
|
|
console.log("加载 bundle: " + _preloadBundles[idx])
|
|
}
|
|
else {
|
|
this.preloadRes(0);
|
|
}
|
|
});
|
|
}
|
|
|
|
preloadRes(idx: number) {
|
|
let res = _preloadRes[idx];
|
|
let bundle = assetManager.getBundle(res.bundle);
|
|
|
|
let onComplete = () => {
|
|
idx++;
|
|
this.onResLoaded();
|
|
if (idx < _preloadRes.length) {
|
|
this.preloadRes(idx);
|
|
}
|
|
else {
|
|
this.onPreloadingComplete();
|
|
}
|
|
}
|
|
if (bundle) {
|
|
if (res.type == 'prefab') {
|
|
bundle.preload(res.url, Prefab, onComplete);
|
|
}
|
|
else if (res.type == 'scene') {
|
|
console.log("res.url === ",res)
|
|
bundle.preloadScene(res.url, onComplete);
|
|
}
|
|
}
|
|
}
|
|
|
|
async onPreloadingComplete() {
|
|
director.loadScene(_firstScene.name);
|
|
}
|
|
|
|
update(deltaTime: number) {
|
|
if (this._percent) {
|
|
this.txtLoading.string = 'Loading...' + this._percent;
|
|
}else {
|
|
let idx = Math.floor(game.totalTime / 1000) % 3;
|
|
this.txtLoading.string = _loadingText[idx];
|
|
}
|
|
this.loadingBar.setScale(this._numCurrentLoaded / _totalNum, 1, 1);
|
|
}
|
|
}
|
|
|
|
|