Skip to main content

Method Injection

If you need to inject dependencies into a type but can't do so with constructors, consider using method injection instead. This is most useful for MonoBehaviours but can be done with any class, including test cases within the Unity Test Framework.

public class SomeBehaviour : MonoBehaviour
{
float speed;

[Inject]
public void Construct(GameSettings settings)
{
speed = settings.speed;
}
}

The [Inject]-annotated method can have any name and any accessibility level.

For more information about managing MonoBehaviours and GameObjects, see Injecting into MonoBehaviours.