Interfaces

Tutorial

·

intermediate

·

+10XP

·

5 mins

·

(359)

Unity Technologies

Interfaces

In this tutorial, you’ll review the basics of interfaces. By the end of the tutorial, you’ll be able to:

  • Explain what an interface is and how they work.
  • Declare an interface.
  • Implement an interface.
  • Use an implemented interface.

Languages available:

1. Overview

An interface is a collection of method signatures and properties. You can think of interfaces like a contract that classes can implement: when a class implements an interface, a class instance can be treated as an instance of that interface. This functionality means that you can group together multiple disparate types and treat them in the same way.


By the end of the tutorial, you’ll be able to:


  • Explain what an interface is and how they work.

  • Declare an interface.

  • Implement an interface.

  • Use an implemented interface.

2. Before you begin

This tutorial is part of Intermediate Scripting, a collection of basic introductions to a wide range of different programming concepts. If you want a guided introductory learning experience, try the Create with Code course or the full Junior Programmer pathway.


3. What is an interface?

An interface is a collection of method signatures and properties. You can write a class or struct to implement an interface by using methods and properties which match the signatures declared by the interface. Classes and structs that implement an interface have methods and properties that can be called in an identical way, which means that they can be called without prior knowledge of the class or struct’s type.


4. Declare an interface

You can declare an interface as you would declare class or struct, but using the interface keyword. Within an interface, methods and properties have their signature declared but not their definition.


Here’s an example of an interface:


[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop



Let’s go through the interface declaration.


Name


The interface is called IDamageable. Note that the name starts with the letter I. This is a convention to make it easy for you to identify interfaces; it’s not required, but we recommend that you follow this convention.


Property


The first line of the interface is a declaration for a property called Position. The property is of the Vector3 type and must have at least a public getter. The property can optionally have a setter of any protection level.


Method


The second line of the interface is the declaration for a single method called Damage. This method has a single float parameter and a return type of void.


Other details


All methods and properties in an interface are automatically public and so when declaring them, the public keyword is not required. Also, since the definitions of methods and properties are up to the implementing classes and structs, no bodies are required.


5. Implement an interface

When you write a class or a struct that implements an interface, you must:


1. List the interface after any inheritance.


2. Then declare all the methods and properties which are part of the interface.


Here is an example that implements the IDamageable interface:


[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop

In this example, the PlayerHealth class implements the IDamageable interface. The class declares that it is implementing the interface after its MonoBehaviour inheritance. PlayerHealth then declares the Damage method, which matches the signature declared in the IDamageable interface.


6. Use an implemented interface

The advantage of interfaces comes in their grouping and anonymity. Consider a situation in a game using the example code for this tutorial: an explosion goes off and it needs to damage everything nearby. If everything in the game implemented the damage differently then the code for the explosion would have to find all of the affected objects and individually call their methods.


The example below demonstrates how interfaces can help with that problem:


[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop

This example script works in two stages:


  1. First, when the Start method is called, the Explosion class finds all of the MonoBehaviours in the scene and adds those which implement the IDamageable interface to a list. For more details on treating objects as different types, review Polymorphism.


  1. Then, when the Explode method is called, the class iterates through all of the objects that implement IDamageable, comparing their Positions to identify whether they are in range for the explosion blast. If an object is in range, its Damage method is called.

7. The SerializeReference attribute

Data that is part of a scene or part of an asset in Unity is serialized. This essentially means it is converted to another format to be saved to the computer’s drive.


A variable that has an interface type can be populated by a wide variety of different objects that implement that interface. That means that normally Unity would not know what data to serialize and so it would be ignored. However, if you use the SerializeReference attribute, Unity will instead serialize the object that is being represented by the variable.


Here is another example using the IDamageable interface:


[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop

This example is a PlayerHealth script, and the player has a shield. This shield is being initialized to an instance of the ProtonShield class, but any IDamageable class or struct could take its place. The public fields of the ProtonShield class will be visible in the Inspector window, because the SerializeReference attribute is being used.


For further details about using the SerializeReference attribute, review the Intermediate Scripting Polymorphism tutorial or the SerializeReference attribute documentation.


8. Summary

In this tutorial, you learned that interfaces behave as contracts for any classes and structs that implement them. This means that you can treat those classes and structs as instances of the interface. You also reviewed how to declare and implement an interface, following example scripts.


How could interfaces help you work more efficiently as you develop your own game or application?


Complete this tutorial