【问题标题】:How can i add items to array to the start and to the end?如何将项目添加到数组的开头和结尾?
【发布时间】:2017-09-29 23:39:12
【问题描述】:

在脚本的顶部:

private Array sections;

然后:

var bm3 = new ExtrudedTrailSectionBM3();
var section = bm3;
section.point = position;
section.matrix = transform.localToWorldMatrix;
section.time = now;
// using 2 sections
sections.Unshift(section); // back
sections.Unshift(section); // front

但 Unshift 适用于 java 脚本,我使用的是 csharp。

【问题讨论】:

  • 老兄,您需要选择一种语言并针对该语言发布问题
  • 在 c# 中不要使用Array。使用List<T> 然后在任何时候添加都会轻而易举

标签: c# unity3d unity5


【解决方案1】:

首先,我只建议使用数组来在检查器中编辑公共变量,因为它们太原始了。

如果您特别需要或想要使用数组,则每次都需要重新创建它以添加数据或调整其大小。

尽管使用列表:D

public class temp : MonoBehaviour {
public List<string> myList;
// Use this for initialization
void Start () {

    //initialize your list
    myList = new List<string> ();
    //add to your list
    myList.Add("some junk");
    string temp = "some more junk";
    myList.Add (temp);
    //add a new item at a specific index
    myList.Insert(0,"The new first item");
    //remove the first item: "The new first item"
    myList.RemoveAt(0);
    //remove everything
    myList.RemoveAll();


}

注意:请考虑在将来正确格式化您的问题,因为其他人将来很难找到信息导致重复问题:)

【讨论】:

  • 用您选择的任何数据类型替换字符串
猜你喜欢
  • 2014-06-01
  • 2014-02-12
  • 1970-01-01
  • 2018-11-25
  • 1970-01-01
  • 2018-08-03
  • 2010-09-28
  • 1970-01-01
  • 2022-12-11
相关资源
最近更新 更多