Unity

[Unity] LoadingScene 만들기

촙촙쓰 2020. 2. 17. 01:54

씬에서 씬으로 전환할 때 로딩화면을 사용하지 않으면 부자연스러울때가 있다. 혹은 로딩시간이 오래걸릴 때 로딩바를 넣거나, 게임 팁을 추가하거나 할 수 있다.

 

로딩씬을 따로 추가하여 a씬에서 b씬으로 넘어갈때  a씬->로딩씬 -> b씬으로 이동하여 자연스러운 씬의 이동을 노려보자

 

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
public class LoadingSceneManager : MonoBehaviour
{
    public static string nextScene;
 
 
    [SerializeField]
 
    Image progressBar;
 
 
    private void Start()
    {
        StartCoroutine(LoadScene());
    }
 
 
 
    public static void LoadScene(string sceneName)
    {
 
        nextScene = sceneName;
 
        SceneManager.LoadScene("LoadingScene");
 
    }
 
 
 
    IEnumerator LoadScene()
    {
 
        yield return null;
 
        AsyncOperation op = SceneManager.LoadSceneAsync(nextScene);
 
        op.allowSceneActivation = false;
 
        float timer = 0.0f;
 
        while (!op.isDone)
        {
 
            yield return null;
 
 
 
            timer += Time.deltaTime;
 
 
 
            if (op.progress < 0.9f)
            {
 
                progressBar.fillAmount = Mathf.Lerp(progressBar.fillAmount, op.progress, timer);
 
                if (progressBar.fillAmount >= op.progress)
                {
                    timer = 0f;
                }
 
            }
            else
            {
                progressBar.fillAmount = Mathf.Lerp(progressBar.fillAmount, 1f, timer);
 
                if (progressBar.fillAmount == 1.0f)
                {
                    op.allowSceneActivation = true;
                    yield break;
                }
            }
        }
    }
}
 

 

 

 위 스트립트는 로딩씬 내에 오브젝트를 하나 생성하여 붙여넣는다. 예제에서는 로딩바만 있는데 이미지를 추가하거나, 로딩애니메이션등을 넣는것도 가능하다.

 

LoadScene 함수는 스태틱이므로 사용할때는 다음과 같이!

LoadingSceneManager.LoadScene("BScene");

 

이제, LoadingScene이라는 이름의 씬을 하나 만든다음, 로딩씬을 구성하면된다.