Generics
Tutorial
Intermediate
5 Mins
Overview
Overview
You have completed 0% of this Tutorial.
1.
Generics
Summary
How to create and use generic methods and classes.
Topics we'll cover
Language
English
Recommended Unity Versions
4.3 and Above
Tutorial
Generics
1.
Generics

SomeClass
using UnityEngine; using System.Collections; public class SomeClass { //Here is a generic method. Notice the generic //type 'T'. This 'T' will be replaced at runtime //with an actual type. public T GenericMethod<T>(T param) { return param; } }

SomeOtherClass
using UnityEngine; using System.Collections; public class SomeOtherClass : MonoBehaviour { void Start () { SomeClass myClass = new SomeClass(); //In order to use this method you must //tell the method what type to replace //'T' with. myClass.GenericMethod<int>(5); } }

GenericClass
using UnityEngine; using System.Collections; //Here is a generic class. Notice the generic type 'T'. //'T' will be replaced with an actual type, as will also //instances of the type 'T' used in the class. public class GenericClass <T> { T item; public void UpdateItem(T newItem) { item = newItem; } }

GenericClassExample
using UnityEngine; using System.Collections; public class GenericClassExample : MonoBehaviour { void Start () { //In order to create an object of a generic class, you must //specify the type you want the class to have. GenericClass<int> myClass = new GenericClass<int>(); myClass.UpdateItem(5); } }