Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Generics : MonoBehaviour {
- //BASIC EXAMPLE
- public void log<T>(T thing){
- string s = thing.ToString();
- Debug.Log(s);
- }
- //GENERIC FUNCTION
- public void swap<T>(ref T a,ref T b){
- T temp = b;
- b = a;
- a = temp;
- }
- void Start(){
- //Discomment what you want to seee working
- //BASIC EXAMPLE
- // log(9);
- // GameObject g = new GameObject("My name is mud");
- // log(g);
- // log(30.55f);
- //GENERIC FUNCTION
- // int first = 1;
- // int second = 2;
- // Robot first = new Robot("001");
- // Robot second = new Robot("002");
- // swap(ref first,ref second); // Discoment and comment again this method will work on any variables you want
- // Debug.Log(first);
- // Debug.Log(second);
- //GENERIC TYPES
- Robot first = new Robot("Thing001");
- Robot second = new Robot("Thing002");
- Robot third = new Robot("Thing003");
- ThreeThings<Robot> threeThings = new ThreeThings<Robot>(first, second, third);
- print("Abstract result: " + threeThings);
- }
- //GENERIC FUNCTION
- public class Robot{
- private string Name;//store the robot name
- //constructor to assign a name on creation.
- public Robot(string name)
- {
- Name = name;
- }
- //override the ToString() command by
- //returning the robots name instead of its type.
- public override string ToString()
- {
- return Name;
- }
- }
- //GENERIC TYPES
- public class ThreeThings<T>
- {
- private T first;
- private T second;
- private T third;
- //constructor for three things
- public ThreeThings(T a, T b, T c)
- {
- first = a;
- second = b;
- third = c;
- }
- //override ToString()
- public override string ToString()
- {
- return first + " " + second + " " + third;
- }
- }
- //Multiple Generic Values
- public class TwoThings<T, U>
- {
- T firstThing;
- U secondThing;
- public void AssignThings(T first, U second)
- {
- firstThing = first;
- secondThing = second;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment