SceneManager클래스- LoadSceneAsync, LoadSceneMode.Additve/Single
2023. 8. 30.

SceneManager 클래스는 동적으로 씬을 생성, 해제하고나 기존에 만들어진 씬을 호출하는 여러 메서드를 제공한다.

SceneManager 클래스의 주요 메서드

씬을 로드할 때 옵션)

 

LoadSceneMode.Single - 기존에 로드된 씬을 모두 삭제하고 새로운 씬 로드

LoadSceneMode.Additive - 기존 씬을 삭제하지 않고 추가해서 새로운 씬을 로드

 

동기 vs 비동기

동기/ 비동기

동기- 하나 시작하고 완료되면 다음 작업을 시작. 요청을 보낸뒤에 결과를 받아야 다음 작업을 시작한다.

비동기- 작업이 끝나면 처리결과를 반환하는 콜백 함수가 필요하다. 

https://docs.unity3d.com/ScriptReference/AsyncOperation.html

 

Unity - Scripting API: AsyncOperation

AsyncOperation class in UnityEngine / Inherits from:YieldInstruction / Implemented in:UnityEngine.CoreModule Description Asynchronous operation coroutine. Properties allowSceneActivationAllow Scenes to be activated as soon as it is ready. isDoneHas the ope

docs.unity3d.com

AsyncOperation 클래스의 프로퍼티

 

-App씬을 실행하면 타이틀 씬으로 넘어가고, 버튼을 누르면 게임씬으로 넘어간다.

App.cs



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

public class AppMain : MonoBehaviour
{
    private string version = "1.0";
    // Start is called before the first frame update
    void Awake()
    {
        DontDestroyOnLoad(this.gameObject);
    }

    
    void Start()
    {
        //동기
        //SceneManager.LoadScene("TitleMain");
        //TitleMain title = GameObject.FindObjectOfType<TitleMain>();
        //title.Init();

        //AsyncOperation oper = SceneManager.LoadSceneAsync("Title");//비동기
        ////비동기: 언제 로드될지 모름
        //oper.completed += this.OnLoadCompleteTitleScene;
        //Debug.Log("Hello world");

        //비동기 
        AsyncOperation oper = SceneManager.LoadSceneAsync("Title");
        oper.completed += OnLoadCompleteTitleScene;
    }

    private void OnLoadCompleteTitleScene(AsyncOperation obj)
    {
        Debug.Log("Title load Complete!");
        TitleMain titleMain = GameObject.FindObjectOfType<TitleMain>();
        //titleMain.Init();
        titleMain.onChangeScene = (nickname) => {
            this.LoadGameScene(nickname);
        };
        Debug.LogFormat("titleMain: {0}", titleMain);
        titleMain.Init(this.version);
    }

    private void LoadGameScene(string nickname)
    {
        //비동기 로드
        AsyncOperation oper = SceneManager.LoadSceneAsync("Game");
        oper.completed += (ao) => {
            GameMain gameMain = GameObject.FindObjectOfType<GameMain>();
            gameMain.Init(nickname);
        };
    }
}

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class TitleMain : MonoBehaviour
{
    [SerializeField]
    private Button btn;
    public System.Action<string> onChangeScene;

    private void Awake()
    {
        Debug.Log("[TitleMain] Awake");
    }

    private void OnEnable()
    {
        Debug.Log("[TitleMain] OnEnable");
    }

    private void Start()
    {
        Debug.Log("[TitleMain] Start");
    }

    public void Init(string version)
    {
        Debug.Log("[TitleMain] <color=yellow>Init</color>");
        Debug.LogFormat("version: {0}", version);

        this.btn.onClick.AddListener(() => {
            this.onChangeScene("홍길동");
        });
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameMain : MonoBehaviour
{
    private void Awake()
    {
        Debug.Log("[GameMain] Awake");
    }

    private void OnEnable()
    {
        Debug.Log("[GameMain] OnEnable");
    }

    private void Start()
    {
        Debug.Log("[GameMain] Start");
    }

    public void Init(string nickname)
    {
        Debug.LogFormat("[GameMain] <color=yellow>Init</color> : {0}", nickname);
    }
}
myoskin