싱글톤, 게임매니저 로직 수정

1. 싱글톤이 파괴되기전에 OnAwake를 호출하는 오류 수정
2. 게임매니저 로직 start -> awake로 변경
This commit is contained in:
NTG 2025-08-14 17:01:55 +09:00
parent 73c46c651a
commit 6df9f4cfae
2 changed files with 8 additions and 16 deletions

View File

@ -11,7 +11,7 @@ public class GameManager : Singleton<GameManager>
private List<Singleton> _managerInstances; private List<Singleton> _managerInstances;
protected async void Start() protected override void OnAwake()
{ {
base.OnAwake(); base.OnAwake();
@ -22,38 +22,29 @@ protected async void Start()
private async Task Initialize() private async Task Initialize()
{ {
if (_managerDefinitionSo == null) Debug.Assert(_managerDefinitionSo != null, "_managerDefinitionSo is null");
{
Debug.LogError("_managerDefinitionSo");
return;
}
_managerInstances = new List<Singleton>(_managerDefinitionSo.ManagerClasses.Count); _managerInstances = new List<Singleton>(_managerDefinitionSo.ManagerClasses.Count);
// 매니저 클래스들을 순회하면서 씬에 존재하는지 확인하고 생성 또는 재사용
foreach (var managerPrefab in _managerDefinitionSo.ManagerClasses) foreach (var managerPrefab in _managerDefinitionSo.ManagerClasses)
{ {
if (managerPrefab == null) if (managerPrefab == null)
{ {
Debug.LogWarning("매니저 프리팹이 null입니다. 건너뜁니다."); Debug.LogWarning($"{managerPrefab.name} 프리팹이 null입니다. 건너뜁니다.");
continue; continue;
} }
// 씬에 이미 해당 타입의 매니저가 존재하는지 확인 var existingManager = FindAnyObjectByType(managerPrefab.GetType()) as Singleton;
var existingManager = FindObjectOfType(managerPrefab.GetType()) as Singleton;
if (existingManager != null) if (existingManager != null)
{ {
// 이미 씬에 존재하는 경우 재사용 print($"{existingManager.name} 오브젝트가 이미 존재합니다.");
Debug.Log($"매니저 {managerPrefab.GetType().Name}이(가) 씬에 이미 존재합니다. 재사용합니다.");
_managerInstances.Add(existingManager); _managerInstances.Add(existingManager);
} }
else else
{ {
// 씬에 존재하지 않는 경우 새로 생성
var newManagerInstance = Instantiate(managerPrefab); var newManagerInstance = Instantiate(managerPrefab);
newManagerInstance.name = $"{managerPrefab.name}_Instance"; newManagerInstance.name = managerPrefab.name;
Debug.Log($"매니저 {managerPrefab.GetType().Name}을(를) 새로 생성했습니다.");
_managerInstances.Add(newManagerInstance); _managerInstances.Add(newManagerInstance);
} }
} }

View File

@ -76,6 +76,7 @@ protected virtual void Awake()
else if (_instance != this) else if (_instance != this)
{ {
Destroy(gameObject); Destroy(gameObject);
return;
} }
} }
OnAwake(); OnAwake();