// 싱글톤 인스턴스 존재 여부 확인 (체크 2) private static void CheckExsistence() { // 싱글톤 검색 _instance = FindObjectOfType <FowManager>(); // 인스턴스 가진 오브젝트가 존재하지 않을 경우, 빈 오브젝트를 임의로 생성하여 인스턴스 할당 if (_instance == null) { // 빈 게임 오브젝트 생성 GameObject container = new GameObject("FowManager Singleton Container"); // 게임 오브젝트에 클래스 컴포넌트 추가 후 인스턴스 할당 _instance = container.AddComponent <FowManager>(); } }
/// <summary> /// [Awake()에서 호출] /// <para/> 싱글톤 스크립트를 미리 오브젝트에 담아 사용하는 경우를 위한 로직 /// </summary> private void CheckInstance() { // 싱글톤 인스턴스가 존재하지 않았을 경우, 본인으로 초기화 if (_instance == null) { _instance = this; } // 싱글톤 인스턴스가 존재하는데, 본인이 아닐 경우, 스스로(컴포넌트)를 파괴 if (_instance != null && _instance != this) { Debug.Log("이미 FowManager 싱글톤이 존재하므로 오브젝트를 파괴합니다."); Destroy(this); // 만약 게임 오브젝트에 컴포넌트가 자신만 있었다면, 게임 오브젝트도 파괴 var components = gameObject.GetComponents <Component>(); if (components.Length <= 2) { Destroy(gameObject); } } }
private void OnDestroy() => FowManager.RemoveUnit(this);
private void OnDisable() => FowManager.RemoveUnit(this);
void OnEnable() => FowManager.AddUnit(this);