ConfigTable.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System;
  5. public class ConfigTable : Dictionary<string, SingleData>
  6. {
  7. }
  8. [SerializeField]
  9. public class SingleField
  10. {
  11. public FieldType m_type;
  12. public string m_content;
  13. public string m_enumType;
  14. #region 构造函数
  15. public SingleField()
  16. {
  17. m_type = FieldType.String;
  18. m_content = "";
  19. }
  20. public SingleField(FieldType type,string content,string enumType)
  21. {
  22. m_type = type;
  23. m_content = content;
  24. m_enumType = enumType;
  25. if (content == null)
  26. {
  27. Reset();
  28. }
  29. }
  30. public SingleField(string contrnt)
  31. {
  32. m_type = FieldType.String;
  33. m_content = contrnt;
  34. }
  35. public SingleField(int contrnt)
  36. {
  37. m_type = FieldType.Int;
  38. m_content = contrnt.ToString();
  39. }
  40. public SingleField(float content)
  41. {
  42. m_type = FieldType.Float;
  43. m_content = content.ToString();
  44. }
  45. public SingleField(bool content)
  46. {
  47. m_type = FieldType.Bool;
  48. m_content = content.ToString();
  49. }
  50. public SingleField(Vector2 content)
  51. {
  52. m_type = FieldType.Vector2;
  53. m_content = content.ToSaveString();
  54. }
  55. public SingleField(Vector3 content)
  56. {
  57. m_type = FieldType.Vector3;
  58. m_content = content.ToSaveString();
  59. }
  60. public SingleField(Color content)
  61. {
  62. m_type = FieldType.Color;
  63. m_content = content.ToSaveString();
  64. }
  65. public SingleField(List<string> content)
  66. {
  67. m_type = FieldType.StringArray;
  68. m_content = content.ToSaveString();
  69. }
  70. public SingleField(Enum content)
  71. {
  72. m_type = FieldType.Enum;
  73. m_enumType = content.GetType().Name;
  74. m_content = content.ToString();
  75. }
  76. #endregion
  77. #region ReSet
  78. public void Reset()
  79. {
  80. switch (m_type)
  81. {
  82. case FieldType.Bool:
  83. m_content = false.ToString();
  84. break;
  85. case FieldType.Vector2:
  86. m_content = Vector2.zero.ToSaveString();
  87. break;
  88. case FieldType.Vector3:
  89. m_content = Vector3.zero.ToSaveString();
  90. break;
  91. case FieldType.Color:
  92. m_content = Color.white.ToSaveString();
  93. break;
  94. case FieldType.Float:
  95. m_content = (0.0f).ToString();
  96. break;
  97. case FieldType.Int:
  98. m_content = (0).ToString();
  99. break;
  100. case FieldType.Enum:
  101. if (m_enumType != "" && m_enumType != null)
  102. {
  103. m_content = Enum.GetValues(GetEnumType()).GetValue(0).ToString();
  104. }
  105. else
  106. {
  107. throw new Exception("EnumType is Null! ");
  108. }
  109. break;
  110. }
  111. }
  112. #endregion
  113. #region 取值封装
  114. /// <summary>
  115. /// 显示在编辑器UI上的字符串
  116. /// </summary>
  117. /// <returns></returns>
  118. public string GetShowString()
  119. {
  120. switch (m_type)
  121. {
  122. case FieldType.Bool:
  123. return GetBool().ToString();
  124. case FieldType.Vector2:
  125. return GetVector2().ToString();
  126. case FieldType.Vector3:
  127. return GetVector3().ToString();
  128. case FieldType.Color:
  129. return GetColor().ToString();
  130. case FieldType.Float:
  131. return GetFloat().ToString();
  132. case FieldType.Int:
  133. return GetInt().ToString();
  134. case FieldType.Enum:
  135. return GetEnum().ToString();
  136. }
  137. return m_content;
  138. }
  139. public int GetInt()
  140. {
  141. return int.Parse(m_content);
  142. }
  143. public float GetFloat()
  144. {
  145. return float.Parse(m_content);
  146. }
  147. public bool GetBool()
  148. {
  149. return bool.Parse(m_content);
  150. }
  151. public string GetString()
  152. {
  153. return m_content;
  154. }
  155. public string[] GetStringArray()
  156. {
  157. return ParseTool.String2StringArray(m_content);
  158. }
  159. public Vector2 GetVector2()
  160. {
  161. return ParseTool.String2Vector2(m_content);
  162. }
  163. public Vector3 GetVector3()
  164. {
  165. return ParseTool.String2Vector3(m_content);
  166. }
  167. public Color GetColor()
  168. {
  169. return ParseTool.String2Color(m_content);
  170. }
  171. public T GetEnum<T>() where T:struct
  172. {
  173. return (T)Enum.Parse(typeof(T), m_content);
  174. }
  175. public Enum GetEnum()
  176. {
  177. if (m_content == null && m_content == "")
  178. {
  179. throw new Exception("GetEnum Fail Content is null !");
  180. }
  181. if (GetEnumType() == null)
  182. {
  183. throw new Exception("GetEnum Fail GetEnumType() is null ! ->" + m_enumType + "<-");
  184. }
  185. try
  186. {
  187. return (Enum)Enum.Parse(GetEnumType(), m_content);
  188. }
  189. catch(Exception e)
  190. {
  191. throw new Exception("Enum Parse Fail! EnumType is ->" + m_enumType + "<- GetEnumType() is ->" + GetEnumType() + "<- Content is ->" + m_content + "<-\n" + e.ToString());
  192. }
  193. }
  194. public Type GetEnumType()
  195. {
  196. return Type.GetType(m_enumType);
  197. }
  198. #endregion
  199. }
  200. public enum FieldType
  201. {
  202. String,
  203. Bool,
  204. Int,
  205. Float,
  206. Vector2,
  207. Vector3,
  208. Color,
  209. Enum,
  210. StringArray,
  211. IntArray,
  212. FloatArray,
  213. BoolArray,
  214. Vector2Array,
  215. Vector3Array,
  216. }