Introduction to Interfaces in Unity C#

When programming in Unity, it’s easy to overcomplicate your code, which in turn can become harder to maintain the more you add to it. Luckily there are ways to simplify the programming workflow, one of such ways is by implementing C# Interfaces.

C# interface contains definitions of a method(s) or variable(s) that the class which uses it must implement, basically ensuring that any class that uses a certain interface has all its methods implemented.

To start using C# interfaces in Unity, follow the example below:

  • Create a new script and name it anything (in my case I’ll name it InterfaceContainer)
  • Remove everything inside it then paste the code below:
public interface IEntity
{
    void Initialize(); //Function without any arguments
    float health { get; set; } //A variable
    void ApplyDamage(float points); //Function with one argument
}

The interface is called IEntity (Note: The capital i at the start is not necessary, but for convenience, name all your interfaces with ‘I’ at the start, that way you would know when the class uses an interface).

To use the interface in a C# script, follow the steps below:

  • Create a new script and name it anything (in my case I’ll name it EntityScript)
  • Add interface name after the MonoBehaviour, which is IEntity in this case (separated by comma)
public class EntityScript : MonoBehaviour, IEntity

You’ll notice that the script gives an error, that’s because the interface methods are not implemented. So let’s implement IEntity methods:

using UnityEngine;

public class EntityScript : MonoBehaviour, IEntity
{

    public float health { get; set; }

    public void Initialize()
    {
        health = 100;
    }

    public void ApplyDamage(float points)
    {
        health -= points;
    }
}

The interface methods are now implemented.

How do interfaces simplify the programming workflow?

The main advantage of C# interfaces is that they can be used by multiple classes, so instead of calling GetComponent for each script, you can get all the script references by using the interface name.

Use the C# interfaces when you need to implement the same methods in more than one script, or when you need to reference an unknown class in the same context.

Check the example below:

        //Get the script that uses IEntity interface
        IEntity interfaceEntity = gameObject.GetComponent<IEntity>();
        interfaceEntity.Initialize(); //Initializing the entity
        interfaceEntity.ApplyDamage(10); //Applying the damage
        interfaceEntity.health += 10; //Healing the entity

The script above gets a component with IEntity interface, then calls its methods.

Bonus

Interfaces can also accept a custom data type. For example:

public interface IHealable<T>
{
    void Heal(T type);
}

The data type is then provided when implementing the Interface in a Class (It can be a standard type such as float or int, or a more complex type such as a Class or even another Interface):

using UnityEngine;

public class EntityScript : MonoBehaviour, IEntity, IHealable<int>
{

    public float health { get; set; }

    public void Initialize()
    {
        //health = 100;
        Heal(100);
    }

    public void ApplyDamage(float points)
    {
        health -= points;
    }

    public void Heal(int points)
    {
        health = points;
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *