1.Unity GameSceneManager

개발 일지/Move Cube 2020. 8. 17. 14:39

1.GameSceneManager

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
 
public class GameSceneManager
{
    private static GameSceneManager instance;
    private List<string> listBeforeScene;
    private string caller;
 
    private GameSceneManager()
    {
        this.listBeforeScene = new List<string>();
    }
 
    public static GameSceneManager GetInstance()
    {
        if(GameSceneManager.instance == null)
        {
            GameSceneManager.instance = new GameSceneManager();
        }
        return GameSceneManager.instance;
    }
 
    #region 로드씬
    public void LoadScene(int id, int stageLevel = 0)
    {
        this.listBeforeScene = new List<string>();
 
        var sceneName = ((SceneEnums.sceneName)id).ToString();
        var operation = SceneManager.LoadSceneAsync(sceneName);
 
        Debug.LogFormat("불러온 씬 : {0}", sceneName);
 
        operation.completed += (asyncOper) =>
        {
            SceneManager.SetActiveScene(SceneManager.GetSceneByName(sceneName));
            this.InitScene((SceneEnums.sceneName)id, "", stageLevel);
        };   
    }
    #endregion
 
    #region 언로드씬
    public void UnloadScene()
    {
        var operation = SceneManager.UnloadSceneAsync(SceneManager.GetActiveScene());
 
        operation.completed += (asyncOper) =>
        {
            SceneManager.SetActiveScene(SceneManager.GetSceneByName(this.listBeforeScene[this.listBeforeScene.Count-1]));
            this.listBeforeScene.RemoveAt(this.listBeforeScene.Count - 1);
        };
    }
 
    public void UnloadAllScene()
    {
        foreach(var name in this.listBeforeScene)
        {
            SceneManager.UnloadSceneAsync(name);
        }
    }
    #endregion
 
    #region 씬초기화
    //Scene을 초기화하는 과정이므로 Init메서드는 Scene의 대표 오브젝트 스크립트에서 정의
    //caller는 LoadScene 메소드를 호출한 Scene을 뜻함
    private void InitScene(SceneEnums.sceneName sceneName, string caller = ""int stageLevel = 0)
    {
        switch(sceneName)
        {
            case SceneEnums.sceneName.Logo:
                {
                    var go = GameObject.FindObjectOfType<SceneLogo>();
                    go.Init();
                    break;
                }
            case SceneEnums.sceneName.Title:
                {
                    var go = GameObject.FindObjectOfType<SceneTitle>();
                    go.Init();
                    break;
                }
            case SceneEnums.sceneName.Loading:
                {
                    var go = GameObject.FindObjectOfType<SceneLoading>();
                    go.Init();
                    break;
                }
            case SceneEnums.sceneName.Stage:
                {
                    var go = GameObject.FindObjectOfType<SceneStage>();
                    go.InitStage(stageLevel);
                    break;
                }
            default:
                break;
        }
    }
    #endregion
}
 
cs


2.SceneEnums


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class SceneEnums
{
    //Scene Enums
    public enum sceneName
    {
        None = -1,
        App = 0,
        Loading = 1,
        Logo = 2,
        Title = 3,
        Stage = 4
    }
}
 
cs


'개발 일지 > Move Cube' 카테고리의 다른 글

6.Google Play Beta.ver//구글 플레이 베타버전 업로드  (0) 2020.08.17
5.Stage, Cube, CubeMove  (0) 2020.08.17
4.Title/StageSelect  (0) 2020.08.17
3.App -> Logo  (0) 2020.08.17
2.Unity Info관리  (0) 2020.08.17
: