メインコンテンツまでスキップ

Register MonoBehaviour

Register from LifetimeScope's [SerializeField]

[SerializeField]
YourBehaviour yourBehaviour;

// ...

builder.RegisterComponent(yourBehaviour);
注記

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>();
注記

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 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

RegsiterComponent* method chain can be 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'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);