Unity/シーン遷移

Last-modified: 2020-02-22 (土) 15:38:04

考慮しないといけない要素

  • シーン読み込み時間
  • ロード待ちを許容するか

メモ

  • AsyncOperation.progress の最大値は0.9
    • なのでprogressが1になるまで待ってはいけない。
  • 処理される順番はシーン読み込み完了 → 前シーンのオブジェクトDestroy → 新シーンのオブジェクトAwake

実装方法

最低限のシーン遷移

public void LoadSimple(string nextSceneName)
{
    SceneManager.LoadSceneAsync(nextSceneName).AsObservable()
        .Subscribe(_ => Debug.Log($"Scene {nextSceneName} Load Completed"));
}

Progressを監視したいする場合はIProgress<float>を渡してあげればよい。
UniRx7.1.0ではReport()が呼ばれるのはAsyncOperationExtensions.csの38行目。

private class Reporter : IProgress<float>
{
    Action<float> report;
    public Reporter(Action<float> report) => this.report = report;
    public void Report(float value) => report(value);
}

public void LoadWithReporter(string nextSceneName)
{
    SceneManager.LoadSceneAsync(sceneName)
        .AsObservable(new Reporter(x => Debug.Log($"{sceneName} Loading {x*100}%")))
        .Subscribe(_ => Debug.Log($"{sceneName} Load Completed"));
}

なうろーでぃんぐ画面を出す

なうろーでぃんぐシーンにはProgressBarなんかを用意しておくといいね。

あらかじめ裏で読み込んでおく

そもそもシーン遷移をしない