
For those who came across Unity, it’s no secret that this platform provides a large number of different arrays - as many as 5 pieces (for JS, even more - 6!). So what to choose and how not to get lost in this variety?
I'll start from the end. Immediately bring the data collected in the plate.
| Untyped | Typed |
Access by index fixed length | - | embedded array (built-in array) |
Access by index dynamic size | ArrayList or javascript array | List |
Key Access | Hashtable | Dictionary |
And now - let's talk about each individually ...
Javascript array
The easiest and slowest variant of the array. Available only in JavaScript (UnityScript). Untyped, with a dynamic size. You can store objects of any type, interspersed. However, this can bring confusion, and also (using pragma strict), you will have to give types each time.
Using:
| UnityScript | C # |
announcement | var a: Array = new Array (); | - |
adding | a.Add (item); | - |
access | a [i] | - |
deletion | a.RemoveAt (i); | - |
the size | a.length | - |
Documentation on the Unity website:
unity3d.com/support/documentation/ScriptReference/Array.html')
ArrayList
.Net array type is similar to the previous Javascript Array, however it is 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 # |
announcement | var a: Array = new ArrayList (); | ArrayList a = new ArrayList (); |
adding | a.Add (item); |
access | a [i] |
deletion | a.RemoveAt (i); |
the size | a.Count |
MSDN documentation:
msdn.microsoft.com/en-US/library/system.collections.arraylist.aspxBuilt-in array
The fastest array option. However, this is a hard array, with a fixed length, which does not allow inserting elements into the middle, etc. However, if you need maximum performance, then embedded arrays are what you are looking for. In addition, they can be two-dimensional.
Using:
| UnityScript | C # |
announcement | 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] |
Conversion from Array and other types to the Built-in array is done using the .ToBuiltin () method.
MSDN documentation:
msdn.microsoft.com/en-us/library/system.array%28VS.80%29.aspxHashtable
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 the type Object).
Using:
| UnityScript | C # |
announcement | var a: Hashtable = new Hashtable (); | Hashtable a = new Hashtable (); |
adding | a ["key"] = item; |
access | a [key] |
deletion | a.Remove (key) |
the size | a.Count |
MSDN documentation:
msdn.microsoft.com/en-us/library/system.collections.hashtable.aspxDictionary
Similar to Hashtable, except that both the key and the element in such an array are of the specified type. Consequently, this business works faster and does not require an extra type cast.
Using:
| UnityScript | C # |
announcement | var a: Dictionary. <KeyT, ValueT> = new Dictionary. <KeyT, ValueT> (); | Dictionary <KeyT, ValueT> a = new Dictionary <KeyT, ValueT> (); |
adding | a ["key"] = item; |
access | a [key] |
deletion | a.Remove (key) |
the size | a.Count |
MSDN documentation:
msdn.microsoft.com/en-us/library/xfhwa508.aspxConclusion
Actually, the summary plate was at the very beginning of the article.
What can you advise? Need speed - use built-in arrays []. And whenever possible, always use typed arrays. This will protect you from unnecessary confusion, casting and win speed.
Also do not forget that the MSDN array classes are in the System.Collections.Generic package, so either specify the path directly (for example, new System.Collections.Generic.List. <Int>), or write the appropriate import / using.