結構(Struct)就像輕量的class,能夠實作interface,但不可繼承的堆疊配置類型。struct是實值類型,可用來封裝一小組相關變數,例如矩形的座標或某個項目的特性。
1 2 3 4 5 6 |
public struct Cat { public int life; public string name; public Color color; } |
使用時須要將其序列化,UNITY序列化的關鍵字是Serializable和SerializeField。用法如下:
1 2 3 4 5 6 7 8 9 |
//序列化一隻貓的細節 [System.Serializable] public struct Cat { public GameObject modle; public Material material; } //宣告一隻貓 public Cat cat; |
獲得基本型態欄位。