A simple dependency injection container for Unity3D.
- Container that operates within a Scene
- Component can be registered and resolved as Interface, etc.
- OptionalInjection (Default value is entered if there is no target instance in the container by adding the
[Nullable]
attribute.) - Method Injection
- Dependency injection into dynamically generated GameObjects. (IContainer.Instantiate())
- Open the Unity Package Manager
- Click the + button
- Select "Add package from git URL..."
- Enter
https://github.com/witalosk/UnitySimpleContainer.git?path=Packages/UnitySimpleContainer
- Create a
SceneContainer
in the scene. - Write a method with
[Inject]
attribute in the code of the injector.
public class BarComponent : MonoBehaviour
{
[SerializeField]
private BarComponent _prefab;
private float _timer = 0f;
private bool _isInstantiated = false;
private IFooComponent _fooComponent;
private IEnumerable<IParams> _paramsObjects;
private IContainer _container;
[Inject]
public void Construct([Nullable] IFooComponent fooComponent, IEnumerable<IParams> paramsObjects, IContainer container)
{
_fooComponent = fooComponent;
_paramsObjects = paramsObjects;
_container = container;
}
private void Start()
{
if (_fooComponent != null)
{
Debug.Log(_fooComponent.Test());
}
}
private void Update()
{
_timer += Time.deltaTime;
if (!_isInstantiated && _timer > 5f)
{
_container.Instantiate(_prefab);
_isInstantiated = true;
}
}
}
- If the
[Nullable]
attribute is given to the parameter, an exception will not be thrown if the target type is not registered in the container. - if the
RuntimeOnly
attribute is given to the parameter, the parameter will be injected only at runtime. (No error in the editor)