Unity3D圣典3.3版本-中文版
文集大纲加载中……
本文档使用 MrDoc 发布
-
+
首页
Array 数组
# 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 see [here](http://msdn2.microsoft.com/en-us/library/system.collections.arraylist_members%28VS.80%29.aspx). 数组允许你在一个变量中储存多个对象。 这个 数组类仅仅用于 Javascript。更多用于 C#或 Javascript 关于 ArrayLists,字典或哈希表,看[这里](http://msdn2.microsoft.com/en-us/library/system.collections.arraylist_members(VS.80).aspx)。 这里是一个你能用数组类做什么的基本的例子。 * [C#](#) * [JavaScript](#) Array 不能用于 C#. ``` function Start () { var arr = new Array (); // 添加一个元素 arr.Push ("Hello"); // 打印第一个元素"Hello" print(arr[0]); // 调整数组大小 arr.length = 2; // 把 "World" 赋给第二个元素 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 有两种类型的数组,内置数组和标准的 Javascript 数组。 *Builtin arrays (native .NET arrays), are extremely fast and efficient but they can not be resized.* 内置的数组 (原始的.NET 数组), 是非常快速和有效的, 但是他们不能被调整大小。 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#. ``` // 在检视面板中暴露一个浮点数组,你可以在那里编辑它 var values : float[]; function Start () { // 遍历数组 for (var value in values) { print(value); } // 由于我们不能调整内置数组的大小, 我们必须重新创建一个数组来调整其大小 values = new float[10]; // 给第二个元素赋值 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. * 内置数组在性能相关代码中是非常有用的 (使用 Unity javascript 数组和内置数组可以很容易使用 [mesh interface](../Mesh.html) 在一秒内处理 200 万个顶点)。 另一方面,标准 Javascript 数组可以调整大小、排序,并可以做所有你期望的数组类的操作,Javascript 数组不显示在检视面板。你能很容易的在 Javascript 数组和内置数组之间转换。 * [C#](#) * [JavaScript](#) Array 不能用于 C#. ``` 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)); // 复制JS数组到内置数组 var builtinArray : Vector3[] = array.ToBuiltin(Vector3); // 将内置数组赋给JS数组 var newarr = new Array (builtinArray); // newarr包含相同的元素作为数组 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 用户,Unity 也接受数组类小写函数。 *注意:* Unity 不支持一个列表的列表序列化,不是一个数组的数组。 ### Variables**变量** * [length](Array.length.html) the length property of the array that returns or sets the number of elements in array. 数组的长度属性, 返回或设置数组中元素的数量 。 ### Constructors**构造函数** * [Array](Array.Array.html) Creates an Array of a fixed size. 创建一个固定大小的数组。 ### Functions**函数** * [Concat](Array.Concat.html) Concat joins two or more arrays. The method does not change the existing arrays 连接两个或更多数组,这个方法不会改变现有的数组。 * [Join](Array.Join.html) Joins the contents of an array into one string. 连接数组的内容到一个字符串。 * [Push](Array.Push.html) Adds value to the end of the array. 在数组的末端添加值。 * [Add](Array.Add.html) Adds value to the end of the array. 在数组的末端添加值。 * [Pop](Array.Pop.html) Removes the last element of the array and returns it. 移除数组最后一个元素并返回它。 * [Shift](Array.Shift.html) Removes the first element of the array and returns it. 移除数组第一个元素并返回它。 * [RemoveAt](Array.RemoveAt.html) Removes the element at index from the array. 从数组中移除位于索引的元素。 * [Unshift](Array.Unshift.html) Unshift adds one or more elements to the beginning of an array and returns the new length of the array. 在数组开始位置添加一个或多个元素并返回新的数组长度。 * [Clear](Array.Clear.html) Empties the array. The length of the array will be zero. 清空数组,数组长度将变为 0。 * [Reverse](Array.Reverse.html) Reverses the order of all elements contained in the array. 反转数组中所有元素的顺序。 * [Sort](Array.Sort.html) Sorts all Array elements 排序所有数组元素 。
da
2022年5月13日 23:06
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
关于 MrDoc
觅思文档MrDoc
是
州的先生
开发并开源的在线文档系统,其适合作为个人和小型团队的云笔记、文档和知识库管理工具。
如果觅思文档给你或你的团队带来了帮助,欢迎对作者进行一些打赏捐助,这将有力支持作者持续投入精力更新和维护觅思文档,感谢你的捐助!
>>>捐助鸣谢列表
微信
支付宝
QQ
PayPal
Markdown文件
分享
链接
类型
密码
更新密码