KDT Unity 5주차 팀 프로젝트
2024-02-02 TIL
BaseScene을 상속받은 로딩씬 구현
기본 BaseScene을 상속받아 해당 로딩중에 어드레서블로 가져오는 씬을 구현하였다.
해당 씬은 처음 게임을 시작할때 로딩바를 만들고 일반적으로는 로딩이 완료되었을 시 바로 다음 씬으로 이동한다.
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class LodingManager : BaseScene
{
private int _count;
private int _totalCount;
private string _key;
private bool _resourcesLoaded = false;
[SerializeField] private Image progressBar;
[SerializeField] private GameObject anyKeyDownTxt;
private float timer = 0.0f; // 페이크 로딩을 계산하기 위한 타이머 변수
protected override bool Initialize()
{
if (!base.Initialize()) return false;
Main.NextScene ??= "TitleScene";
// Main.Sound.PlayBGM("BGM");
Main.StageClear = Main.Data.Lord();
LoadResourcesAndScene();
return true;
}
private void LoadResourcesAndScene()
{
LoadResources();
}
private void LoadResources()
{
string loadName;
loadName = Main.NextScene == "TitleScene" ? Main.NextScene : "GameScene";
Main.Resource.LoadAllAsync<Object>($"{loadName}", (key, count, totalCount) =>
{
_key = key;
_count = count;
_totalCount = totalCount;
Debug.Log($"[{Main.NextScene}] Load asset {_key} ({_count}/{_totalCount})");
if (_count != _totalCount) return;
_resourcesLoaded = true; // 리소스 로딩 완료
LoadNextSceneAsync(); // 리소스 로딩 완료 후 씬 로딩 시작
});
}
private void LoadNextSceneAsync()
{
AsyncOperation sceneLoadOperation = SceneManager.LoadSceneAsync(Main.NextScene);
if(Main.FirstLord)sceneLoadOperation.allowSceneActivation = false;
StartCoroutine(UpdateLoadingProgress(sceneLoadOperation));
}
private IEnumerator UpdateLoadingProgress(AsyncOperation sceneLoadOperation)
{
while (!_resourcesLoaded || sceneLoadOperation.progress < 0.9f)
{
if(Main.FirstLord)progressBar.fillAmount = sceneLoadOperation.progress*0.1f; // 실제 로딩
yield return null;
}
if (Main.FirstLord)
{
// 페이크 로딩
while (progressBar.fillAmount < 1f)
{
timer += Time.unscaledDeltaTime;
progressBar.fillAmount = Mathf.Lerp(0.09f, 1f, timer);
yield return null;
}
anyKeyDownTxt.SetActive(true);
// 페이크 로딩이 끝나면 키 입력을 대기합니다.
while (!Input.anyKeyDown)
{
Main.FirstLord = false;
yield return null;
}
}
// 키 입력이 들어오면 씬 전환이 가능하게 설정합니다.
sceneLoadOperation.allowSceneActivation = true;
}
public override void Clear()
{
}
}
This post is licensed under CC BY 4.0 by the author.