如何将项目添加到数组的开头和结尾?
How can i add items to array to the start and to the end?
在脚本的顶部:
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。
首先,我只建议对要在检查器中编辑的 public 变量使用数组,因为它们太原始了。
如果您特别需要或想要使用数组,则每次都需要重新创建它以添加数据或以其他方式调整它的大小。
尽管使用列表 :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();
}
注意:请考虑在将来正确格式化您的问题,因为其他人将来很难找到信息,从而导致重复问题:)
在脚本的顶部:
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。
首先,我只建议对要在检查器中编辑的 public 变量使用数组,因为它们太原始了。
如果您特别需要或想要使用数组,则每次都需要重新创建它以添加数据或以其他方式调整它的大小。
尽管使用列表 :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();
}
注意:请考虑在将来正确格式化您的问题,因为其他人将来很难找到信息,从而导致重复问题:)