FarmRPG　Unity Scene Management Part1 Sceneを読み込む

# 概要
今回はスクリプトからシーンを読み込む処理を実装します。
下gifアニメは実装後の様子です。

![0615.gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/23fa018f-792a-ab13-1eb2-00274c0bbd9e.gif)


# 開発環境

IDE:Rider
Unity:2020.3.42(LTS)
OS:Windows10

# UnityEditor上の設定

## Build SettingでSceneを加える

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/c2076764-0f6c-1a40-868c-9beb3fa97984.png)



### オブジェクトを作成して　SceneControllerManagerスクリプトをアタッチする

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/bed402f1-be15-3cbf-46bb-abc4b3421e75.png)



# 実装のポイント
新規クラス　SceneControllerManagerを作成しStartメソッド内でSceneを読み込む処理を実装します。Sceneを読み込む関数がコルーチンですが、ドキュメントにサンプルがあるので詰まりにくいです。

# コード部分

## SceneControllerManager

```diff_c_sharp:SceneControllerManager.cs

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEngine.SceneManagement;

public class SceneControllerManager :SingletonMonobehaviour<SceneControllerManager>
{
    public SceneName startingSceneName;
    
    private IEnumerator Start()
    {
        yield return StartCoroutine(LoadSceneAndSetActive(startingSceneName.ToString()));
        
    }

    private IEnumerator Fade(float finalAlpha)
    {
        yield return null;
    }
    
    
    private IEnumerator LoadSceneAndSetActive(string sceneName)
    {
        yield return SceneManager.LoadSceneAsync(sceneName,LoadSceneMode.Additive);

        Scene newlyLoadedScene = SceneManager.GetSceneAt(SceneManager.sceneCount - 1);
        SceneManager.SetActiveScene(newlyLoadedScene);
    }
    
}





```

## Enums

```diff_c_sharp:Enums.cs
public enum SceneName
{
    Scene1_Farm,
}

```





# 参考

## C#

### Enum.ToString()

https://learn.microsoft.com/ja-jp/dotnet/api/system.enum.tostring?view=net-7.0

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/11dc8ccf-2d65-e1e8-f425-c07b99d5dd23.png)


### IEnumerator

https://learn.microsoft.com/ja-jp/dotnet/api/system.collections.ienumerator?view=net-7.0

## Unity Editor コンポーネント
### BuildSetting

https://docs.unity3d.com/ja/2018.4/Manual/BuildSettings.html

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/12f78352-4770-abb0-bea1-5c88871d2b92.png)






## Unity スクリプト
### StartCoroutine

https://docs.unity3d.com/ja/2018.4/ScriptReference/MonoBehaviour.StartCoroutine.html

### SceneManager.LoadSceneAsync

https://docs.unity3d.com/jp/2018.4/ScriptReference/SceneManagement.SceneManager.LoadSceneAsync.html

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/60da6abc-4815-7aa2-19a5-7becc4fc6bd1.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/b91da939-5688-7100-ce52-c513eb4f8d64.png)


### SceneManager.GetSceneAt

https://docs.unity3d.com/ja/2018.4/ScriptReference/SceneManagement.SceneManager.GetSceneAt.html

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/7004bb2c-151f-22b6-2ddd-88f4f4c05831.png)


### SceneManager.SetActive

https://docs.unity3d.com/ja/2021.1/ScriptReference/SceneManagement.SceneManager.SetActiveScene.html

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/82a282d9-4f45-3084-d5c3-34aa29a0fdf0.png)


## その他

Section9 36 Create A Scene

https://www.udemy.com/course/unity-2d-game-developer-course-farming-rpg/learn/lecture/19896566#content

github コミット分（個人確認用　privateなので見れません）

https://github.com/RYA234/LearnFarmRPG/commit/cafaff1839a34ac61324167fc71bb51cf8680e08