(感谢张子阳老师的著作《.Net之美》一书,感谢hcw_peter老师分享的关于DataTable文章)
1.创建Define类,创建状态枚举
public enum BookingStatus { 未提交 = 1, 已提交, 已取消, 受理中, 已退回, 已定妥 = 6, 已过期 } public class Defines { public Defines() { } }
2.在Unity中创建实例,使用反射遍历枚举字段
using UnityEngine; using System.Collections; using System.Data; using System.Data.SqlClient; using System; using System.Collections.Generic; using UnityEditorInternal.VersionControl; using System.Reflection; using UnityEngine.UI; using System.IO; using System.Xml; public class SoftwareRun : MonoBehaviour { public DataTable m_dataTable = new DataTable(); Text text; void Start () { m_dataTable = GetDataTable("StudyReflection"); text = GameObject.Find("Text").GetComponent<Text>(); ConvertToXmlToString(m_dataTable); } void Update () { } private DataTable GetDataTable(string _dataTableName) { //创建类型 Type t = typeof(BookingStatus); //获取字段信息对象集合 FieldInfo[] fileArray = t.GetFields(); DataTable table = new DataTable(_dataTableName); //创建列 table.Columns.Add("Name", Type.GetType("System.String")); table.Columns.Add("Value", Type.GetType("System.Int32")); //遍历集合 foreach (FieldInfo field in fileArray) { if (!field.IsSpecialName) { DataRow row = table.NewRow(); row[0] = field.Name; row[1] = Convert.ToInt32(field.GetRawConstantValue()); table.Rows.Add(row); } } return table; } private void ConvertToXmlToString(DataTable _dataTable) { TextWriter tw = new StringWriter(); _dataTable.TableName = _dataTable.TableName.Length == 0 ? "StudyReflection" : _dataTable.TableName; _dataTable.WriteXmlSchema(tw); _dataTable.WriteXml(tw); text.text = tw.ToString(); } }
3.最后在Unity中的编译