数组允许您在单个变量中存储多个对象。
Array 类仅在 Javascript 中可用。
以下是关于使用数组类能够执行哪些操作的基本示例:
在 Unity 中有两种类型的数组,即内置数组和常规的 Javascript 数组。
内置数组(原生 .NET 数组)非常迅速高效,但无法调整大小。
它们属于静态类型,能够在检视面板中进行编辑。以下是关于如何使用内置数组的基本示例:
// example c# script showing how // an array can be implemented. using UnityEngine; using System.Collections;
public class ExampleClass : MonoBehaviour { // Exposes an float array in the inspector, which you can edit there. public float[] values; void Start() { foreach (float 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.0F; } }
内置数组在性能关键代码中十分有用(借助 Unity 的 javascript 和内置数组,您可以使用 mesh interface 在一秒内轻松处理 200 万个顶点。)
另一方面,常规的 Javascript 数组可以调整大小、排序,并且还可以执行您希望数组类能够执行的所有其他操作。
Javascript 数组不会在检视面板中显示。
注意:您可以轻松在 Javascript 数组和内置数组之间进行转换。
注意,按照 Unity 的命名惯例,Array 函数采用大写形式。
为了方便 javascript 用户使用,Unity 还接受 Array 类的小写函数。
注意:Unity 不支持“列表中的列表”或“数组中的数组”的序列化。
length | 长度属性,可返回或设置 Array 中的元素数量。 |
Array | 创建一个固定大小的数组。 |
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.