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