ProjectDDD/Assets/_DDD/_Scripts/GameFramework/GameManager.cs

89 lines
2.9 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
namespace DDD
{
public class GameManager : Singleton<GameManager>
{
2025-07-10 05:48:44 +00:00
[SerializeField]
private ManagerDefinitionSo _managerDefinitionSo;
2025-07-10 05:48:44 +00:00
private List<Singleton> _managerInstances;
protected async void Start()
{
base.OnAwake();
EventBus.ClearAll();
_ = Initialize();
}
private async Task Initialize()
{
if (_managerDefinitionSo == null)
{
Debug.LogError("_managerDefinitionSo");
return;
}
2025-07-17 08:15:40 +00:00
2025-07-10 05:48:44 +00:00
_managerInstances = new List<Singleton>(_managerDefinitionSo.ManagerClasses.Count);
// 매니저 클래스들을 순회하면서 씬에 존재하는지 확인하고 생성 또는 재사용
foreach (var managerPrefab in _managerDefinitionSo.ManagerClasses)
{
if (managerPrefab == null)
{
Debug.LogWarning("매니저 프리팹이 null입니다. 건너뜁니다.");
continue;
}
// 씬에 이미 해당 타입의 매니저가 존재하는지 확인
var existingManager = FindObjectOfType(managerPrefab.GetType()) as Singleton;
if (existingManager != null)
{
// 이미 씬에 존재하는 경우 재사용
Debug.Log($"매니저 {managerPrefab.GetType().Name}이(가) 씬에 이미 존재합니다. 재사용합니다.");
_managerInstances.Add(existingManager);
}
else
{
// 씬에 존재하지 않는 경우 새로 생성
var newManagerInstance = Instantiate(managerPrefab);
newManagerInstance.name = $"{managerPrefab.name}_Instance";
Debug.Log($"매니저 {managerPrefab.GetType().Name}을(를) 새로 생성했습니다.");
_managerInstances.Add(newManagerInstance);
}
}
// PreInit 단계 실행
foreach (var managerInstance in _managerInstances)
{
if (managerInstance is IManager manager)
{
manager.PreInit();
}
}
// Init 단계 실행 (비동기)
foreach (var managerInstance in _managerInstances)
{
if (managerInstance is IManager manager)
{
await manager.Init();
}
}
// PostInit 단계 실행
2025-07-10 05:48:44 +00:00
foreach (var managerInstance in _managerInstances)
{
if (managerInstance is IManager manager)
{
manager.PostInit();
}
}
}
}
}