close

List,也就是清單,
可以把它當作是一個動態的陣列,
不需要一開始就宣告長度,
可以依需求做List的新增或移除,
也可以容易的將資料插入至List中相對應的index位置。

Example:
先引用
using System.Collections.Generic;

//部分程式碼

//List<T> 依需求宣告為int string GameObjet 或自定義的Class等型態

List<string> type = new List<string>();
//新增walk run jump sit 等狀態
type.Add("walk");
type.Add("run");
type.Add("jump");
type.Add("sit");

//搬移List裡面的資料測試
type.Insert(0,type[type.Count-1]); //type.Insert (index,data)將最後一筆資料移置index0的位置
type.RemoveAt(type.Count-1); //移除index 的資料

for(int i =0;i<type.Count;i++)
    Debug.Log ("type " + i.ToString() + ": " +type[i] +"\n");

//Output
type 0: sit
type 1: walk
type 2: run
type 3: jump

//--------

 

小補充:
使用type.Remove時須注意,
假如List中有兩筆以上相同的資料,
如:

//部分程式碼
type.Add("walk");
type.Add("run");
type.Add("walk");
type.Add("walk");

type.Remove(type[type.Count-1]);
for(int i =0;i<type.Count;i++)
Debug.Log ("type " + i.ToString() + ": " +type[i] +"\n");

//Output
type 0: run
type 1: walk
type 2: walk
//--------

被移除的將會是第一筆被搜尋到的資料
因此,如果想移除的是第三或第四筆資料
還是建議使用type.RemoveAt

大概就是這樣,
有錯有誤麻煩告知!
謝謝收看!

arrow
arrow
    文章標籤
    Unity List
    全站熱搜

    WilsonYo 發表在 痞客邦 留言(0) 人氣()