Skip to main content

Register MonoBehaviour

Register from LifetimeScope's [SerializeField]

[SerializeField]
YourBehaviour yourBehaviour;

// ...

builder.RegisterComponent(yourBehaviour);
note

RegisterComponent is similar to RegisterInstance. The key difference is that MonoBehaviours registered with RegisterComponent will have dependencies injected into them even if they are not explicitly resolved.

Register from Scene with LifetimeScope

builder.RegisterComponentInHierarchy<YourBehaviour>();
note

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

Register Component Instantiated from Prefab upon Resolution

[SerializeField]
YourBehaviour prefab;

// ...

builder.RegisterComponentInNewPrefab(prefab, Lifetime.Scoped);

Register Component on New GameObject upon Resolution

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

Register Component as Interface

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

Register Component under Specific Parent Transform

// Instantiate new GameObject under the specified transform
builder.RegisterComponentOnNewGameObject<YourBehaviour>(Lifetime.Scoped)
.UnderTransform(parent);

// Instantiate new prefab under the specified transform
builder.RegisterComponentInNewPrefab(prefab, Lifetime.Scoped)
.UnderTransform(parent);

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

Or find at runtime.

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

Register as DontDestroyOnLoad

RegisterComponent* method chain supports DontDestroyOnLoad.

// Instantiate new GameObject as DontDestroyOnLoad
builder.RegisterComponentOnNewGameObject<YourBehaviour>(Lifetime.Scoped)
.DontDestroyOnLoad();

// Instantiate new prefab as DontDestroyOnLoad
builder.RegisterComponentInNewPrefab(prefab, Lifetime.Scoped)
.DontDestroyOnLoad();

Grouping MonoBehaviour Registrations

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

This is the same as:

builder.RegisterComponent(yourBehaviour);
builder.RegisterComponentInHierarchy<YourBehaviour>();
builder.RegisterComponentInNewPrefab(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.AddInNewPrefab(prefab, Lifetime.Scoped);
components.AddOnNewGameObject<YourBehaviour>(Lifetime.Scoped, "name");
})

This is the same as:

builder.RegisterComponentInHierarchy<YourBehaviour>()
.UnderTransform(parentTransform);

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

Register Multiple Components from Hierarchy

RegisterComponentInHierarchy<T>() registers one component found in the scene. If you want to register and inject into all instances of a specific component type in the scene, you need to find them manually and register them.

var weapons = FindObjectsOfType<FireballWeapon>();
foreach (var weapon in weapons)
{
builder.RegisterComponent(weapon);
}

To ensure dependencies are injected into all of them (even if they are not resolved elsewhere), you can register a build callback that resolves them as a collection:

// Force injection for all registered FireballWeapon instances
builder.RegisterBuildCallback(container =>
{
container.Resolve<IEnumerable<FireballWeapon>>();
});