Version: 5.3 (switch to 5.4b)
IdiomaEnglish
  • C#
  • JS

Idioma de script

Selecciona tu lenguaje de programación favorito. Todos los fragmentos de código serán mostrados en este lenguaje.

Sugiere un cambio

¡Éxito!

Gracias por ayudarnos a mejorar la calidad de la documentación de Unity. A pesar de que no podemos aceptar todas las sugerencias, leemos cada cambio propuesto por nuestros usuarios y actualizaremos los que sean aplicables.

Cerrar

No se puedo enviar

Por alguna razón su cambio sugerido no pudo ser enviado. Por favor <a>intente nuevamente</a> en unos minutos. Gracias por tomarse un tiempo para ayudarnos a mejorar la calidad de la documentación de Unity.

Cerrar

Cancelar

Cambiar al Manual

Descripción

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

The Array class is only available in Javascript.

Aquí hay un ejemplo básico de lo que puede hacer con la clase Array:

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 arr[1] = "World"; // iterate through the array for (var value : String in arr) { print(value); } }
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:

// Exposes an float array in the inspector,
// which you can edit there.
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; }
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. You can easily convert between Javascript Arrays and builtin arrays.

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 var builtinArray : Vector3[] = array.ToBuiltin(Vector3) as Vector3[]; // Assign the builtin array to a js Array var newarr = new Array (builtinArray); // newarr contains the same elements as array print (newarr); }
The Array class is only available in Javascript.

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

Variables

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

Constructores

ArrayCrea un Array de tamaño fijo.

Funciones Públicas

AddAgrega value al final del array.
ClearVacía el array. El tamaño del array será cero (0).
ConcatConcat une dos o más Arrays.
JoinUne el contenido de un Array en un String.
PopElimina el último elemento del array, y lo devuelve.
PushAgrega value al final del array.
RemoveAtElimina del Array el elemento en la posición index.
ShiftElimina el primer elemento del Array y lo devuelve.
SortOrdena todos los elementos del Array.
UnshiftUnshift adds one or more elements to the beginning of an array and returns the new length of the array.