123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using System;
- public class ConfigTable : Dictionary<string, SingleData>
- {
- }
- [SerializeField]
- public class SingleField
- {
- public FieldType m_type;
- public string m_content;
- public string m_enumType;
- #region 构造函数
- public SingleField()
- {
- m_type = FieldType.String;
- m_content = "";
- }
- public SingleField(FieldType type,string content,string enumType)
- {
- m_type = type;
- m_content = content;
- m_enumType = enumType;
- if (content == null)
- {
- Reset();
- }
- }
- public SingleField(string contrnt)
- {
- m_type = FieldType.String;
- m_content = contrnt;
- }
- public SingleField(int contrnt)
- {
- m_type = FieldType.Int;
- m_content = contrnt.ToString();
- }
- public SingleField(float content)
- {
- m_type = FieldType.Float;
- m_content = content.ToString();
- }
- public SingleField(bool content)
- {
- m_type = FieldType.Bool;
- m_content = content.ToString();
- }
- public SingleField(Vector2 content)
- {
- m_type = FieldType.Vector2;
- m_content = content.ToSaveString();
- }
- public SingleField(Vector3 content)
- {
- m_type = FieldType.Vector3;
- m_content = content.ToSaveString();
- }
- public SingleField(Color content)
- {
- m_type = FieldType.Color;
- m_content = content.ToSaveString();
- }
- public SingleField(List<string> content)
- {
- m_type = FieldType.StringArray;
- m_content = content.ToSaveString();
- }
- public SingleField(Enum content)
- {
- m_type = FieldType.Enum;
- m_enumType = content.GetType().Name;
- m_content = content.ToString();
- }
- #endregion
- #region ReSet
- public void Reset()
- {
- switch (m_type)
- {
- case FieldType.Bool:
- m_content = false.ToString();
- break;
- case FieldType.Vector2:
- m_content = Vector2.zero.ToSaveString();
- break;
- case FieldType.Vector3:
- m_content = Vector3.zero.ToSaveString();
- break;
- case FieldType.Color:
- m_content = Color.white.ToSaveString();
- break;
- case FieldType.Float:
- m_content = (0.0f).ToString();
- break;
- case FieldType.Int:
- m_content = (0).ToString();
- break;
- case FieldType.Enum:
- if (m_enumType != "" && m_enumType != null)
- {
- m_content = Enum.GetValues(GetEnumType()).GetValue(0).ToString();
- }
- else
- {
- throw new Exception("EnumType is Null! ");
- }
- break;
- }
- }
- #endregion
- #region 取值封装
- /// <summary>
- /// 显示在编辑器UI上的字符串
- /// </summary>
- /// <returns></returns>
- public string GetShowString()
- {
- switch (m_type)
- {
- case FieldType.Bool:
- return GetBool().ToString();
- case FieldType.Vector2:
- return GetVector2().ToString();
- case FieldType.Vector3:
- return GetVector3().ToString();
- case FieldType.Color:
- return GetColor().ToString();
- case FieldType.Float:
- return GetFloat().ToString();
- case FieldType.Int:
- return GetInt().ToString();
- case FieldType.Enum:
- return GetEnum().ToString();
- }
- return m_content;
- }
- public int GetInt()
- {
- return int.Parse(m_content);
- }
- public float GetFloat()
- {
- return float.Parse(m_content);
- }
- public bool GetBool()
- {
- return bool.Parse(m_content);
- }
- public string GetString()
- {
- return m_content;
- }
- public string[] GetStringArray()
- {
- return ParseTool.String2StringArray(m_content);
- }
- public Vector2 GetVector2()
- {
- return ParseTool.String2Vector2(m_content);
- }
- public Vector3 GetVector3()
- {
- return ParseTool.String2Vector3(m_content);
- }
- public Color GetColor()
- {
- return ParseTool.String2Color(m_content);
- }
- public T GetEnum<T>() where T:struct
- {
- return (T)Enum.Parse(typeof(T), m_content);
- }
- public Enum GetEnum()
- {
- if (m_content == null && m_content == "")
- {
- throw new Exception("GetEnum Fail Content is null !");
- }
- if (GetEnumType() == null)
- {
- throw new Exception("GetEnum Fail GetEnumType() is null ! ->" + m_enumType + "<-");
- }
- try
- {
- return (Enum)Enum.Parse(GetEnumType(), m_content);
- }
- catch(Exception e)
- {
- throw new Exception("Enum Parse Fail! EnumType is ->" + m_enumType + "<- GetEnumType() is ->" + GetEnumType() + "<- Content is ->" + m_content + "<-\n" + e.ToString());
- }
- }
- public Type GetEnumType()
- {
- return Type.GetType(m_enumType);
- }
- #endregion
- }
- public enum FieldType
- {
- String,
- Bool,
- Int,
- Float,
- Vector2,
- Vector3,
- Color,
- Enum,
- StringArray,
- IntArray,
- FloatArray,
- BoolArray,
- Vector2Array,
- Vector3Array,
- }
|