import { _decorator, assetManager, Component, director, ProgressBar } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('LoadScene')
export class LoadScene extends Component {
    private progressBar: ProgressBar = null;

    start() {
        this.progressBar = this.node.getComponentInChildren(ProgressBar);
        this.loadSceneWithProgress("GameScene");
    }

    loadSceneWithProgress(sceneName: string) {
        assetManager.loadBundle("Scene", (err, bundle) => {
            if (err) {
                console.log("加载失败", err);
                return;
            }
            bundle.loadScene(
                sceneName,
                (finished: number, total: number) => {
                    this.progressBar.progress = finished / total;
                },
                (err, sceneAsset) => {
                    if (err) {
                        console.log("加载场景失败", err);
                        return;
                    }
                    this.node.active = false;
                    director.runSceneImmediate(sceneAsset);
                }
            );
        });
    }
}


