Version: 2017.3

설명

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

The Array class is only available in Javascript.

Here is a basic example of what you can do with an array class:

//The Array class is only available in Javascript.

There are two types of arrays in Unity, builtin arrays and normal Javascript Arrays. Builtin arrays (native .NET arrays), are extremely fast and efficient but they can not be resized.

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:

// 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; } }

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. Note: You can easily convert between Javascript Arrays and builtin arrays.

//The Array class is only available in Javascript.

Note that Array 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.

변수

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

생성자

ArrayCreates an Array of a fixed size.

Public 함수

AddAdds value to the end of the array.
ClearEmpties the array. The length of the array will be zero.
ConcatConcat joins two or more arrays.
JoinJoins the contents of an array into one string.
PopRemoves the last element of the array and returns it.
PushAdds value to the end of the array.
RemoveAtRemoves the element at index from the array.
ShiftRemoves the first element of the array and returns it.
SortSorts all Array elements.
UnshiftUnshift adds one or more elements to the beginning of an array and returns the new length of the array.
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961