Skip to content

場景加載與切換

場景預加載(director.preloadScene)

用於預先載入指定的場景。1

輸入參數 描述
sceneName 場景名稱。
onProgress 加載進度的 callback function。
onLoaded 加載完成的 callback function。
    director.preloadScene(sceneName, onProgress, onLoaded);

onProgress

加載進度的 callback。2

輸入參數 描述
completedCount 加載完畢的資源數量。
totalCount 資源總量。

Warning

totalCount 可能會提高,若將此數值用於計算載入進度時需注意。

onLoaded

加載完成的 callback。3

輸入參數 描述
error 例外資訊,正常載入時為 null。
sceneAsset 載入的場景資源。

場景切換(director.loadScene)4**

用於切換至指定場景,若場景尚未加載則會先載入再加載,因此不一定要先呼叫 director.preloadScene

輸入參數 描述
sceneName 場景名稱。
    director.loadScene(sceneName);

使用範例

export class Loading extends Component {
    @property(ProgressBar)
    private readonly progressBar: ProgressBar = null;

    @property(Label)
    private readonly progressLabel: Label = null;

    protected onLoad(): void {
        director.preloadScene('Game', this.onProgress.bind(this), this.onLoaded.bind(this));
    }

    private onProgress(completeCount: number, totalCount: number): void {
        const newPercent = completeCount / totalCount;
        // 避免 totalCount 提高而導致計算出來的 progress 反而變低
        if (newPercent > this.progressBar.progress) {
            this.progressBar.progress = newPercent;
            this.progressLabel.string = `${Math.trunc(newPercent * 100)}%`;
        }
    }

    private onLoaded(): void {
        // 預載完畢後,進入主遊戲場景
        director.loadScene('Game');
    }
}