Generic Interface- C# Skill in Unity: 9

S.J. Jason Liu
3 min readOct 3, 2021

--

Objective: creating generic Interface to handle different implement with one Interface.

We had introduced about Interface and abstract class in the previous tutorials. They are all pretty handy tools to create the core function of your game. In this tutorial, we will dig a little bit deeper about using generic interface.

Concept

As we know, an Interface will not contain anything but properties and methods, and you can set the variable for methods. However, if this Interface would be implemented to many classes, you might need to set multiple variables to same method.

And it would reveal in options when you want to call a method is implemented from an Interface.

This would cause some troubles on classes that implement this Interface. Therefore, we will need to use generic Interface to solve this problem.

Setting generic

First we need to make our Interface as generic type by adding T with brackets to its name.

And use T instead of variables of method.

That way you have define this method as generic type. When implement this Interface, you need to declare what type you would use in that class.

Using generic type of Interface

As we using this Interface, we would need to input the type in brackets of it.

And the implemented method would also use the type.

It would be much easier if you want to implement this method or Interface as another type.

--

--

S.J. Jason Liu

A passionate gamer whose goal is to work in video game development.