1. 목적 / 기능 요약
- 로딩 씬은 게임 시작 전,
데이터 테이블
, Addressable 프리팹
, 세이브 데이터
를 차례로 로딩합니다.
- 로딩 중인 항목과 전체 진행률을 텍스트 UI 로 시각화 합니다.
2. 관련 주요 스크립트 목록
스크립트 파일 |
설명 |
SceneLoading.cs |
로딩 씬의 전반적인 처리를 위해 만듬 |
GameLoaderManager.cs |
게임 플레이에 필요한 데이터를 로드 |
TableLoaderManager.cs |
테이블 파일 로드 |
AddressablePrefabLoader.cs |
프리팹 로드 |
SaveDataLoader.cs |
세이브 데이터 로드 |
3. 스크립트 동작 흐름
GameLoaderManager.cs
public class GameLoaderManager : MonoBehaviour
{
...
private void Awake()
{
...
GameObject gameObjectTableLoaderManager = new GameObject("TableLoaderManager");
tableLoader = gameObjectTableLoaderManager.AddComponent<TableLoaderManager>();
GameObject gameObjectAddressablePrefabLoader = new GameObject("AddressablePrefabLoader");
prefabLoader = gameObjectAddressablePrefabLoader.AddComponent<AddressablePrefabLoader>();
GameObject gameObjectSaveDataLoader = new GameObject("SaveDataLoader");
saveDataLoader = gameObjectSaveDataLoader.AddComponent<SaveDataLoader>();
}
private void Start()
{
StartCoroutine(LoadGameData());
}
private IEnumerator LoadGameData()
{
yield return LoadTableData();
yield return LoadAddressablePrefabs();
yield return LoadSaveData();
UnityEngine.SceneManagement.SceneManager.LoadScene(ConfigDefine.SceneNameGame);
}
/// <summary>
/// 테이블 데이터를 로드하고 진행률을 업데이트합니다.
/// </summary>
private IEnumerator LoadTableData()
{
...
}
/// <summary>
/// Addressable 리소스를 로드하고 진행률을 업데이트합니다.
/// </summary>
private IEnumerator LoadAddressablePrefabs()
{
...
}
/// <summary>
/// 세이브 데이터를 로드하고 진행률을 업데이트합니다.
/// </summary>
private IEnumerator LoadSaveData()
{
...
}
/// <summary>
/// 진행률을 계산하고 UI 업데이트
/// </summary>
private void UpdateLoadingProgress(Type type)
{
...
if (textLoadingPercent != null)
{
textLoadingPercent.text = $"{subTitle} 로드 중... {Mathf.Floor(totalProgress)}%";
}
}
Awake
에서 로딩에 필요한 3개 컴포넌트를 런타임에 생성합니다.
Start
에서 코루틴을 사용해 로딩을 시작합니다.
- 로딩 순서는 LoadTableData, LoadAddressablePrefabs, LoadSaveData 순서입니다.
- LoadTableData 는 테이블 파일들을 하나씩 로드하며, 각 파일 로드마다 진행률을 조금씩 올립니다.
- LoadAddressablePrefabs 는 prefabLoader.LoadAllPreLoadGamePrefabsAsync() 로 비동기 로딩을 합니다.
- 로딩이 완료될 때까지 while 루프로 로딩 진행률을 업데이트 합니다.
- LoadSaveData 는 세이브 데이터를 불러오고, 콜백으로 전달받은 진행률을 기준으로 진행률을 업데이트 합니다.
TableLoaderManager
/// <summary>
/// 데이터 테이블 Loader
/// </summary>
public class TableLoaderManager : MonoBehaviour
{
public static TableLoaderManager Instance;
private string[] dataFiles;
public TableNpc TableNpc { get; private set; } = new TableNpc();
public TableMap TableMap { get; private set; } = new TableMap();
public TableMonster TableMonster { get; private set; } = new TableMonster();
...
protected void Awake()
{
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
dataFiles = new[]
{
ConfigTableFileName.Map, ConfigTableFileName.Monster,
...
};
}
public void LoadDataFile(string fileName)
{
try
{
TextAsset textFile = Resources.Load<TextAsset>($"Tables/{fileName}");
if (textFile != null)
{
string content = textFile.text;
if (!string.IsNullOrEmpty(content))
{
switch (fileName)
{
case ConfigTableFileName.Animation:
TableAnimation.LoadData(content);
break;
case ConfigTableFileName.Monster:
TableMonster.LoadData(content);
break;
case ConfigTableFileName.Npc:
TableNpc.LoadData(content);
break;
...
}
}
}
}
catch (Exception ex)
{
GcLogger.LogError($"테이블 파싱중 오류. file {fileName}: {ex.Message}");
}
}
...
}
Awake
에서 싱글톤 인스턴스를 할당하고, 로드할 테이블 파일 목록을 초기화 합니다.
LoadDataFile
에서 각 테이블 파일을 불러와 해당하는 테이블 클레스에 LoadData()
를 호출합니다.
AddressablePrefabLoader