Problem:
You have a base class that depends on some services.
You want to create a class that derives from that base class, which depends on some additional services on top of the ones that the base class does.
class Base : MonoBehaviour<A, B> { // The base class depends on some services A a; B b; protected override void Init(A a, B b) { this.a = a; this.b = b; } } class Derived : Base { // A derived type depends on some additional services C c; D d; }
Since the Derived class must derive from Base, it can not simultaneously derive from MonoBehaviour<A, B, C, D>.
How can Derived be defined in such a way that:
- all four dependencies can be provided to it from the outside,
- and it will be able to acquire the dependencies automatically at the beginning of the Awake event?
Solution: IInitializable<T…>
To make it possible to inject all the objects that your derived class and its base class depend on, implement the IInitializable<T…> interface, and list the types of all their combined dependencies as generic type arguments of the interface.
You will also need to override the bool Init(Context) method, to make it acquire all the combined dependencies at the beginning of the Awake event, instead of just the ones that the base class depends on.
public class Derived : Base, IInitializable<A, B, C, D> { C c; D d; // Implement Init(A, B, C, D) to be able to receive all four dependencies. public void Init(A a, B b, C c, D d) { // Init the base type Init(a, b); // Init the derived type this.c = c; this.d = d; } // Override Init(Context) to automatically receive all four dependencies at the beginning of the Awake event. protected override bool Init(Context context) => InitArgs.TryGet<Derived, A, B, C, D>(this); }
Problem
You have a base class that does not depend on any services.
You want to create a single class that derives from that base class, which does depend on some services.
// The base class with no dependencies class Base : MonoBehaviour { } class Derived : Base { // A derived type depends on some services A a; B b; }
Since the Derived class must derive from Base, it can not simultaneously derive from MonoBehaviour<A, B>.
How can Derived be defined in such a way that:
- the two dependencies can be provided to it from the outside,
- and it will be able to acquire the dependencies automatically at the beginning of the Awake event?
Solution: IInitializable<T…>
To make it possible to inject all the objects that your derived class and its base class depend on, implement the IInitializable<T…> interface, and list the types of all their combined dependencies as generic type arguments of the interface.
You will also need to use InitArgs.TryGet during the Awake event, to make it acquire all the combined dependencies automatically.
public class Derived : Base, IInitializable<A, B> { A a; B b; // Implement Init(A, B) to be able to receive two Init arguments public void Init(A a, B b) { this.a = a; this.b = b; } // Use InitArgs.TryGet to receive Init arguments at the beginning of the Awake event. void Awake() => InitArgs.TryGet<Derived, A, B>(this); }
Problem
You have a common base class that does not depend on any services.
You want to create a several classes that derives from that base class, which depend on different services.
// The base class with no dependencies namespace MyNamespace { public abstract class BaseBehaviour : MonoBehaviour { } } class Derived1 : BaseBehaviour { // Derived types depend on some services A a; B b; } class Derived2 : BaseBehaviour { C c; D d; }
Since the derived classes must derive from BaseBehaviour, they can not simultaneously derive from MonoBehaviour<T...>. So how can derived types be created that contain all functionality both from your custom base class and from MonoBehaviour<T...>?
Solution: Base Class Generator
You can use a Base Class Generator to generate new custom base classes similar to MonoBehaviour<T...>, but based on your own base type.
To create a new base class generator for your type, perform the following steps:
- Select
Create > Init(args) > Base Class Generatorin the Project window. - Add the namespace that contains your base class to the Using Statements list.
- Add the desired name for the generated base classes to the Class Name field.
- Add the type name of your base type to the Derives From field.
- Click Generate to generate your base classes.

After this your types can derive from one the generated generic base types, just like they would from MonoBehaviour<T...>.
public class Derived1 : BaseBehaviour<A, B> { A a; B b; protected override void Init(A a, B b) { this.a = a; this.b = b; } }
If your base class also does have some dependencies, but only to global services, and you’d still want to use the Base Class Generator with it, you could make it implement IInitializable<T...> and then have it automatically resolve its own dependencies during its Awake event.
// The base class with no dependencies
namespace MyNamespace
{
public abstract class BaseBehaviour : MonoBehaviour, IInitializable<ILogger>
{
ILogger logger;
public void Init(ILogger logger) => this.logger = logger;
protected virtual void Awake() => InitArgs.TryGet<BaseBehaviour, ILogger>(this);
}
}
If you do this, make sure to define the Awake method in your base class as protected virtual, so that the generated base classes can override it.