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:
Firstly, you need to
[Inject]
that system (inherited fromSystemBase
) or object (inherited fromIInjectable
) into your classAlso, you need to call
InjectionManager.InjectTo(this)
from yourStart()
orAwake()
method, or in constructor, if your class is not inherited fromIInjectable
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:
And of course, you can [Inject] any amount of systems or objects and use them all in your class:
How to create my own system?
To create your own system you need to do next steps:
Create new class inherited from
SystemBase
class. You can do this in 2 clicks via special Editor Tool.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:
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:
Create your class and inherit it from
IInjectable
interfaceAdd
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.Create instance of this class from somewhere by
InjectionManager.CreateObject<T>()
method;
Example:
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:
Create your class and inherit it from
MonoBehaviour
class andIInjectable
interfaceAdd
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.Create instance of this class from somewhere by
InjectionManager.CreateGameObject<T>()
method;
Example:
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