Publish and receive some instances what spawned after resolving phase.
upm add package dev.monry.zenject-instancebroker
Note: upm
is command line interface for Unity Package Manager
Add reference to Zenject.InstanceBroker.asmdef
in your project's asmdef.
using Zenject;
public class SampleInstaller : Installer<SampleInstaller>
{
public override void InstallBindings()
{
Container.DeclareBrokableInstance<PublishSample>();
}
}
DiContainer.DeclareBrokableInstance<T>()
binds types and declare signals.
- Bind below types
IInstancePublisher<T>
IInstanceReceiver<T>
- Declare signal of
T
using UnityEngine;
using Zenject;
public class Sample : MonoBehaviour
{
[Inject] private IInstancePublisher<Sample> Publisher { get; }
[Inject]
private void Initialize()
{
Publisher.Publish(this);
}
public void Foo()
{
Debug.Log("Foo");
}
}
Invoke IInstancePublisher<T>.Publish()
after constructed.
using Zenject;
using UniRx;
public class ReceiveSample : IInitializable
{
[Inject] private IInstanceReceiver<Sample> Receiver { get; }
public void Initialize()
{
Receiver.Receive().Subscribe(x => x.Foo());
}
}
Will stream instance of Sample
when instantiated it.
Invoke IInstancePublisher<T>.Publish()
automatically according to the settings of ZenjectBinding
.
- Attach
ZenjectInstancePublisher
to a GameObject that contains the Component you want to publish - Drag & Drop Component you want to publish into
Components
field onZenjectBinding
- You can limit the scope in which instances are exchanged using
Context
field ofZenjectBinding
- Change
Bind Type
field ofZenjectBinding
if you want to publish other than instance of Component itself such as interface