Register MonoBehaviour
Register from LifetimeScope's [SerializeField]
[SerializeField]
YourBehaviour yourBehaviour;
// ...
builder.RegisterComponent(yourBehaviour);
注記
RegisterComponent are similar to RegisterInstance. The only difference is that MonoBehaviour
s 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);