Dependency Injection in Unity3D

Akshay Arora
XRPractices
Published in
3 min readApr 5, 2020
Dependency Injection in Unity

A motorcycle needs tyres to move and tyres provide the motorcycle with enough balance for movement. Therefore, motorcycles depend on tyres in general for their movement, not a specific tyre brand.

The motorcycle only “cares” about its tyres when it’s about to move.
Changing the tyre brand doesn’t mean the entire motorcycle has to be remanufactured.

A form of dependency injection would be to create a motorcycle’s rim that is independent of a specific tyre brand.

Now, let’s have an overview of common terminologies:

Inversion of control (IoC) is a design principle, which inverts the flow of control as compared to traditional control flow. In simple English, IoC means, instead of cooking your food yourself, you order it from a restaurant, where another person will cook for you. Thus, this is called inversion of the control — from you to the chef.

Dependency Injection is a design pattern used to implement IoC.

Dependency Injection in Unity vs Dependency Injection in Classical Programming

In the case of classical programming, we have constructors. A Constructor is a special method which is used to initialize or construct objects. So, a constructor is going to reveal the class’s dependencies and clarifies that you cannot build the class object without passing the required data which that particular class is dependent on. For example:

using System;
public class Bike
{
public string make;
public string model;
public int manufacturingYear;
// Constructor with multiple parameters
public Bike(string makeName, string modelName, int modelYear)
{
make = makeName;
model = modelName;
manufacturingYear = modelYear;
}
public static void Main(string[] args)
{
Bike myBike = new Bike("Royal Enfield", "Himalayan", 2019);
Console.WriteLine("I bought " + myBike.make + " " + myBike.model + " in " + myBike.manufacturingYear);
}
}

Now in Unity, with monobehaviours, there are no constructors. So, the only way to check what are the dependencies is to have a look at the public or Serialized fields.

Let’s take an example:

public class GameManager : MonoBehaviour
{
private Player player;
InputController controller;
public void Awake()
{
controller = new InputController();
GameObject myPlayer = GameObject.Find("Player");
player = myPlayer.GetComponent<Player>();
}
public void Update()
{
if (controller.LeftKeyPressed())
player.MoveLeft();
if (controller.RightKeyPressed())
player.MoveRight();
}
}

Here you can see, this class GameManager depends on Player and InputController. You can fill this dependency by calling GameObject.Find or GetComponent. In this case, GameManager itself is responsible for getting its dependencies.

Now, if you invert this and some other class injects these dependencies, the GameManager doesn’t care or even know about how to inject these dependencies.

Benefits of Dependency Injection

  • Extensibility: Loose coupling helps with extending the XR application or game. In game development where the requirements may change a great deal during the lifetime of the project, this can be very useful.
  • Testability and Mocking: Unit testing becomes much easier as the developer can change the implementations.
  • Clean, Readable and Reusable code.

How to get started with Dependency Injection in Unity?

Dependency Injection frameworks or IoC containers are used to handle the resolution of dependencies in objects. Using these frameworks, you can take all the monobehaviours of your scene in one single place and let the framework pass the dependencies of these monobehaviours.

Extenject Dependency Injection IOC is a very popular and widely used framework for Unity.

In the next article, we will cover how to install and work with Extenject.

Till then, Happy Coding! :)

--

--