Game Object Bind Methods - mariaheine/Zenject-But-Wiki GitHub Wiki

For bindings that create new game objects (eg. FromComponentInNewPrefab, FromNewComponentOnNewGameObject, etc.) there are also two extra bind methods.

  • WithGameObjectName = The name to give the new Game Object associated with this binding.

    Container.Bind<Foo>().FromComponentInNewPrefabResource("Some/Path/Foo").WithGameObjectName("Foo1");
    Container.Bind<Foo>().FromNewComponentOnNewGameObject().WithGameObjectName("Foo1");
  • UnderTransformGroup(string) = The name of the transform group to place the new game object under. This is especially useful for factories, which can be used to create many copies of a prefab, so it can be nice to have them automatically grouped together within the scene hierarchy.

    Container.BindFactory<Bullet, Bullet.Factory>()
        .FromComponentInNewPrefab(BulletPrefab)
        .UnderTransformGroup("Bullets");
  • UnderTransform(Transform) = The actual transform to place the new game object under.

    Container.BindFactory<Bullet, Bullet.Factory>()
        .FromComponentInNewPrefab(BulletPrefab)
        .UnderTransform(BulletTransform);
  • UnderTransform(Method) = A method to provide the transform to use.

    Container.BindFactory<Foo, Foo.Factory>()
        .FromComponentInNewGameObject()
        .UnderTransform(GetParent);
    
    Transform GetParent(InjectContext context)
    {
        if (context.ObjectInstance is Component)
        {
            return ((Component)context.ObjectInstance).transform;
        }
    
        return null;
    }

    This example will automatically parent the Foo GameObject underneath the game object that it is being injected into, unless the injected object is not a MonoBehaviour in which case it will leave Foo at the root of the scene hierarchy.

⚠️ **GitHub.com Fallback** ⚠️