Introduction to Interfaces in Unity 3D C#

Introduction to Interfaces in Unity 3D C#

by NSDG • Aug 16, 2019 • 0 Comments
128
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. Luckly there are ways to simplify the programming workflow, one of such ways is by implementing a C# Interfaces.

In C#, the interface contains a definition of methods that the class which uses it must implement. That way you can ensure that the class that uses certain interface has all the necessary methods implemented.

Example of C# Interface in Unity 3D


Interfaces can contain a declaration of a variable and/or a function (with or without arguments). With that being said let's create an example interface in Unity 3D:

  • Create a new script and 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).

Now let's create a script which will use the interface above:

  • Create new script and name it EntityScript
  • Open the script and add IEntity after the MonoBehaviour (separated by comma)

public class EntityScript : MonoBehaviour, IEntity

You'll notice that the script gives error, that's because the interface methods are not implemented yet. 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.

So how does it simplify the programming workflow you may ask?

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 now Get all the script references by using the interface name. 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

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 standart 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;
    }
}