Version: 2017.3

描述

数组的长度属性,可返回或设置数组中的元素数量。

Array.length 能够控制 JS 如何访问或设置该数组的大小。 以下 JS 脚本示例显示了以 小写 "l" 开头的数组长度属性。

注意: 该页面包括一个 c-sharp 脚本示例。显示了使用的两种数组类型 以及它们使用的长度。

// C# array length example
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { void Start() { // use string array approach string[] names = new string[] {"Hello", "World", "Really"};

foreach (string s in names) { print("user is: " + s); }

print("length is: " + names.Length);

// use ArrayList approach ArrayList arr = new ArrayList(); arr.Add("Hello"); arr.Add("World");

// ArrayList uses Count instead of Length print(arr.Count); } }