Facebook

Interfaces in Unity

by Liam Cubicle Published February 13, 2024

There is no game without interactions. There are different options in Unity to create interactions among different game objects. One viable option is Interface. Interfaces allow scripts to interact with each other based on a collection of functionalities.

In this article, you’ll learn how interfaces work, how to create them, and how to use them to make creative interactions in your game.

What is an Interface?

An Interface is another type of script in Unity that contains a list of functionalities. It has a collection of methods, functions, and properties to be required by any class that implements the interface.

Classes that implement the same interface have their functionalities declared uniquely, that is, you don’t need to know what the class is before you can declare the functions in the classes.

Is an Interface the same as a User Interface?

No. An interface defines a contract that classes can implement. When a class implements an interface, this means that the class must meet the conditions of the contract. In contrast, a user interface is a means by which a person interacts with a computer.

Why use an Interface?

An interface is used to include certain functionalities in a script so that other scripts can access them. When a class implements an interface, it is treated as an instance of the interface. This means that different classes even though called in the same way can perform different actions.

Therefore, an interface can be used to call the same function to perform different actions.

How to create an interface?

As earlier mentioned, an interface is another type of script in Unity. Therefore, to create an interface, you’ll need a new C# Script. However, it has to be created inside the project, you can’t add interfaces to a game object.

Just right-click on Create, then select C# Script. There are conventional namings for interfaces, an interactable interface should begin with I followed by what it does. This convention makes it easy for you to identify your interfaces. It is recommended that you follow it.

Now, to declare the interface inside the script:

public interface IExampleInterface
{
  // Declare methods that implementing classes must implement
  void ExampleMethod();
}

In the example code above, IExampleInterface is the name of the example interface which declares a method ExampleMethod().

In the body of an interface, you can declare any method or function you want. These functionalities will be used by any class that implements the interface.

How to Implement an Interface?

Now, that you have made an interface, how can you implement it in a class?

public class ExampleClass : MonoBehaviour, IExampleInterface
{
  // Implement the method declared in the interface
  public void ExampleMethod();

  {
  Debug.Log(“Example method implementation”);
  }
}

In the code above, ExampleClass implements the IExampleInterface. Note that the method or function needs to be named exactly how it was in the interface. Inside the method, you can add whatever code you want. Just like we debug log Example method implementation in the above code.

How to use an implemented interface

Now, if you want to use the implemented interface in another script. How can we do this?

public class AnotherScript : MonoBehaviour
{
  // Reference to the class implementing the interface
  private IExampleInterface exampleInstance; 

  void Start()
  {
    exampleInstance = new ExampleClass();
    exampleInstance.ExampleMethod();
  }
}

The above code demonstrates the basic usage of an implemented interface in Unity. In this example, AnotherScipt creates an instance of ExampleClass, which implements IExampleInterface. Then, the ExampleMethod() was triggered.

Conclusion

In this article, you have learned how interfaces behave and how to use them in Unity, following example scripts. Now, it’s up to you to start using interfaces in your Unity projects.

Free download: Indie Game Marketing Checklist

Download now

Category

programming

Don't forget to share this post!

Popular assets for Unity

See all assets ->
    Cutting-edge volumetric fog and volumetric lighting with support for transparent materials.
    Volumetric clouds, day night cycles, dynamic skies, global lighting, weather effects, and planets and moons.
    A lightweight procedural skybox ideal for semi-stylized projects.
    Image-based Outlines for 2D and 3D games with variable line weight, color, and displacement options.
    Per-pixel gaussian blur on your entire screen, part of the UI, or in-scene objects.
    Drag-and-drop ready-to-use ambient, impact, and spell particle effects.

Free Indie Game Marketing Checklist

Learn how to make your game successful with this handy checklist.

Download for free