using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Reflection; public class Test01 : MonoBehaviour { void Start () { FieldInfo[] array = GetComponent<Test01>().GetType().GetFields(); foreach (var item in array) { print("字段名稱:" + item); } } [DataStoreSave] public string str = "abc"; [DataStoreSave] public int i = 5; [DataStoreSave] public bool b = true; [DataStoreSave] public Vector3 V3 = new Vector3(4, 5, 6); }
結果如下圖:
Type.GetFields
返回當前 Type 的所有公共字段。
注意要引用命名空間:using System.Reflection;