Unity数组

Unity数组

介绍:数组是一种将一组相同类型的数据存储在一起的方法。

声明数组:要使用C#声​​明数组, 必须首先说出将在数组中存储什么类型的数据。在类型之后, 指定一个开放的方括号, 然后立即指定一个封闭的方括号[]。这将使变量成为实际的数组。我们还需要指定数组的大小。它仅表示变量中要访问的位置数。

accessModifier datatype[] arrayname = new datatype[arraySize];

举例

//要将空值分配给数组中的所有位置, 只需编写” new”关键字, 然后键入类型, 方括号, 描述数组大小的数字, 然后是方括号。
public class ArrayExample : MonoBehaviour
{
    public int[] playerNumber= new int[5];

    void Start()
    {
        for (int i = 1; i < playerNumber.Length; i++)
        {
            playerNumber[i] = i;
            Debug.Log("Player Number: " +i.ToString());

        }
    }
}

你可能感兴趣的:(unity,unity)