Zenject 아주 기본 정리 - KG-Crash/crash GitHub Wiki

  1. Context -> 특정 범위 내에서 Installer 호출
  • ProjectContext : 프로젝트 시작할 때 에셋을 가지고 있음. 알아서 바인딩 해줌.
  • SceneContext : 씬 로드할 때 에셋을 가지고 있음. 알아서 바인딩 해줌.
  • GameObjectContext : Zenject 메소드 기반으로 Instantiate 하면 에셋을 가지고 있음. 알아서 바인딩 해줌.
  1. Installer -> 직렬화 데이터 및 의존성 주입 코드 작성
  • MonoInstaller : 씬 안에서 참조가능
  • ScriptableObjectInstaller : ScriptableObject 의 용도와 같음
  • PrefabInstaller : 프리팹 가능
  1. binding methods : Installer 에서 넣어줄 것들
    // 요청된 타입 클래스 (목적지)
Container.Bind<ContractType>()
    .WithId(Identifier)
    // 어떤 타입 클래스를 만들어 넣음 (들어가는놈)
    .To<ResultType>()
    // 어떻게 만들건지에 대해 : 생성자, 프리팹, 리소스 폴더에서 로드, 인스턴스
    .FromConstructionMethod()
    // AsTransient : 다따로, AsCached : Bind 타입별로 하나, AsSingle : 전부 하나(싱글톤)
    .AsScope()
    // 몰?루? 1
    .WithArguments(Arguments)
    // 몰?루? 2
    .OnInstantiated(InstantiatedCallback)
    // 몰?루? 3
    .When(Condition)
    // 몰?루? 4
    .(Copy|Move)Into(All|Direct)SubContainers()
    // Lazy 는 Zenject 마음대로, NonLazy 는 호출 이후 바로 (즉시 생성 필요하면 씀)
    .NonLazy() 
    // 몰?루? 5
    .IfNotBound();
  1. injection methods : constructor, method, property, field
// 1. 생성자
public class A
{
    A(string str) 
    { }
}

public class Installer : MonoInstaller<Installer>
{
    public override void InstallBinding()
    {
        Container.Bind<A>().FromInstance(new A("쓰벌"));
    }
}

// 2. 함수 (생성자와 같으나, Mono 에 쓸 때 사용)

// 3. 필드

public class A
{
    A(string str) 
    { }
}

public class B
{
    [Inject]
    public A object;
}

public class Installer : MonoInstaller<Installer>
{
    public override void InstallBinding()
    {
        Container.Bind<A>().FromInstance(new A("쓰벌"));
    }
}

// 4. 프로퍼티 (방법은 필드와 동일)
  1. lifecycle matched
  • MonoBehaviour.Awake : Injection 타이밍
  • MonoBehaviour.Start : IInitializable 구현 및 BindInterfacesTo
  • MonoBehaviour.Update : ITIckable 구현 및 BindInterfacesTo
  • MonoBehaviour.OnDestroy: IDisposable 구현 및 BindInterfacesTo
⚠️ **GitHub.com Fallback** ⚠️