Unity3d 通过子容器安装程序注入IPoolable facade对象的运行时参数

Unity3d 通过子容器安装程序注入IPoolable facade对象的运行时参数,unity3d,dependency-injection,zenject,Unity3d,Dependency Injection,Zenject,问题是我在子容器安装程序构造函数中没有得到正确的值。例如,如果我正在使用如下工厂创建我的可池facadeObject:QuxFactory.Create(3.1415)然后在QuxInstaller构造函数中,我得到的浮点参数是0,而不是3.1415。。但是,如果我没有像poolablememorypool那样绑定QuxFactory,我就会得到预期的结果。所以我很困惑如何在子容器安装程序中获取参数,以便进一步注入子容器的依赖项中 下面是我正在玩的Extenject文档中的代码: 我已经注入了我

问题是我在子容器安装程序构造函数中没有得到正确的值。例如,如果我正在使用如下工厂创建我的可池facadeObject:
QuxFactory.Create(3.1415)然后在QuxInstaller构造函数中,我得到的浮点参数是0,而不是3.1415。。但是,如果我没有像poolablememorypool那样绑定
QuxFactory
,我就会得到预期的结果。所以我很困惑如何在子容器安装程序中获取参数,以便进一步注入子容器的依赖项中

下面是我正在玩的Extenject文档中的代码:

我已经注入了我的工厂,我正在实例化像这样的新实例

_shipFactory.Create(Random.RandomRange(2, 20));
公共类游戏安装程序:MonoInstaller
{
[SerializeField]私有对象[]ShipPrefables;
public override void InstallBindings()
{
Container.BindInterfacesTo().AsSingle();
Container.BindFactory()。
FromPoolableMemoryPool(x=>x.WithInitialSize(2)。FromSubcainerResolve()。
ByNewPrefableInstaller(GetPrefable));
}
私有对象GetPrefab(InjectContext上下文)
{
返回ShipPrefables[随机范围(0,ShipPrefables.Length)];
}
}
公共类ShipFacade:IPoolable、IDisposable
{
私有IMemoryPool _memoryPool;
私人浮动速度;
...
废话
...
当值公共无效(浮动速度,IMemoryPool内存池)
{
_memoryPool=memoryPool;
_速度=速度;//这里我得到了正确的值
}
公共空间处置()
{
_记忆工具(本);
}
公共类工厂:占位符工厂
{
}
}
公共类ShipInstaller:安装程序
{
私有只读浮点运算速度;
公共ShipInstaller([可选]浮动速度)
{
Debug.Log(速度);//这里我得到的是0!,而不是2:20之间的随机数
_速度=速度;
}
public override void InstallBindings()
{
Container.Bind().AsSingle();
Container.Bind().FromComponentOnRoot();
Container.BindInterfacesTo().AsSingle();
Container.BindInstance(_speed).whenInjectedTo();
Container.Bind();
}
}
此外,当我将float注入ShipInputHandler时,它注入为0

我认为这一行有一个“输入错误”:

Container.BindFactory<Vector3, Foo, Foo.Factory>().
FromMonoPoolableMemoryPool<Foo>(x => x.WithInitialSize(2).
FromComponentInNewPrefab(FooPrefab).UnderTransformGroup("FooPool"));
Container.BindFactory()。
FromMonoPoolableMemoryPool(x=>x.WithInitialSize(2)。
fromComponentNetnewPrefab(fooPrefact).transformGroup下(“FooPool”);

它不适用于MonopoolableMemoryPool()中的
,因为我们有一个参数Vector3。它应该是
来自MonopoolableMemoryPool()
来自MonopoolableMemoryPool()
。如果我在这里是正确的..

因为实例已经由池创建,那么我必须在
OnSpawned(Vector3参数,IMemeoryPool-pool)
中执行我的重新初始化逻辑。 要做到这一点,需要将依赖项(在我的例子中是ShipInputHandler)注入ShipFacede,并在OnPawned方法中执行如下操作:

public void OnSpawned(float speed, IMemoryPool memoryPool)
{
    _memoryPool = memoryPool;
    _speed = speed;

    _shipInputHandler.Initialize(speed);
}

或者我猜也可以从工厂通过ResolveGetter将其绑定。

您问过您使用的资产/库的制造商吗?@derHugo我用Zenject标记了这个问题。希望他能澄清这一点。
public void OnSpawned(float speed, IMemoryPool memoryPool)
{
    _memoryPool = memoryPool;
    _speed = speed;

    _shipInputHandler.Initialize(speed);
}