Injection Manager

This is the most important class in Engine: with its assistance you will be able to inject different project systems and classes to each other, which can significantly reduce amount of annoying fields in inspector and make your architecture much more flexible and scalable.

How to use existing systems and objects in my class?

To use any system or object in your class, you should use InjectionManager for this, by doing next 2 steps:

  1. Firstly, you need to [Inject] that system (inherited from SystemBase) or object (inherited from IInjectable) into your class

  2. Also, you need to call InjectionManager.InjectTo(this) from your Start() or Awake() method, or in constructor, if your class is not inherited from IInjectable

Without 2nd step the 1st one will not work, in case if your class is not inherited from ISystem.

Here is an example how to do that:

public class ExampleClass1 : IInjectable
{
    public void PostInject()
    {
        Debug.Log("I'm here!");
    }

    public void Foo()
    {
        Debug.Log("Hello!");
    }
}

public class ExampleClass2 : MonoBehaviour
{
    [Inject] private IEventsManager eventsManager;
    [Inject] private ExampleClass1 exampleObject;

    private void Start()
    {
        InjectionManager.CreateObject<ExampleClass1>();
        InjectionManager.InjectTo(this);
        eventsManager.TriggerEvent("ExampleEvent");
        exampleObject.Foo();
    }
}

And of course, you can [Inject] any amount of systems or objects and use them all in your class:

public class ExampleClass : MonoBehaviour
{
    [Inject] private IEventsManager eventsManager;
    [Inject] private IPlayerProgressionManager playerProgressionManager;
    [Inject] private IUIController uiController;




    private void Start()
    {
        InjectionManager.InjectTo(this);

        eventsManager.TriggerEvent("ExampleEvent");
        playerProgressionManager.AddExperience(25);
        uiController.ShowMessageBox("Info", "Hello!");
    }
}

How to create my own system?

To create your own system you need to do next steps:

  1. Create new class inherited from SystemBase class. You can do this in 2 clicks via special Editor Tool.

  2. You should create gameobject onto initial scene and add your newly created script as component. (You can do this via the same Editor Tool mentioned before).

  • If you don't need this system from game start and if you need it only on the certain scene, you should create such gameobject on that certain scene.

Example:

public class MySystem : SystemBase
{

}

How to create my own injectable class which is not inherited from MonoBehaviour?

To create your own injectable class you need to do next steps:

  1. Create your class and inherit it from IInjectable interface

  2. Add public void PostInject() method inside of this class. In this method you can do your initial logics related to systems and other objects injected to this class.

  3. Create instance of this class from somewhere by InjectionManager.CreateObject<T>() method;

Example:

public class ExampleClass1 : IInjectable
{
    [Inject] private IEventsManager eventsManager;

    public void PostInject()
    {
        eventsManager.TriggerEvent("ExampleClassCreated");
    }
}

public class ExampleClass2 : MonoBehaviour
{
    private void Start()
    {
        InjectionManager.CreateObject<ExampleClass1>();
    }
}

How to create my own injectable class which is inherited from MonoBehaviour?

To create your own injectable class inherited from MonoBehaviour you need to do next steps:

  1. Create your class and inherit it from MonoBehaviour class and IInjectable interface

  2. Add public void PostInject() method inside of this class. In this method you can do your initial logics related to systems and other objects injected to this class.

  3. Create instance of this class from somewhere by InjectionManager.CreateGameObject<T>() method;

Example:

//Player.cs
public class Player : MonoBehaviour, IInjectable
{
    [Inject] private IEventsManager eventsManager;

    public void PostInject()
    {
        eventsManager.TriggerEvent("Player initialized");
    }
}

//Enemy.cs
public class Enemy : MonoBehaviour, IInjectable
{
    [Inject] private IEventsManager eventsManager;
    [Inject] private Player player;

    public void PostInject()
    {
        eventsManager.TriggerEvent("Enemy initialized");
        transform.LookAt(player.transform);
    }
}

//GameManager.cs
public class GameManager : MonoBehaviour
{
    [SerializeField] private Transform spawnPoint;
    [SerializeField] private Transform enemySpawnPoint;

    private void Start()
    {
        Player player = InjectionManager.CreateGameObject<Player>();
        player.transform.position = spawnPoint.position;
        Enemy enemy = InjectionManager.CreateGameObject<Enemy>();
        enemy.transform.position = enemySpawnPoint.position;
    }
}

Each class can be presented only in 1 instance in InjectionManager! If you will try to call CreateGameObject or CreateObject method once again for the same class, InjectionManager will replace the previous instance by new one. So we recommend to use this system only for unique objects like player character, some managers, etc.

Last updated