Array-数组类只能用于Javascript。
Variables 变量
length
the length property of the array that returns or sets the number of elements in array。数组的长度属性, 返回或设置数组中元素的数量。
Functions 函数
Concat
Concat joins two or more arrays. The method does not change the existing arrays。联接两个或多个数组, 该方法不改变现有的数组。
Join
Joins the contents of an array into one string。链接数组的内容为一个字符串。
Push
Adds value to the end of the array。添加值到数组的末尾。
Add
Adds value to the end of the array。添加值到数组的末尾。
Pop
Removes the last element of the array and returns it。删除数组的最后一个元素并返回它。
Shift
Removes the first element of the array and returns it。删除数组的第一个元素并返回它。
RemoveAt
Removes the element at index from the array。从数组中移除索引为 “index” 的元素。
Unshift
Unshift adds one or more elements to the beginning of an array and returns the new length of the array。Unshift添加一个或多个元素到数组的开始位置, 并返回新的数组长度。
Clear
Empties the array. The length of the array will be zero。清空数组, 数组的长度将为零。
Reverse
Reverses the order of all elements contained in the array。颠倒数组中所有元素顺序。
Sort
Sorts all Array elements。排序所有数组元素。
参考例子为:
function Start ()
{
var arr = new Array ();
// Add one element
arr.Push (“Hello”);
// print the first element (“Hello”)
print(arr[0]);
// Resize the array
// 调整数组大小
arr.length = 2;
// Assign “World” to the second element
// 将 “World” 赋给第二个因素
arr[1] = “World”;
// iterate through the array
// 遍历这个数组
for (var value : String in arr)
{
print(value);
}
}
Unity有两种类型的数组, 内置的数组和普通的JavaScript数组。内置的数组 (原始的 “NET” 数组), 时非常快速和有效的, 但是他们不能被调整大小。它们是静态类型的, 这允许他们在检视面板中被编辑。
下面是一个如何使用内置数组的简单例子:
var values : float[];
function Start ()
{
// iterate through the array
// 遍历数组
for (var value in values) {
print(value);
}
// Since we can't resize builtin arrays, we have to recreate the array to resize it
// 由于我们不能调整内置数组的大小, 我们必须重新创建一个数组来调整其大小
values = new float[10];
// assign the second element
// 给第二个元素赋值
values[1] = 5.0;
}
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));
// Copy the js array into a builtin array
// 复制JS数组到内置数组
var builtinArray : Vector3[] = array.ToBuiltin(Vector3);
// Assign the builtin array to a js Array
// 将内置数组赋给JS数组
var newarr = new Array (builtinArray);
// newarr contains the same elements as array
// newarr包含相同的元素作为数组
print (newarr);
}
数组允许你将多个对象存储在一个变量中。Array类只能用于JavaScript 。
这是一个基本的例子,说明可以使用一个数组类做什么。
function Start( )
{
var arr = new Array ( ) ;
arr.Push (“Hello”); //添加一个元素
Print(arr[ 0]); //打印第一个元素
arr length = 2 ; //调整数组大小
arr [ 1] = “World”; //将“World”赋给第二个元素
for (var value : String in arr) //遍历这个数组
{
Print ( value );
}
}
var value : float[ ]; //在检视面板中公开一个浮点数组,你可以在那里编辑它
function Start ( )
{
for ( var value in values) //遍历数组
{
print ( value );
}
value = new float[ 10 ]; //因为我们不能调整内置数组的大小 , 我们必须重新创建一个数组来调整它的大小
value[ 1 ] = 5.0;//给第二个元素赋值
}
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 ); //拷贝js数组到内置数组
var newarr = new Array ( builtinArray ); //将内置数组赋给js数组
print ( newarr ); //newarr与array 包含相同的元素
}
注意按照Unity的命名规则下面所有函数均大写开头。为方便JavaScript用户 , Unity数组类也接受小写函数。
变量
var length : int // 描述:数组的长度属性,返回或设置数组中元素的数量。
function Start ( )
{
var arr = Array ( “Hello” , “World” ) ;
print (arr . length ) ; //打印两个
arr . Length = 5 ; //调整数组的大小为5
}
function Add ( value : object ) : void // 描述:添加 value 到数组末端。
var arr = new Array (“Hello”);
arr.Add (“ World ”);
Print ( arr ); //打印“Hello ”,“World”
function Clear ( ) : void // 描述: 清空数组。 数组的长度将为零。
var hello = new Array (“Hello ”,“World ”);
hello.Clear ( ) ; //现在hello包含零个元素
function Concat ( array :Array , optionalArray0: Array, optionalArray1 : Array):Array // 描述:连接两个或多个数组。这个方法不会改变已有的数字并返回连接后的数组拷贝
function Start ( )
{
var arr = new Array (“Hello”,“World”):
var arr2 = new Array (“!”);
var joined = arr.Concat ( arr2 ); //现在jointed包含所有3个字符串
Print ( joined ); //打印“Hello”,“World”,“!”
}
function Join ( seperator :string ) : String // 描述:链接数组内容为一个字符串。元素将被seperator字符串分割,并返回数组的拷贝
function Start ( )
{
var arr = new Array (“Hello” , “World”);
print ( arr . join (“ , ”));//打印“Hello,World”
}
function Pop ( ) : object // 描述:移除数组最后一个元素并返回它。
var arr = new Array (“Hello ”,“World”);
arr . Pop ( );
print ( arr );//只打印“Hello”
function Push (value : object) : int // 描述: 添加value到数组末端。并返回新数组长度。
var arr = new Array (“Hello”);
arr.Push (“World”);//Unity3D教程手册:www.unitymanual.com
print ( arr );//打印“Hello”,“World”
function RemoveAt (index : int ) : void // 描述:从数组中移除索引为index的元素。
var arr = new Array (“Hello” , “ and good morning” , “World ”);
arr.Remove ( 1 ) ; //移除 “and good morning”
print ( arr );//打印 “ Hello World ”
function Reverse () : Array // 描述:颠倒数组中所有元素顺序。
var hello = new Array (“ Hello ” ,“ World ”) ;
hello Reverse( ) ;
print (hello);//打印World,Hello
function Shift ( ) :object // 描述:移除数组的第一个元素并返回它。
var arr = new Array ( “ Hello ” , “ World ”);
arr . Shift ( ) ;//Unity3D教程手册:www.unitymanual.com
print ( “ World ” ) ; //现在arr只包含“ World ”
function Sort( ) : Array // 描述:排序所有数组元素
var hello = new Array ( “ e ” ,“ a ” ,“ b ”);
hello . Sort ( ) ;
print ( hello ) ;// 打印 a ,b ,c
function Unshift ( newElement : object , optionalElement : object ) : int // 描述: Unshift 添加一个或多个元素到数组的开始位置并返回新的数组长度。
var arr = new Array (“ Hello ”,“ World ”);
arr . Unshift (“ This ”,“ is ”);
print ( arr ) ;//打印 This,is,Hello,World