Zenject icon indicating copy to clipboard operation
Zenject copied to clipboard

Passing dynamic data + prefab through method Create (Factory) while that prefab has GameObjectContext

Open lucid-dreamm opened this issue 3 years ago • 1 comments

Hey, I would like to be able to pass dynamic data + prefab through method Create (Factory) while that prefab has GameObjectContext (Installer). In this situation, it seems zenject cannot inject dynamic data and it fails while without GameObjectContext, it works.

_factory.Create(prefab,data)

In scene context

  Container.BindFactory<Object, SpawnModuleData, ModuleFacade, ModuleFacade.Factory>()
                .FromFactory<PrefabFactory<SpawnModuleData, ModuleFacade>>();

lucid-dreamm avatar Jan 06 '23 22:01 lucid-dreamm

Try this instead:

Container.BindFactory<Object, SpawnModuleData, ModuleFacade, ModuleFacade.Factory>()
                .ResolveFromSubContainer().ByNewPrefabInstaller<YourInstaller>(prefabInstance);

Inside YourInstaller, you will inject the instances you're passing in.

public class YourInstaller : Installer
{
    [Inject]private SpawnModuleData _data;
    public override void InstallBindings()
    {
        Container.Bind<ModuleFacade>.AsSingle();
        Container.Bind.FromInstance(_data);
    }
}

SimonNordon4 avatar Apr 05 '23 13:04 SimonNordon4