Repository navigation
Expand file tree
/
Copy pathGenerics.cs
More file actions
209 lines (188 loc) · 4.24 KB
/
Copy pathGenerics.cs
File metadata and controls
209 lines (188 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
using UnityEngine;
using System.Collections;
public class Generics : MonoBehaviour
{
void Start()
{
zombie firstZombie = new zombie("stubbs");
zombie secondZombie = new zombie("jackson");
zombie thirdZombie = new zombie("bob");
// var keyword is very open keyword - It tells the computer to expect any data type and it means to implicitly get a type once it's been assigned.
// Only after the actual object is crated and assigned to the identifier that was created with var will the identifier turn into a type.
var SomeThings = new ThreeThings<zombie>(firstZombie, secondZombie, thirdZombie);
Debug.Log(SomeThings);
var whatAmI = 1;
Debug.Log(whatAmI.GetType());
var mixedThings = new TwoThings<zombie, float>();
mixedThings.AssignThings(firstZombie, 1.0f);
LogTwoThings(mixedThings.GetFirstThing(), mixedThings.GetSecondThing());
var t = GetComponent<Transform>();
if (t is Transform)
{
t.localPosition = new Vector3(1, 0, 0);
}
}
// void Start()
// {
// var ImAFloat = tellMeLies(11.8f);
// Debug.Log(ImAFloat);
// }
// void Start()
// {
// zombie firstZombie = new zombie("stubbs");
// zombie secondZombie = new zombie("jackson");
// zombie thirdZombie = new zombie("bob");
// ThreeThings<zombie> SomeThings =
// new ThreeThings<zombie>(firstZombie, secondZombie, thirdZombie);
// Debug.Log(SomeThings);
// }
// void Start()
// {
// zombie first = new zombie("stubbs");
// zombie second = new zombie("jackson");
// Debug.Log(first);
// Debug.Log(second);
// swap(ref first, ref second);
// Debug.Log(first);
// Debug.Log(second);
// }
// void Start()
// {
// string first = "one";
// string second = "two";
// Debug.Log(first);
// Debug.Log(second);
// swap(ref first, ref second);
// Debug.Log(first);
// Debug.Log(second);
// }
// // Use this for initialization
// void Start()
// {
// int first = 1;
// int second = 2;
// Debug.Log(first);
// Debug.Log(second);
// swap(ref first, ref second);
// Debug.Log(first);
// Debug.Log(second);
//
// log(9);
// GameObject g = new GameObject("My name is mud");
// log(g);
// }
public void log<T>(T thing)
{
string s = thing.ToString();
Debug.Log(s);
}
public void swap<T>(ref T a, ref T b)
{
T temp = b;
b = a;
a = temp;
}
public class zombie
{
private string Name;//store the zombies name
//constructor to assign a name on creation.
public zombie(string name)
{
Name = name;
}
//override the ToString() command by
//returning the zombies name instead of it's type.
public override string ToString()
{
return Name;
}
}
public class ThreeThings<T> // we create a new class called ThreeThings that can hold 3 types of anything (ints, floats, gameObjects, strings, object[], etc)
{
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;
}
}
// 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;
// }
// }
public int tellMeLies(float f)
{
return (int)f;
}
public class Stuff<T>
{
T thing;
public void assignThing(T something)
{
thing = something;
}
}
// public class TwoThings<T, U>
// {
// T firstThing;
// U secondThing;
//
// public void AssignThings(T first, U second)
// {
// firstThing = first;
// secondThing = second;
// }
// }
public void LogTwoThings<T, U>(T firstThing, U secondThing)
{
Debug.Log(firstThing.GetType());
Debug.Log(secondThing.GetType());
}
public class TwoThings<T, U>
{
T firstThing;
U secondThing;
public void AssignThings(T first, U second)
{
firstThing = first;
secondThing = second;
}
public T GetFirstThing()
{
return firstThing;
}
public U GetSecondThing()
{
return secondThing;
}
}
// Update is called once per frame
void Update()
{
}
}