Scripting
Intermediate

Generics

Checked with version: 4.1

-

Difficulty: Intermediate

-

Date: 13 Mar 2014

How to create and use generic methods and classes.

Generics

Intermediate Scripting

SomeClass

Code snippet

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;
    }
}
//JavaScript does not allow generics to be defined. To utilize
//this functionality you will need to use C#
import UnityEngine
import 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 def GenericMethod [of T](param as T) as T:
        return param

SomeOtherClass

Code snippet

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);
    }
}
//JavaScript does not allow generics to be defined. To utilize
//this functionality you will need to use C#
import UnityEngine
import System.Collections


public class SomeOtherClass(MonoBehaviour):

    private def Start():
        myClass = SomeClass()
        
        //In order to use this method you must
        //tell the method what type to replace
        //'T' with.
        myClass.GenericMethod[of int](5)

GenericClass

Code snippet

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;
    }
}
//JavaScript does not allow generics to be defined. To utilize
//this functionality you will need to use C#
import UnityEngine
import 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 [of T]:

    private item as T

    
    public def UpdateItem(newItem as T):
        item = newItem

GenericClassExample

Code snippet

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);
    }
}
//JavaScript does not allow generics to be defined. To utilize
//this functionality you will need to use C#
import UnityEngine
import System.Collections


public class GenericClassExample(MonoBehaviour):

    private def Start():
        //In order to create an object of a generic class, you must
        //specify the type you want the class to have.
        myClass as GenericClass[of int] = GenericClass[of int]()
        
        myClass.UpdateItem(5)
Method Overloading
Inheritance

Related tutorials