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.
    UntypedTyped
    Index Access,
    Fixed Length
    -built array
    (built-in array)
    Index Access,
    Dynamic Size
    ArrayList
    or Javascript Array
    List
    Key AccessHashtableDictionary
    And now - let's talk about each one individually ...

    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:
    UnityScriptC #
    advar a: Array = new Array ();-
    addinga.Add (item);-
    accessa [i]-
    deletiona.RemoveAt (i);-
    the size
    a.length
    -
    Unity site documentation: unity3d.com/support/documentation/ScriptReference/Array.html

    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:
    UnityScriptC #
    advar 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 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:
    UnityScriptC #
    advar 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]
    Converting 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


    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:
    UnityScriptC #
    advar 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


    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:
    UnityScriptC #
    advar a: Dictionary. = new Dictionary.();Dictionary a = new Dictionary();
    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 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.), or write the appropriate import / using.

    Also popular now: