Array classes (arrays)
Arrays allow you to store multiple objects in a variable. The array class can only be used with JavaScript. For more information about arraylists, dictionaries, or hash tables in C # or JavaScript, refer to MSDN.
This is a basic example of what you can do with an array class
function Start ()
{
var arr = new Array ();
Arr. Push ("Hello"); Add an Element
Print (arr[0]); Print the first element
Arr length = 2; Resizing an array
arr [1] = "World"; Assigning "World" to a second element
for (Var value:string in arr)//traverse this array
{
Print (value);
}
}
There are two types of arrays in unity, built-in arrays, and normal javascript arrays. Built-in array (original. NET array), is very fast and efficient, but they cannot be resized. They are statically typed, which allows them to be edited in the Inspector. This is a simple example of how to use a built-in array.
var value:float[]; Expose a floating-point group in the inspector, where you can edit it
function Start ()
{
for (var value in values)//traversal array
{
print (value);
}
Value = new float[10]; Since we cannot adjust the size of the built-in array, we must recreate an array to adjust its size
value[1] = 5.0;//assigns a value to the second element
}
Built-in arrays are useful in performance-related code (using Unity's JavaScript and built-in arrays makes it very easy to use mesh interface to process 20,000 vertices within a second.) On the other hand, an ordinary JavaScript array can be resized, sorted, and can do all of the array classes you expect. The JavaScript array is not displayed in the Inspector panel. You can easily convert between JavaScript arrays and built-in arrays.
function Start ()
{
var array = new Array (Vector3 (0,0,0), Vector3 (0,0,1));
Array. Push (Vector3 (0,0,2));
Array. Push (Vector3 (0,0,3));
var builtinarray:vector3[] = array. Tobuiltin (VECTOR3); Copy the JS array to the built-in array
var newarr = new Array (Builtinarray); Assigning a built-in array to a JS array
Print (newarr); Newarr and array contain the same elements
}
Note that all functions under Unity's naming convention begin with uppercase. For the convenience of JavaScript users, the Unity array class also accepts lowercase functions.
Variable
var length:int//Description: The Length property of the array, which returns or sets the number of elements in the array.
function Start ()
{
var arr = Array ("Hello", "World");
Print (arr. length); Print Two
Arr. Length = 5; Resize an array to a size of 5
}
Function
function Add (value:object): void//Description: Adds value to the end of the array.
var arr = new Array ("Hello");
Arr. ADD ("World");
Print (arr); Print "Hello", "World"
function clear (): void//Description: Empties the array. The length of the array will be zero.
var hello = new Array ("Hello", "World");
Hello. Clear (); Now hello contains 0 elements
function Concat (Array:array, Optionalarray0:array, Optionalarray1:array): Array//Description: Connects two or more arrays. This method does not change the existing number and returns the concatenated array copy
function Start ()
{
var arr = new Array ("Hello", "World"):
var arr2 = new Array ("!");
var joined = arr. Concat (ARR2); Now jointed contains all 3 strings
Print (joined); Print "Hello", "World", "!"
}
function Join (seperator:string): string//Description: The contents of the linked array are a string. The element will be split by the Seperator string and return a copy of the array
function Start ()
{
var arr = new Array ("Hello", "World");
Print (arr. Join (","));//Printing "Hello,world"
}
function Pop (): Object//Description: Removes the last element of the array and returns it.
var arr = new Array ("Hello", "World");
Arr. Pop ();
Print (arr);//Only "Hello" is printed
function Push (value:object): Int//Description: Adds value to the end of the array. and returns the new array length.
var arr = new Array ("Hello");
Arr. Push ("World");
Print (arr);//Printing "Hello", "World"
function RemoveAt (index:int): void//Description: Removes the element indexed to index from the array.
var arr = new Array ("Hello", "and Good Morning", "World");
Arr. Remove (1); Remove "and Good Morning"
Print (arr);//Printing "Hello World"
function Reverse (): Array//Description: Reverses the order of all elements in the array.
var hello = new Array ("Hello", "World");
Hello Reverse ();
print (hello);//Printing World,hello
function Shift (): Object//Description: Moves the first element of the divisor group and returns it.
var arr = new Array ("Hello", "World");
Arr. Shift ();
Print ("World"); Arr now contains only "world"
function sort (): Array//Description: Sort all array elements
var hello = new Array ("E", "A", "B");
Hello. Sort ();
print (hello);//Printing A, B, c
function Unshift (Newelement:object, optionalelement:object): Int//Description: Unshift adds one or more elements to the beginning of the array and returns the new array length.
var arr = new Array ("Hello", "World");
Arr. Unshift ("This", "is");
Print (arr);//Printing This,is,hello,world
Unity3d Script Tutorial 3: Arrays (Array Class)