找回密码
 注册帐号

扫一扫,访问微社区

array 鏁扮粍_unity3d娓告垙鑴氭湰鍒朵綔鏁欑▼-娓告垙铔墰鍑哄搧 ...

2014-12-31 12:11| 发布者: 杨炎| 查看: 2005| 评论: 0|原作者: 蛮牛|来自: unity3d脚本manual

摘要: array鏁扮粍arrays allow you to store multiple objects in a single variable.the array class is only only available in javascript. for more information about arraylists, dictionaries or hashtables in c# ...

array鏁扮粍

arrays allow you to store multiple objects in a single variable.

the array class is only only available in javascript. for more information about arraylists, dictionaries or hashtables in c# or javascript seehere.

鏁扮粍鍏佽浣犲湪涓€涓彉閲忎腑鍌ㄥ瓨澶氫釜瀵硅薄銆?/p>

杩欎釜 鏁扮粍绫讳粎浠呯敤浜嶫avascript銆傛洿澶氱敤浜嶤#鎴朖avascript鍏充簬arraylists锛屽瓧鍏告垨鍝堝笇琛紝鐪?a href="http://msdn2.microsoft.com/en-us/library/system.collections.arraylist_members(vs.80).aspx">杩欓噷銆?/p>

杩欓噷鏄竴涓綘鑳界敤鏁扮粍绫诲仛浠€涔堢殑鍩烘湰鐨勪緥瀛愩€?/p>

c#

javascript

array涓嶈兘鐢ㄤ簬c#.

function start () {

var arr = new array ();

// 娣诲姞涓€涓厓绱?/span>

arr.push ("hello");

// 鎵撳嵃绗竴涓厓绱?hello"

print(arr[0]);

// 璋冩暣鏁扮粍澶у皬

arr.length = 2;

// 鎶?"world" 璧嬬粰绗簩涓厓绱?/span>

arr[1] = "world";

// 閬嶅巻杩欎釜鏁扮粍

for (var value : string in arr) {

print(value);

}

}

there are two types of arrays in unity, builtin arrays and normal javascript arrays.

unity 鏈変袱绉嶇被鍨嬬殑鏁扮粍锛屽唴缃暟缁勫拰鏍囧噯鐨凧avascript鏁扮粍銆?/p>

builtin arrays (native .net arrays), are extremely fast and efficient but they can not be resized.

鍐呯疆鐨勬暟缁?(鍘熷鐨?net 鏁扮粍), 鏄潪甯稿揩閫熷拰鏈夋晥鐨? 浣嗘槸浠栦滑涓嶈兘琚皟鏁村ぇ灏忋€?/p>

they are statically typed which allows them to be edited in the inspector. here is a basic example of how you can use builtin arrays.

瀹冧滑鏄潤鎬佺被鍨嬬殑, 杩欏厑璁镐粬浠湪妫€瑙嗛潰鏉夸腑琚紪杈戙€?

杩欓噷鏄竴涓浣曚娇鐢ㄥ唴缃暟缁勭殑鍩烘湰渚嬪瓙

c#

javascript

array涓嶈兘鐢ㄤ簬c#.

// 鍦ㄦ瑙嗛潰鏉夸腑鏆撮湶涓€涓诞鐐规暟缁勶紝浣犲彲浠ュ湪閭i噷缂栬緫瀹?/span>

var values : float[];

function start () {

// 閬嶅巻鏁扮粍

for (var value in values) {

print(value);

}

// 鐢变簬鎴戜滑涓嶈兘璋冩暣鍐呯疆鏁扮粍鐨勫ぇ灏? 鎴戜滑蹇呴』閲嶆柊鍒涘缓涓€涓暟缁勬潵璋冩暣鍏跺ぇ灏?/span>

values = new float[10];

// 缁欑浜屼釜鍏冪礌璧嬪€?/span>

values[1] = 5.0;

}

