A lightweight DI tool for Unity that supports singleton services with constructor and method injection.
- Add to your
Packages/manifest.json:
{
"dependencies": {
"com.mobiray.unity_di": "https://github.com/mobiray/unity-di.git"
}
}- Create a subclass of GameInstaller and register dependencies
public class MyInstaller : GameInstaller
{
protected override void RegisterDependencies(Container container)
{
container.Register<ISomeService, SomeService>();
container.RegisterSingleton<GameManager>();
}
}-
Add this Installer to every scene where you want these dependencies to be injected.
-
Use [Inject] annotation for injecting to method or constructor:
public class Player : MonoBehaviour
{
private ISomeService _service;
[Inject]
public void Construct(ISomeService service)
{
_service = service;
}
}