プログラマーの卵の日記

備忘録になってます!小難しそうなことから超簡単なことまでやってます!!

【Unity】シーン移動と終了やりたい

構成


No.1 シーンの下準備
No.2 シーン移行する
No.3 非同期でシーン移行させる
No.4 アプリケーションを終了させる







    No.1 シーンの下準備


 シーンの切り替えにはまず、どのシーンを使うかを設定しないといけません。
先んじては切り替えたのためにシーンを二つ用意します。

f:id:yutateno:20191102203012p:plain



次に使うシーンを設定するためにビルド設定にて調整します。

f:id:yutateno:20191102203021p:plain



ここで追加するシーンを開いた状態でAdd Open Scenesをクリックします。

f:id:yutateno:20191102203031p:plain



このシーンに右に0と1が出てると思いますが、おそらく0がゲーム起動時に表示されるものだと思います。1以降はシーン移行をこちらで指定するのであまり気にしなくていいと思います。





    No.2 シーン移行する


 それではシーン移行したいです。今回はSampleSceneからNextSceneに移行するようにします。
そのためにSampleSceneの方にGameObjectとSampleSceneScriptを作成します。
ここで分かると思いますが、シーン移行はスクリプトで制御します。

f:id:yutateno:20191102203044p:plain



f:id:yutateno:20191102203053p:plain



次にそのスクリプトでZキーを押したらシーンを移行するようなコードを書きます。


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class SampleSceneScript : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetKey(KeyCode.Z))
        {
            SceneManager.LoadScene("Next Scene");
        }
    }
}






    No.3 非同期でシーン移行させる


 先ほどのロードでもいいのですが、基本的に3Dゲームはロードするオブジェクトが多くなりがちになってロードするものが増えてしまいます。
そういった場合、全部を一気にロードするとゲームがフリーズしたような状態になってしまいます。その対策として基本的に3Dゲームでは非同期ロードを行うようになっています。
なので非同期ロードをするためのコードを以下に書きます。


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class SampleSceneScript : MonoBehaviour
{
    private AsyncOperation m_asyncEnd;
    private bool m_nextLoadStart;


    // Start is called before the first frame update
    void Start()
    {
        m_nextLoadStart = false;
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetKey(KeyCode.Z) && !m_nextLoadStart)
        {
            m_asyncEnd = SceneManager.LoadSceneAsync("Next Scene");
            m_asyncEnd.allowSceneActivation = false;
            m_nextLoadStart = true;
        }
        if(Input.GetKey(KeyCode.X) && m_asyncEnd.progress >= 0.9f)
        {
            m_asyncEnd.allowSceneActivation = true;
        }
        if(m_nextLoadStart)
        {
            Debug.Log(m_asyncEnd.progress >= 0.9f ? "ロード終了" : "ロード中");
        }

        if(Input.GetKey(KeyCode.Escape))
        {
            Application.Quit();
        }
    }
}







    No.4 アプリケーションを終了させる


 終了するのもスクリプトで行うので先ほどのスクリプトを修正して以下のように知れやれば終わりです。
ただ、これはアプリケーションの終了なのでエディターの終了などは行えないので実際に挙動を確認することはビルドしたものじゃないとできないです。


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class SampleSceneScript : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetKey(KeyCode.Z))
        {
            SceneManager.LoadScene("Next Scene");
        }

        if(Input.GetKey(KeyCode.Escape))
        {
            Application.Quit();
        }
    }
}