builtin arrays are useful in performance critical code (with unity's javascript and builtin arrays you could easily process 2 million vertices using the mesh interface in one second.)

normal javascript arrays on the other hand can be resized, sorted and can do all other operations you would expect from an array class. javascript arrays do not show up in the inspector.you can easily convert between javascript arrays and builtin arrays.

鍐呯疆鏁扮粍鍦ㄦ€ц兘鐩稿叧浠g爜涓槸闈炲父鏈夌敤鐨?(浣跨敤unity javascript 鏁扮粍鍜屽唴缃暟缁勫彲浠ュ緢瀹规槗浣跨敤mesh interface鍦ㄤ竴绉掑唴澶勭悊200涓囦釜椤剁偣)銆?/p>

鍙︿竴鏂归潰锛屾爣鍑咼avascript鏁扮粍鍙互璋冩暣澶у皬銆佹帓搴忥紝骞跺彲浠ュ仛鎵€鏈変綘鏈熸湜鐨勬暟缁勭被鐨勬搷浣滐紝javascript鏁扮粍涓嶆樉绀哄湪妫€瑙嗛潰鏉裤€備綘鑳藉緢瀹规槗鐨勫湪javascript鏁扮粍鍜屽唴缃暟缁勪箣闂磋浆鎹€?/p>

c#

javascript

array涓嶈兘鐢ㄤ簬c#.

function start () {

var array = new array (vector3(0, 0, 1));

array.push(vector3(0, 0, 2));

array.push(vector3(0, 0, 3));

// 澶嶅埗js鏁扮粍鍒板唴缃暟缁?/span>

var builtinarray :vector3[] = array.tobuiltin(vector3);

// 灏嗗唴缃暟缁勮祴缁橨s鏁扮粍

var newarr = new array (builtinarray);

// newarr鍖呭惈鐩稿悓鐨勫厓绱犱綔涓烘暟缁?/span>

print (newarr);

}

note that in the following all functions are upper case following unity's naming convention. as a convenience for javascript users, unity also accepts lower case functions for the array class.

note:unity doesn't support serialization of a list of lists, nor an array of arrays.

娉ㄦ剰鎸夌収unity鐨勫懡鍚嶈鍒欎笅闈㈡墍鏈夊嚱鏁伴瀛楁瘝鍧囦负澶у啓锛屼负鏂逛究javascript鐢ㄦ埛锛孶nity涔熸帴鍙楁暟缁勭被灏忓啓鍑芥暟銆?/p>

娉ㄦ剰锛?/em>unity涓嶆敮鎸佷竴涓垪琛ㄧ殑鍒楄〃搴忓垪鍖栵紝涓嶆槸涓€涓暟缁勭殑鏁扮粍銆?/p>

variables鍙橀噺

length

the length property of the array that returns or sets the number of elements in array.

鏁扮粍鐨勯暱搴﹀睘鎬? 杩斿洖鎴栬缃暟缁勪腑鍏冪礌鐨勬暟閲? 銆?/div>

constructors鏋勯€犲嚱鏁?/span>

array

creates an array of a fixed size.

鍒涘缓涓€涓浐瀹氬ぇ灏忕殑鏁扮粍銆?/div>

functions鍑芥暟

concat

concat joins two or more arrays. the method does not change the existing arrays

杩炴帴涓や釜鎴栨洿澶氭暟缁勶紝杩欎釜鏂规硶涓嶄細鏀瑰彉鐜版湁鐨勬暟缁勩€?/div>

join

joins the contents of an array into one string.

杩炴帴鏁扮粍鐨勫唴瀹瑰埌涓€涓瓧绗︿覆銆?/div>

push

adds value to the end of the array.

鍦ㄦ暟缁勭殑鏈娣诲姞鍊笺€?/div>

add

adds value to the end of the array.

鍦ㄦ暟缁勭殑鏈娣诲姞鍊笺€?/div>

pop

removes the last element of the array and returns it.

绉婚櫎鏁扮粍鏈€鍚庝竴涓厓绱犲苟杩斿洖瀹冦€?/div>

shift

removes the first element of the array and returns it.

绉婚櫎鏁扮粍绗竴涓厓绱犲苟杩斿洖瀹冦€?/div>

removeat

removes the element at index from the array.

浠庢暟缁勪腑绉婚櫎浣嶄簬绱㈠紩鐨勫厓绱犮€?/div>

unshift

unshift adds one or more elements to the beginning of an array and returns the new length of the array.

鍦ㄦ暟缁勫紑濮嬩綅缃坊鍔犱竴涓垨澶氫釜鍏冪礌骞惰繑鍥炴柊鐨勬暟缁勯暱搴︺€?/div>

clear

empties the array. the length of the array will be zero.

娓呯┖鏁扮粍锛屾暟缁勯暱搴﹀皢鍙樹负0銆?/div>

reverse

reverses the order of all elements contained in the array.

鍙嶈浆鏁扮粍涓墍鏈夊厓绱犵殑椤哄簭銆?/div>

sort

sorts all array elements

鎺掑簭鎵€鏈夋暟缁勫厓绱? 銆?/div>

上一篇:array.concat 连接下一篇:array.join 连接

相关阅读

文章点评
相关文章