📜 ⬆️ ⬇️

Unity - choose which array to use

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.
UntypedTyped
Access by index
fixed length
-embedded array
(built-in array)
Access by index
dynamic size
ArrayList
or javascript array
List
Key AccessHashtableDictionary
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:
UnityScriptC #
announcementvar a: Array = new Array ();-
addinga.Add (item);-
accessa [i]-
deletiona.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:
UnityScriptC #
announcementvar a: Array = new ArrayList ();ArrayList a = new ArrayList ();
addinga.Add (item);
accessa [i]
deletiona.RemoveAt (i);
the sizea.Count
MSDN documentation: msdn.microsoft.com/en-US/library/system.collections.arraylist.aspx

Built-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:
UnityScriptC #
announcementvar a: int [] = new int [100];int [] a = new int [100];
addinga [i] = item;
accessa [i]
deletion-
the sizea.Length
2D advar a: int [,] = new int [10, 10];int [,] a = new int [10, 10];
2D accessa [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.aspx

Hashtable


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:
UnityScriptC #
announcementvar a: Hashtable = new Hashtable ();Hashtable a = new Hashtable ();
addinga ["key"] = item;
accessa [key]
deletiona.Remove (key)
the sizea.Count
MSDN documentation: msdn.microsoft.com/en-us/library/system.collections.hashtable.aspx

Dictionary


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:
UnityScriptC #
announcementvar a: Dictionary. <KeyT, ValueT> = new Dictionary. <KeyT, ValueT> ();Dictionary <KeyT, ValueT> a = new Dictionary <KeyT, ValueT> ();
addinga ["key"] = item;
accessa [key]
deletiona.Remove (key)
the sizea.Count
MSDN documentation: msdn.microsoft.com/en-us/library/xfhwa508.aspx

Conclusion


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.

Source: https://habr.com/ru/post/142285/


All Articles