【发布时间】:2020-10-17 17:27:48
【问题描述】:
我刚刚开始学习新的硕士课程以提升我的职业生涯,我正在从头开始学习 c# 以进行 Unity 3D 游戏编程。 我得到了我的第一个每周任务,我很不知道我应该做什么。我被要求创建一个任何类型的数组,并通过一个开关能够从数组中添加一个元素或删除一个元素。我设法打印以记录我的初始数组,然后切换到添加大小写并向索引添加一个元素,但我遇到的问题是我只设法将一个元素添加到数组中。之后,我重新创建的辅助数组重置为每次返回添加案例时只添加一个。如果你能帮助我一些指针,那就太好了。
这是我的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EjercicioSemanal_01 : MonoBehaviour
{
public enum manageEnum {None, Add, Delete};
public manageEnum enumValue;
public int[] array = new int[5] {1, 2, 3, 4, 5};
int[] newArray = new int[5];
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
switch (enumValue)
{
case manageEnum.None:
if (newArray.Length > array.Length)
{
for (int i = 0; i < newArray.Length; i++)
{
Debug.Log(newArray[i]);
}
}
else
{
for (int j = 0; j < array.Length; j++)
{
Debug.Log(array[j]);
}
}
break;
case manageEnum.Add:
int itemToAdd = 0;
newArray = new int[array.Length + 1];
for (int i = 0; i < array.Length; i++)
{
newArray[i] = array[i];
}
for (int j = 0; j < newArray.Length; j++)
{
Debug.Log(newArray[j]);
}
break;
case manageEnum.Delete:
break;
default:
break;
}
}
}
【问题讨论】:
标签: c# unity3d game-engine