Unity - choose which array to use
For those who have come across Unity, it is no secret that this platform provides a large number of various arrays - as many as 5 pieces (for JS and even more - 6!). So what to choose and how not to get confused in this variety? I'll start - from the end. Immediately bring the data collected to the tablet.
| Untyped | Typed | |
| Index Access, Fixed Length | - | built array (built-in array) |
| Index Access, Dynamic Size | ArrayList or Javascript Array | List |
| Key Access | Hashtable | Dictionary |
Javascript array
The simplest and slowest version of the array. Available only in JavaScript (UnityScript). Untyped, with dynamic size. You can store objects of any type, mixed. However, this can be confusing, and also (when using pragma strict) you will have to cast types each time.
Using:
| UnityScript | C # | |
| ad | var a: Array = new Array (); | - |
| adding | a.Add (item); | - |
| access | a [i] | - |
| deletion | a.RemoveAt (i); | - |
| the size | a.length | - |
Arraylist
.Net is an array type similar to the previous Javascript Array, but available for both UnityScript and C #. It has all the same advantages and disadvantages, but the set of functions is richer than in the previous case.
Using:
| UnityScript | C # | |
| ad | var a: Array = new ArrayList (); | ArrayList a = new ArrayList (); |
| adding | a.Add (item); | |
| access | a [i] | |
| deletion | a.RemoveAt (i); | |
| the size | a.Count | |
Built-in array
The fastest version of the array. However, it is a hard array with a fixed length, which does not allow inserting elements in the middle, etc. However, if you need maximum performance - then built-in arrays are what you are looking for. In addition, they can be two-dimensional.
Using:
| UnityScript | C # | |
| ad | var a: int [] = new int [100]; | int [] a = new int [100]; |
| adding | a [i] = item; | |
| access | a [i] | |
| deletion | - | |
| the size | a.Length | |
| 2D ad | var a: int [,] = new int [10, 10]; | int [,] a = new int [10, 10]; |
| 2D access | a [x, y] | |
MSDN Documentation: msdn.microsoft.com/en-us/library/system.array%28VS.80%29.aspx
Hashtable
An untyped array with access not by index, but by key. The key, by the way, is also untyped (more precisely, it, like the value, are objects of type Object).
Using:
| UnityScript | C # | |
| ad | var a: Hashtable = new Hashtable (); | Hashtable a = new Hashtable (); |
| adding | a ["key"] = item; | |
| access | a [key] | |
| deletion | a.Remove (key) | |
| the size | a.Count | |
Dictionary
It is similar to Hashtable, except that both the key and the element in such an array are of the specified type. Therefore, this business works faster and does not require an extra type cast.
Using:
| UnityScript | C # | |
| ad | var a: Dictionary. | Dictionary |
| adding | a ["key"] = item; | |
| access | a [key] | |
| deletion | a.Remove (key) | |
| the size | a.Count | |
Conclusion
Actually, the summary plate was at the very beginning of the article.
What can I advise? Need speed - use the built-in arrays []. And whenever possible - always use typed arrays. This will protect you from unnecessary confusion, type casting and win speed.
Also, do not forget that MSDN's array classes are in the System.Collections.Generic package, so either specify the path directly (for example, new System.Collections.Generic.List.