using UnityEngine; using UVC.Core; namespace Sample { /// /// App 레벨 서비스 등록 컨텍스트 - 애플리케이션 전체 생명주기 동안 유지되는 서비스를 등록합니다. /// /// /// [ 역할 ] /// InjectorAppContext를 상속받아 App 라이프사이클 서비스를 등록합니다. /// 씬 전환 시에도 DontDestroyOnLoad로 유지되며, 등록된 서비스들도 함께 유지됩니다. /// /// [ 설정 방법 ] /// /// 빈 GameObject 생성 → 이름: "AppContext" /// 이 컴포넌트(InjectorSampleAppContext) 추가 /// Inspector에서 UI Manager Prefab 연결 /// /// /// [ 등록되는 서비스 ] /// /// Type AILogService → ConsoleLogger (순수 C#) /// Type AIGameService → GameService (순수 C#, ILogService 의존성) /// Type BIAudioManager → InjectorSampleAudioManager (MonoBehaviour 동적 생성) /// Type CIUIManager → InjectorSampleUIManager (Prefab 기반) /// Type DInjectorSampleSettingsManager (순수 C# Singleton) /// Type DInjectorSampleNetworkManager (MonoBehaviour SingletonApp) /// FactoryISceneConfig → SceneConfig (팩토리 패턴) /// /// /// [ 실행 순서 ] /// DefaultExecutionOrder(-1000)으로 가장 먼저 실행됩니다. /// public class InjectorSampleAppContext : InjectorAppContext { [Header("Prefabs")] [SerializeField] [Tooltip("Type C 테스트용 - InjectorSampleUIManager 프리팹을 연결하세요")] private InjectorSampleUIManager uiManagerPrefab; /// /// App 라이프사이클 서비스들을 등록합니다. /// Awake 시점에 자동 호출되며, 모든 서비스는 씬 전환 시에도 유지됩니다. /// protected override async void RegisterServices() { base.RegisterServices(); // ========== Type A: 순수 C# 클래스 등록 ========== // MonoBehaviour가 아닌 일반 클래스를 서비스로 등록 // new T()로 인스턴스 생성되며, [Inject] 필드도 자동 주입됨 Injector.Register(ServiceLifetime.App); Injector.Register(ServiceLifetime.App); // ========== Type B: MonoBehaviour 동적 생성 등록 ========== // 런타임에 새 GameObject를 생성하고 컴포넌트를 추가 // DontDestroyOnLoad가 자동 적용됨 Injector.Register(ServiceLifetime.App); // ========== Type C: Prefab 기반 등록 ========== // Inspector에서 연결한 프리팹을 Instantiate하여 서비스로 등록 // 프리팹에 미리 설정된 값들이 유지됨 if (uiManagerPrefab != null) Injector.RegisterPrefab(uiManagerPrefab.gameObject, ServiceLifetime.App); // ========== Type D: 기존 Singleton 연동 ========== // Singleton 또는 SingletonApp를 상속받은 클래스를 Injector에 등록 // 기존 Instance 접근 방식과 [Inject] 방식 모두 사용 가능 Injector.RegisterSingleton(); // 순수 C# Singleton Injector.RegisterSingleton(); // MonoBehaviour SingletonApp // ========== Factory 패턴 사용 ========== // 복잡한 초기화 로직이 필요한 경우 팩토리 함수를 사용 Injector.RegisterFactory(injector => new SceneConfig { SceneName = "MainMenu", Difficulty = 1.0f }, ServiceLifetime.App); Debug.Log("[InjectorSampleAppContext] All App services registered"); } } }