Built-in Arrays
The most basic type of array, available in both JS and C#, is the built-in array. The main shortcoming of built-in arrays is that they have a fixed-size (which you choose when you declare the array), however this shortcoming is balanced by their very fast performance. For this reason, built-in arrays are the best choice if you need the fastest performance possible from your code (for example, if you’re targeting iPhone). If there is a fixed and known number of items that you want to store, this is the best choice.
It’s also common to use this type of array if you have a varying number of items to store, but you can decide on a ‘maximum’ for the number of objects that you’ll need. You can then leave some of the elements in the array null when they’re not required, and design your code around this. For example, for the bullets in a shooting game, you may decide to use an array of size 50, allowing a maximum of 50 active bullets at any one time.
This type of array is also useful because it’s one of the type osf array which show up in Unity’s inspector window. This means that a built-in array ia good choice if you want to populate its contents in the Unity editor, by dragging and dropping references.
It’s also usually the type of array you get back from Unity functions, if you use a function which may return a number of objects, such asGetComponentsInChildren.
Built-in arrays are declared by specifying the type of object you want to store, followed by brackets. Eg:
Basic Declaration & Use:
C#
// declaration TheType[] myArray = new TheType[lengthOfArray]; // declaration example using ints int[] myNumbers = new int[10]; // declaration example using GameObjects GameObject[] enemies = new GameObject[16]; // get the length of the array int howBig = myArray.Length; // set a value at position i myArray[i] = newValue; // get a value from position i TheType thisValue = myArray[i];
Javascript
// declaration var myArray = new TheType[lengthOfArray]; // declaration example using ints var myNumbers = new int[10]; // declaration example using GameObjects var enemies = new GameObject[16]; // get the length of the array var howBig = enemies.Length; // set a value at position i myArray[i] = newValue; // get a value from position i var thisValue = myArray[i]
ArrayLists
The ArrayList is a .Net class, and is very similar to the Javascript Array mentioned previously, but this time available in both JS and C#. Like JS Arrays, ArrayLists are dynamic in size, so you can add and remove items, and the array will grow and shrink in size to fit. ArrayLists are also untyped, so you can add items of any kind, including a mixture of types in the same ArrayList. ArrayLists are also similarly a little more costly when compared to the blazingly fast performance of built-in arrays. ArrayLists have a wider set of features compared to JS Arrays, although neither of their feature sets completely overlaps the other.
Basic Declaration & Use:
Javascript
// declaration var myArrayList = new ArrayList(); // add an item to the end of the array myArrayList.Add(anItem); // change the value stored at position i myArrayList[i] = newValue; // retrieve an item from position i var thisItem : TheType = myArray[i]; (note the required casting!) // remove an item from position i myArray.RemoveAt(i); // get the length of the array var howBig = myArray.Count;
C#
var howBig = myArray.Count;
// declaration ArrayList myArrayList = new ArrayList(); // add an item to the end of the array myArrayList.Add(anItem); // change the value stored at position i myArrayList[i] = newValue; // retrieve an item from position i TheType thisItem = (TheType) myArray[i]; // remove an item from position i myArray.RemoveAt(i); // get the number of items in the ArrayList
2D Array
So far, all the examples of Arrays and Collections listed above have been one-dimensional structures, but there may be an occasion where you need to place data into an array with more dimensions. A typical game-related example of this is a tile-based map. You might have a ‘map’ array which should have a width and a height, and a piece of data in each cell which determines the tile to display. It is also possible to have arrays with more than two dimensions, such as a 3D array or a 4D array – however if you have a need for a 3D or 4D array, you’re probably advanced enough to not require an explanation of how to use them!
There are two methods of implementing a multi-dimensional array. There are “real” multi-dimensional arrays, and there are “Jagged” arrays. The difference is this:
With a “real” 2D array, your array has a fixed “width” and “height” (although they are not called width & height). You can refer to a location in your 2d array like this: myArray[x,y].
In contrast, “Jagged” arrays aren’t real 2D arrays, because they are created by using nested one-dimensional arrays. In this respect, what you essentially have is a one-dimensional outer array which might represent your ‘rows’, and each item contained in this outer array is actually an inner array which represents the cells in that row. To refer to a location in a jagged array, you would typically use something like this: myArray[y][x].
Usually, “real” 2D arrays are preferable, because they are simpler to set up and work with, however there are some valid cases for using jagged arrays. Such cases usually make use of the fact that – with a jagged array – each ‘inner’ array doesn’t have to be the same length (hence the origin of the term “jagged”).
Another important correction is that Unity’s Javascript used to have no support for creating 2D arrays – however since Unity 3.2, Unity’s JS now supports this.
Basic Declaration & Use of “real” 2D arrays:
JS:
// declaration:
// a 16 x 4 array of strings
var myArray = new string[16,4];
// and a real-world declaration example (where 'Tile' is a user-created custom class):
// create an array to hold a map of 32x32 tiles
var map = new Tile[32,32];
// set the value at a given location in the array
myArray[x,y] = newValue;
// retrieve a value from a given location in the array
var thisValue = myArray[x,y];
// get the length of 1st dimension of the array
var width = myArray.GetUpperBound(0);
// get the length of 2nd dimension of the array
var length = myArray.GetUpperBound(1);
C#:
// declaration:
// a 16 x 4 array of strings
string[,] myArray = new string[16,4];
// and a real-world declaration example (where 'Tile' is a user-created custom class):
// create an array to hold a map of 32x32 tiles
Tile[,] map = new Tile[32,32];
// set the value at a given location in the array
myArray[x,y] = newValue;
// retrieve a value from a given location in the array
ValueType thisValue = myArray[x,y];
// get the length of 1st dimension of the array
int width = myArray.GetUpperBound(0);
// get the length of 2nd dimension of the array
int length = myArray.GetUpperBound(1);