Register MonoBehaviour

Register from LifetimeScope's [SerializeField]#

[SerializeField]
YourBehaviour yourBehaviour;
// ...
builder.RegisterComponent(yourBehaviour);
note

RegisterComponent are similar to RegisterInstance. The only difference is that MonoBehaviours registered with RegisterComponent will be injected even if not Resolved.

Register from scene with LifetimeScope#

builder.RegisterComponentInHierarchy<YourBehaviour>();
note

RegisterComponentInHierarchy always have a Scoped lifetime because the lifetime is equal to the scene.

Register component that Instantiate from prefab when resolving#

[SerializeField]
YourBehaviour prefab;
// ...
builder.RegisterComponentInNewPrefab(prefab, Lifetime.Scoped);

Register component that with new GameObject when resolving#

builder.RegisterComponentOnNewGameObject<YourBehaviour>(Lifetime.Scoped, "NewGameObjectName");

Register component as interface#

builder.RegisterComponentInHierarchy<YourBehaviour>()
.AsImplementedInterfaces();

Register component to specific parent Transform#

// Instantiate under the specified transform
builder.RegisterComponentFromInNewPrefab<YourBehaviour>(Lifetime.Scoped)
.UnderTransform(parent);
// Find the component under the specified transform.
builder.RegisterComponentInHierarchy<YourBehaviour>()
.UnderTransform(parent);

Or find at runtime.

builder.RegisterComponentFromInNewPrefab<YourBehaviour>(Lifetime.Scoped)
.UnderTransform(() => {
// ...
return parent;
});

Grouping MonoBehaviour's Registration#

builder.UseComponents(components =>
{
components.AddInstance(yourBehaviour);
components.AddInHierarchy<YourBehaviour>();
components.AddFromNewPrefab(prefab, Lifetime.Scoped);
components.AddOnNewGameObject<YourBehaviour>(Lifetime.Scoped, "name");
});

This is the same as:

builder.RegisterComponent(yourBehaviour);
builder.RegisterComponentInHierarchy<YourBehaviour>();
builder.RegisterComponentFromNewPrefab(prefab, Lifetime.Scoped);
builder.RegisterComponentOnNewGameObject<YourBehaviour>(Lifetime.Scoped, "name");

You can create a group with a specified parent.

builder.UseComponents(parentTransform, components =>
{
// GetComponentInChildren under `parentTransform`
components.AddInHierarchy<YourBehaviour>();
// Instantiate under `parentTransform`
components.AddFromNewPrefab(prefab, Lifetime.Scoped);
components.AddOnNewGameObject<YourBehaviour>(Lifetime.Scoped, "name");
})

This is the same as:

builder.RegisterComponentInHierarchy<YourBehaviour>()
.UnderTransform(parentTransform);
builder.RegisterComponentFromNewPrefab(prefab, Lifetime.Scoped)
.UnderTransform(parentTransform);
builder.RegisterComponentOnNewGameObject<YourBehaviour>(Lifetime.Scoped, "name");
.UnderTransform(parentTransform);