I am new in Zenject world. I am modifying a sample code SampleGame1 (Beginner)
(you can check the full example here) on Zenject in ShipState
by implementing the Iinitializable
, the existing one is IDisposable
, so there are two implemented Interfaces right now.
public abstract class ShipState : IDisposable, IInitializable
{
public abstract void Update();
public virtual void Start()
{
// optionally overridden
}
public virtual void Dispose()
{
// optionally overridden
}
public virtual void OnTriggerEnter(Collider other)
{
// optionally overridden
}
public virtual void Initialize()
{
// optinally overridden
}
}
The purpose is I want to use the Initialize()
method in one of the states, so I can Subscribe to a signal inside the state.
public override void Initialize()
{
Debug.Log("Initializing ShipStateMoving");
signalBus.Subscribe("ExampleSignal");
}
public override void Dispose()
{
Debug.Log("Disposing ShipStateMoving");
_ship.ParticleEmitter.gameObject.SetActive(false);
}
But when I try to implement the method, Initialize()
is not called, but Dispose()
is successfully called... Why is that?
If I look at the InstallShip()
in GameInstaller.cs
I don't have any clue about how Idisposable
is bound, but why the implementation of Idisposable
in the existing example is successfully called while IInitializable
isn't? I have no idea.
// I have no clue in this function why Idisposable is bound, there are no BindInterfaces in it.
void InstallShip()
{
Container.Bind<ShipStateFactory>().AsSingle();
// Note that the ship itself is bound using a ZenjectBinding component (see Ship
// game object in scene hierarchy)
Container.BindFactory<ShipStateWaitingToStart, ShipStateWaitingToStart.Factory>().WhenInjectedInto<ShipStateFactory>();
Container.BindFactory<ShipStateDead, ShipStateDead.Factory>().WhenInjectedInto<ShipStateFactory>();
Container.BindFactory<ShipStateMoving, ShipStateMoving.Factory>().WhenInjectedInto<ShipStateFactory>();
}