Unity的C#编程教程_41_array 数组挑战1

  • 任务说明:

    • 设定一个数组,存储 4 个名字,并打印最后一个名字
    • 设定一个数组,存储 4 个等级,对应上面的 4 个名字,打印最后一个的等级
    • 设定一个数组,存储 4 种武器,对应上面 4 个名字,打印最后一种武器
  • 附加任务:

    • 按下空格键的时候进行打印
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ArrayOverview : MonoBehaviour
{
		// 初始化 3 个数组
    public string[] names = new string[] { "Magician", "Monk", "Hero", "Demon" };
    public int[] level = new int[] { 20, 18, 16, 33 };
    public string[] weapons = new string[] { "axe", "hand", "sword", "spear" };


    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space)) // 检测按下空格
        {
            Debug.Log("The level of " + names[3] + " is " + level[3] + " and his weapon is " + weapons[3]); // 按照序号索引元素,进行打印
        }
    }
}

这里也可以先初始化空列表,然后到 unity 中进行设定元素个数和赋值

Tags: