2017-09-29 50 views
-6

在腳本的頂部:如何將項目添加到數組到開始和結束?

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 

但不印字是Java腳本,我使用CSHARP。

+2

多德在未來的信息,你需要選擇一種語言併發布該語言的問題 –

+2

在c#中不要使用'Array'。使用'List '然後在任何時候添加將是微風 –

回答

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(); 


} 

注:請正確地考慮在將來格式化你的問題,因爲它成爲他人難以找到導致重複的問題:)

+0

替換字符串與您選擇的任何數據類型 – Tiaan