├── .gitignore ├── README.md └── src ├── Controller ├── CController.cs ├── CPage.cs ├── CPageSwitch.cs ├── CScene.cs ├── Input │ ├── INPUT_INFO.cs │ └── UIEvent.cs └── UILAYER.cs ├── Model ├── CModel.cs ├── CModelMgr.cs ├── IModel.cs └── LitJson.dll ├── Other └── SingletonMono.cs ├── Table ├── CTable.cs ├── SimpleJSON.cs └── TableMgr.cs └── View └── CView.cs /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | *.meta 3 | 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Unity3DMVC 2 | ============ 3 | 4 | 专为unity3d打造的数据层架构
5 | 6 | # 还未完成,抱歉 7 | 8 | //本mvc架构主要是为了提高开发速度,可能与其他mvc架构不同。
9 | //本架构重在理解软件架构与u3d开发方式
10 | //加上没有文档解释,我暂时定个理解难度系数:8
11 | -------------------------------------------------------------------------------- /src/Controller/CController.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System; 3 | using System.Collections.Generic; 4 | 5 | //using BUTTON_CALLBACK = System.Action<> 6 | 7 | 8 | // UIControllerBase.cs 9 | // Author: Lu Zexi 10 | // 2014-07-05 11 | 12 | 13 | 14 | /// 15 | /// the ui controller. 16 | /// 17 | public abstract class CController : MonoBehaviour 18 | { 19 | /// 20 | /// Gets the layer. 21 | /// 22 | /// The layer. 23 | public virtual UILAYER GetLayer(){return UILAYER.NONE;} 24 | 25 | /// 26 | /// Gets the root path. 27 | /// 28 | /// The root path. 29 | public abstract GameObject GetAnchor(); 30 | 31 | /// 32 | /// Init this instance. 33 | /// 34 | public virtual void Init() 35 | { 36 | this.transform.parent = GetAnchor().transform; 37 | this.transform.localPosition = Vector3.zero; 38 | this.transform.localScale = Vector3.one; 39 | } 40 | 41 | 42 | //============================ set parent function ========================== 43 | 44 | /// 45 | /// SEs the t_ PAREN. 46 | /// 47 | /// Child. 48 | /// Parent. 49 | /// If set to true layerfix. 50 | public void SET_PARENT(GameObject child , MonoBehaviour parent , bool layerfix = false) 51 | { 52 | SET_PARENT(child.transform,parent.transform, layerfix); 53 | } 54 | 55 | /// 56 | /// set the parent. 57 | /// 58 | /// Child. 59 | /// Parent. 60 | /// If set to true layerfix. 61 | public void SET_PARENT( MonoBehaviour child , GameObject parent, bool layerfix = false) 62 | { 63 | SET_PARENT(child.transform,parent.transform, layerfix); 64 | } 65 | 66 | /// 67 | /// set the parent. 68 | /// 69 | /// Child. 70 | /// Parent. 71 | /// If set to true layerfix. 72 | public void SET_PARENT( GameObject child , GameObject parent , bool layerfix = false) 73 | { 74 | SET_PARENT(child.transform,parent.transform , layerfix); 75 | } 76 | 77 | /// 78 | /// Set the parent. 79 | /// 80 | /// Child. 81 | /// Tmp_parent. 82 | /// If set to true layerfix. 83 | public void SET_PARENT( Transform child , Transform tmp_parent , bool layerfix = false) 84 | { 85 | child.parent = tmp_parent; 86 | Vector3 pos = Vector3.zero; 87 | if( layerfix ) 88 | { 89 | pos = Vector3.zero; 90 | } 91 | child.localPosition = pos; 92 | child.localScale = Vector3.one; 93 | } 94 | 95 | //========================= FIND function ================================== 96 | 97 | /// 98 | /// find the gameobject. 99 | /// 100 | /// 101 | /// 102 | public GameObject FIND(string path) 103 | { 104 | return this.transform.Find(path).gameObject; 105 | } 106 | 107 | /// 108 | /// find the mono scripts. 109 | /// 110 | /// 111 | /// 112 | /// 113 | public T FIND(string path) where T : MonoBehaviour 114 | { 115 | return this.transform.Find(path).GetComponent(); 116 | } 117 | 118 | /// 119 | /// find the gameobject. 120 | /// 121 | /// 122 | /// 123 | /// 124 | public GameObject FIND(GameObject obj, string path) 125 | { 126 | return obj.transform.Find(path).gameObject; 127 | } 128 | 129 | /// 130 | /// find the mono scripts 131 | /// 132 | /// 133 | /// 134 | /// 135 | /// 136 | public T FIND(GameObject obj, string path) where T : MonoBehaviour 137 | { 138 | return obj.transform.Find(path).GetComponent(); 139 | } 140 | 141 | //========================= UI EVENT Regist Function ================================== 142 | // 143 | //======================================================================== 144 | } 145 | 146 | -------------------------------------------------------------------------------- /src/Controller/CPage.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System; 3 | using System.Collections; 4 | using System.Collections.Generic; 5 | 6 | 7 | // CPage.cs 8 | // Author: Lu Zexi 9 | // 2014-09-19 10 | 11 | 12 | 13 | /// 14 | /// page. 15 | /// 16 | public class CPage : MonoBehaviour 17 | where P : MonoBehaviour 18 | where V : CView 19 | where C : CController 20 | { 21 | public static V s_cView = null; //view 22 | public static C s_cController = null; //controller 23 | private static P s_cInstance; //instance 24 | 25 | public bool IsShow{get;private set;} //is show 26 | 27 | public static P sInstance 28 | { 29 | get 30 | { 31 | if( s_cInstance == null ) 32 | { 33 | Type t = typeof(P); 34 | GameObject obj = new GameObject(t.Name); 35 | s_cInstance = obj.AddComponent

(); 36 | s_cView = obj.AddComponent(); 37 | s_cController = obj.AddComponent(); 38 | } 39 | return s_cInstance; 40 | } 41 | } 42 | 43 | ///

44 | /// Raises the load complete event. 45 | /// 46 | /// Res map. 47 | public virtual void OnLoadComplete(Dictionary resMap) 48 | { 49 | s_cView.m_mapRes = resMap; 50 | s_cController.Init(); 51 | } 52 | 53 | /// 54 | /// Show this instance. 55 | /// 56 | public virtual void Show() 57 | { 58 | IsShow = true; 59 | } 60 | 61 | /// 62 | /// Init this instance. 63 | /// 64 | public virtual void Init() 65 | { 66 | s_cController.Init(); 67 | } 68 | 69 | /// 70 | /// Hiden this instance. 71 | /// 72 | public virtual void Hiden() 73 | { 74 | GameObject.Destroy(this.gameObject); 75 | Resources.UnloadUnusedAssets(); 76 | GC.Collect(); 77 | } 78 | 79 | /// 80 | /// Raises the destroy event. 81 | /// 82 | void OnDestroy() 83 | { 84 | IsShow = false; 85 | s_cInstance = null; 86 | s_cView = null; 87 | s_cController = null; 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /src/Controller/CPageSwitch.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | // UIController.cs 5 | // Author: LU Zexi 6 | // 2014-09-15 7 | 8 | 9 | 10 | public enum SwitchEffect 11 | { 12 | None, 13 | Fade, 14 | Loading, 15 | PretaskNone, 16 | } 17 | 18 | 19 | /// 20 | /// page controller. 21 | /// 22 | public class CPageSwitch 23 | { 24 | public static void SwitchUI(SwitchEffect effect = SwitchEffect.None) 25 | // where T : CPage<> 26 | { 27 | // T.sInstace.Show(); 28 | } 29 | 30 | public static void SwitchScene() 31 | { 32 | // 33 | } 34 | } 35 | 36 | -------------------------------------------------------------------------------- /src/Controller/CScene.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System; 3 | using System.Collections; 4 | 5 | // BaseScene.cs 6 | // Author: Lu Zexi 7 | // 2014-07-16 8 | 9 | 10 | /// 11 | /// Base scene. 12 | /// 13 | public class CScene 14 | { 15 | protected static CScene s_cCurrentScene; 16 | 17 | public static bool Is(Type type) 18 | { 19 | return type == s_cCurrentScene.GetType(); 20 | } 21 | 22 | public static void Switch() 23 | where T : CScene , new() 24 | { 25 | if(s_cCurrentScene != null ) 26 | s_cCurrentScene.OnExit(); 27 | s_cCurrentScene = new T(); 28 | s_cCurrentScene.OnEnter(); 29 | } 30 | 31 | /// 32 | /// Raises the enter event. 33 | /// 34 | public virtual void OnEnter() 35 | { 36 | // 37 | } 38 | 39 | /// 40 | /// Raises the exit event. 41 | /// 42 | public virtual void OnExit() 43 | { 44 | // 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/Controller/Input/INPUT_INFO.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | 6 | // INPUT_INFO.cs 7 | // Author: Lu Zexi 8 | // 2014-07-06 9 | 10 | 11 | /// 12 | /// input type 13 | /// 14 | public enum INPUT_TYPE 15 | { 16 | NONE = 0, 17 | HOVER, 18 | PRESS, 19 | SELECT, 20 | CLICK, 21 | DOUBLE_CLICK, 22 | DRAG, 23 | DROP, 24 | INPUT, 25 | TOOLTIP, 26 | SCROLL, 27 | KEY, 28 | MAX 29 | } 30 | 31 | /// 32 | /// the info of the input. 33 | /// 34 | public class INPUT_INFO 35 | { 36 | public INPUT_TYPE m_eType; 37 | public float m_fDelta; 38 | public Vector2 m_vecDelta; 39 | public Vector2 m_vecPos; 40 | public bool m_bDone; 41 | public GameObject m_cTarget; 42 | public string m_strText; 43 | public KeyCode m_eKey; 44 | } 45 | -------------------------------------------------------------------------------- /src/Controller/Input/UIEvent.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | using UnityEngine.EventSystems; 5 | 6 | 7 | 8 | // UIEvent.cs 9 | // Author: Lu Zexi 10 | // 2014-07-06 11 | 12 | 13 | /// 14 | /// ui event. 15 | /// 16 | public class UIEvent : UnityEngine.EventSystems.EventTrigger 17 | { 18 | public delegate void PointerEventDelegate ( BaseEventData eventData , GameObject go , object[] arg); 19 | public delegate void BaseEventDelegate ( BaseEventData eventData , GameObject go , object[] arg ); 20 | public delegate void AxisEventDelegate ( BaseEventData eventData , GameObject go , object[] arg ); 21 | 22 | public object[] m_vecArg = null; 23 | 24 | public BaseEventDelegate onDeselect = null; 25 | public PointerEventDelegate onDrag = null; 26 | public PointerEventDelegate onDrop = null; 27 | public AxisEventDelegate onMove = null; 28 | public PointerEventDelegate onClick = null; 29 | public PointerEventDelegate onDown = null; 30 | public PointerEventDelegate onEnter = null; 31 | public PointerEventDelegate onExit = null; 32 | public PointerEventDelegate onUp = null; 33 | public PointerEventDelegate onScroll = null; 34 | public PointerEventDelegate onSelect = null; 35 | public PointerEventDelegate onUpdateSelect = null; 36 | 37 | public static UIEvent Get(GameObject go) 38 | { 39 | UIEvent listener = go.GetComponent(); 40 | if (listener == null) listener = go.AddComponent(); 41 | return listener; 42 | } 43 | 44 | public static UIEvent Get(MonoBehaviour go) 45 | { 46 | UIEvent listener = go.GetComponent(); 47 | if (listener == null) listener = go.gameObject.AddComponent(); 48 | return listener; 49 | } 50 | 51 | public override void OnDeselect( BaseEventData eventData ) 52 | { 53 | if(onDeselect != null) onDeselect(eventData , gameObject , this.m_vecArg); 54 | } 55 | 56 | public override void OnDrag( PointerEventData eventData ) 57 | { 58 | if(onDrag != null) onDrag(eventData , gameObject , this.m_vecArg); 59 | } 60 | 61 | public override void OnDrop( PointerEventData eventData ) 62 | { 63 | if(onDrop != null) onDrop(eventData , gameObject , this.m_vecArg); 64 | } 65 | 66 | public override void OnMove( AxisEventData eventData ) 67 | { 68 | if(onMove != null) onMove(eventData , gameObject , this.m_vecArg); 69 | } 70 | 71 | public override void OnPointerClick(PointerEventData eventData) 72 | { 73 | if(onClick != null) onClick(eventData , gameObject , this.m_vecArg); 74 | } 75 | 76 | public override void OnPointerDown (PointerEventData eventData) 77 | { 78 | if(onDown != null) onDown(eventData , gameObject , this.m_vecArg); 79 | } 80 | 81 | public override void OnPointerEnter (PointerEventData eventData) 82 | { 83 | if(onEnter != null) onEnter(eventData , gameObject , this.m_vecArg); 84 | } 85 | 86 | public override void OnPointerExit (PointerEventData eventData) 87 | { 88 | if(onExit != null) onExit(eventData , gameObject , this.m_vecArg); 89 | } 90 | public override void OnPointerUp (PointerEventData eventData) 91 | { 92 | if(onUp != null) onUp(eventData , gameObject , this.m_vecArg); 93 | } 94 | 95 | public override void OnScroll( PointerEventData eventData ) 96 | { 97 | if(onScroll != null) onScroll(eventData , gameObject , this.m_vecArg); 98 | } 99 | 100 | public override void OnSelect (BaseEventData eventData) 101 | { 102 | if(onSelect != null) onSelect(eventData , gameObject , this.m_vecArg); 103 | } 104 | 105 | public override void OnUpdateSelected (BaseEventData eventData) 106 | { 107 | if(onUpdateSelect != null) onUpdateSelect(eventData , gameObject , this.m_vecArg); 108 | } 109 | } 110 | 111 | -------------------------------------------------------------------------------- /src/Controller/UILAYER.cs: -------------------------------------------------------------------------------- 1 | /// 2 | /// UI层级 3 | /// 4 | public enum UILAYER 5 | { 6 | NONE = -1, 7 | GUI_BACKGROUND = 0, //背景层 8 | GUI_MENU = 1, //菜单层0 9 | GUI_MENU1, //菜单层1 10 | GUI_PANEL, //面板层 11 | GUI_PANEL1, //面板1层 12 | GUI_PANEL2, //面板2层 13 | GUI_PANEL3, //面板3层 14 | GUI_FULL, //满屏层 15 | GUI_LOADING, //加载等待层 16 | GUI_MESSAGE, //消息层0 17 | GUI_MESSAGE1, //消息层1 18 | GUI_LOCKPANEL, //Lock面板层 19 | } 20 | -------------------------------------------------------------------------------- /src/Model/CModel.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System; 3 | using System.Collections; 4 | using System.Collections.Generic; 5 | 6 | 7 | // Model.cs 8 | // Author: Lu Zexi 9 | // 2014-07-05 10 | 11 | 12 | 13 | /// 14 | /// Model类 15 | /// 16 | public abstract class CModel : ScriptableObject , IEnumerable 17 | { 18 | protected static List s_lstData; 19 | 20 | public CModel() 21 | { 22 | Type t = this.GetType(); 23 | if (s_lstData == null) 24 | { 25 | s_lstData = CModelMgr.sInstance.Get(t.FullName); 26 | if(s_lstData == null ) 27 | { 28 | s_lstData = new List(); 29 | CModelMgr.sInstance.Add(t.FullName, s_lstData); 30 | } 31 | } 32 | } 33 | 34 | //count 35 | public static int Count 36 | { 37 | get { return s_lstData.Count; } 38 | } 39 | 40 | //get index 41 | public static T Get(int index) 42 | { 43 | if (index >= s_lstData.Count||index<0) 44 | return default(T); 45 | return (T)s_lstData[index]; 46 | } 47 | 48 | //get index 49 | public T this[int index] 50 | { 51 | get 52 | { 53 | if (index >= s_lstData.Count||index<0) 54 | return default(T); 55 | return (T)s_lstData[index]; 56 | } 57 | } 58 | 59 | /// 60 | /// get the enumerator. 61 | /// 62 | /// 63 | public IEnumerator GetEnumerator() 64 | { 65 | foreach (object item in s_lstData) 66 | yield return item; 67 | } 68 | 69 | /// 70 | /// add the model instance. 71 | /// 72 | /// 73 | public static void Add(T model) 74 | { 75 | s_lstData.Add(model); 76 | } 77 | 78 | /// 79 | /// Remove this instance. 80 | /// 81 | public static void Clear() 82 | { 83 | s_lstData.Clear(); 84 | } 85 | 86 | /// 87 | /// Gets the array. 88 | /// 89 | /// The array. 90 | /// The 1st type parameter. 91 | public static T[] ToArray() 92 | { 93 | T[] vec = new T[s_lstData.Count]; 94 | for(int i = 0 ; i 100 | /// remove the index instance. 101 | /// 102 | /// 103 | public static void Remove(int index) 104 | { 105 | if (index >= s_lstData.Count) 106 | return; 107 | s_lstData.RemoveAt(index); 108 | } 109 | 110 | /// 111 | /// remove the instance in the list. 112 | /// 113 | /// 114 | public static void Remove(T model) 115 | { 116 | s_lstData.Remove(model); 117 | } 118 | } 119 | 120 | -------------------------------------------------------------------------------- /src/Model/CModelMgr.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System; 3 | using System.Reflection; 4 | using System.IO; 5 | using System.Collections; 6 | using System.Collections.Generic; 7 | using LitJson; 8 | 9 | 10 | // ModelManager.cs 11 | // Author: Lu Zexi 12 | // 2014-07-23 13 | 14 | 15 | 16 | /// 17 | /// Model管理类 18 | /// 19 | public class CModelMgr 20 | { 21 | private Dictionary> m_mapData = new Dictionary>(); 22 | 23 | private static CModelMgr s_cInstance; 24 | public static CModelMgr sInstance 25 | { 26 | get 27 | { 28 | if (s_cInstance == null) 29 | { 30 | s_cInstance = new CModelMgr(); 31 | } 32 | return s_cInstance; 33 | } 34 | } 35 | 36 | public CModelMgr() 37 | { 38 | // 39 | } 40 | 41 | /// 42 | /// Save this instance. 43 | /// 44 | public void Save(string pathFile) 45 | { 46 | string json = JsonMapper.ToJson(this.m_mapData); 47 | File.WriteAllText(pathFile , json); 48 | // JSONClass json = new JSONClass(); 49 | // foreach( KeyValuePair> item in this.m_mapData ) 50 | // { 51 | // if(item.Value.Count > 0 ) 52 | // { 53 | // JSONArray arrayNode = new JSONArray(); 54 | // foreach( Model mod in item.Value ) 55 | // { 56 | // JSONClass classNode = new JSONClass(); 57 | // Type t = mod.GetType(); 58 | // FieldInfo[] fis = t.GetFields(BindingFlags.Public | BindingFlags.Instance); 59 | // foreach (FieldInfo f in fis) 60 | // { 61 | // classNode[f.Name] = f.GetValue(mod).ToString(); 62 | // //Type fieldType = f.FieldType; 63 | // } 64 | // arrayNode.Add(classNode); 65 | // } 66 | // json[item.Key] = arrayNode; 67 | // } 68 | // } 69 | // json.SaveToFile(pathFile); 70 | } 71 | 72 | /// 73 | /// Load the specified pathFile. 74 | /// 75 | /// Path file. 76 | public void Load(string pathFile) 77 | { 78 | this.m_mapData.Clear(); 79 | string json = File.ReadAllText(pathFile); 80 | this.m_mapData = JsonMapper.ToObject>>(json); 81 | // JSONClass json = JSONNode.LoadFromFile(pathFile) as JSONClass; 82 | // foreach( KeyValuePair item in json ) 83 | // { 84 | // this.m_mapData.Add(item.Key , new List()); 85 | // JSONArray arrayJson = item.Value.AsArray; 86 | // foreach( JSONNode node in arrayJson ) 87 | // { 88 | // Type modt = Type.GetType(item.Key); 89 | // Model mod = ScriptableObject.CreateInstance(modt) as Model; 90 | // //Model mod = FormatterServices.GetUninitializedObject(modt) as Model; 91 | // FieldInfo[] fis = modt.GetFields(BindingFlags.Public | BindingFlags.Instance); 92 | // foreach (FieldInfo f in fis) 93 | // { 94 | // Type t = f.FieldType; 95 | // JSONNode valueJson = node[f.Name]; 96 | // if(valueJson == null) 97 | // continue; 98 | // if (t.IsPrimitive) 99 | // { 100 | // if (t.Equals (typeof (int))) f.SetValue(mod,int.Parse(valueJson.Value)); 101 | // else if (t.Equals (typeof (uint))) f.SetValue(mod,uint.Parse(valueJson.Value)); 102 | // else if (t.Equals (typeof (float))) f.SetValue(mod,float.Parse(valueJson.Value)); 103 | // else if (t.Equals (typeof (double))) f.SetValue(mod,double.Parse(valueJson.Value)); 104 | // else if (t.Equals (typeof (long))) f.SetValue(mod,long.Parse(valueJson.Value)); 105 | // else if (t.Equals (typeof (ulong))) f.SetValue(mod, ulong.Parse(valueJson.Value)); 106 | // else if (t.Equals (typeof (bool))) f.SetValue(mod, bool.Parse(valueJson.Value)); 107 | // else if (t.Equals (typeof (byte))) f.SetValue(mod, byte.Parse(valueJson.Value)); 108 | // else if (t.Equals (typeof (sbyte))) f.SetValue(mod, sbyte.Parse(valueJson.Value)); 109 | // else if (t.Equals (typeof (short))) f.SetValue(mod, short.Parse(valueJson.Value)); 110 | // else if (t.Equals (typeof (ushort))) f.SetValue(mod, ushort.Parse(valueJson.Value)); 111 | // else if (t.Equals (typeof (char))) f.SetValue(mod, char.Parse(valueJson.Value)); 112 | // else if (t.Equals (typeof(string))) f.SetValue(mod, valueJson.Value); 113 | // else 114 | // { 115 | // Debug.LogError(t.Name); 116 | // } 117 | // } else if( t.Equals(typeof(string))) 118 | // { 119 | // f.SetValue(mod,valueJson.Value); 120 | // } 121 | // } 122 | // mod.Add(mod); 123 | // } 124 | // } 125 | } 126 | 127 | /// 128 | /// get the map data 129 | /// 130 | /// 131 | /// 132 | internal List Get(string name) 133 | { 134 | if (!this.m_mapData.ContainsKey(name)) 135 | return null; 136 | return this.m_mapData[name]; 137 | } 138 | 139 | /// 140 | /// add the data. 141 | /// 142 | /// 143 | /// 144 | internal void Add(string name, List lst) 145 | { 146 | this.m_mapData.Add(name, lst); 147 | } 148 | 149 | /// 150 | /// remove model 151 | /// 152 | /// 153 | internal void Remove(string name) 154 | { 155 | if (!this.m_mapData.ContainsKey(name)) 156 | return; 157 | this.m_mapData.Remove(name); 158 | } 159 | } 160 | -------------------------------------------------------------------------------- /src/Model/IModel.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | 5 | // IModel.cs 6 | // Author: Lu Zexi 7 | // 2014-09-20 8 | 9 | 10 | /// 11 | /// interface model. 12 | /// 13 | public interface IModel 14 | { 15 | //count 16 | int Count 17 | { 18 | get; 19 | } 20 | 21 | // T Get(int index) where T : IModel; 22 | } 23 | 24 | -------------------------------------------------------------------------------- /src/Model/LitJson.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luzexi/Unity3DMVC/702c1b3d878876b1ca685854f01cc5ee192647f0/src/Model/LitJson.dll -------------------------------------------------------------------------------- /src/Other/SingletonMono.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | 5 | 6 | // SingletonMono.cs 7 | // Author: Lu Zexi 8 | // 2014-09-15 9 | 10 | 11 | 12 | /// 13 | /// The MonoBehavior Singleton. 14 | /// 15 | public abstract class SingletonMono : MonoBehaviour where T : SingletonMono 16 | { 17 | private static readonly string dontDestroyObjectName = "DontDestroyObject_" + typeof(T).Name; 18 | private static T mInstance = null; 19 | public static T sInstance 20 | { 21 | get 22 | { 23 | if (mInstance == null) 24 | { 25 | GameObject go = GameObject.Find(dontDestroyObjectName); 26 | if (go == null) 27 | { 28 | go = new GameObject(dontDestroyObjectName); 29 | } 30 | mInstance = go.AddComponent() as T; 31 | } 32 | return mInstance; 33 | } 34 | } 35 | 36 | void Awake() 37 | { 38 | if (mInstance == null) 39 | { 40 | GameObject go = GameObject.Find(dontDestroyObjectName); 41 | if (go != gameObject) 42 | { 43 | Destroy(gameObject); 44 | } 45 | else 46 | { 47 | mInstance = this as T; 48 | gameObject.name = dontDestroyObjectName; 49 | DontDestroyOnLoad(gameObject); 50 | init(); 51 | } 52 | } 53 | else 54 | { 55 | if (mInstance.gameObject != gameObject) 56 | { 57 | Destroy(gameObject); 58 | } 59 | Destroy(this); 60 | } 61 | } 62 | 63 | public virtual void init() {} 64 | } 65 | -------------------------------------------------------------------------------- /src/Table/CTable.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System; 3 | using System.Linq; 4 | using System.Reflection; 5 | using System.Collections; 6 | using System.Collections.Generic; 7 | 8 | 9 | // CTable.cs 10 | // Author: Lu Zexi 11 | // 2014-07-05 12 | 13 | 14 | 15 | /// 16 | /// CTable类 17 | /// 18 | public abstract class CTable : ScriptableObject , IEnumerable 19 | { 20 | protected List s_lstData; 21 | 22 | public CTable() 23 | { 24 | Type t = this.GetType(); 25 | this.s_lstData = TableMgr.sInstance.Get(t.FullName); 26 | if (this.s_lstData == null) 27 | { 28 | this.s_lstData = new List(); 29 | TableMgr.sInstance.Add(t.FullName, this.s_lstData); 30 | } 31 | } 32 | 33 | public int Count 34 | { 35 | get { return s_lstData.Count; } 36 | } 37 | 38 | /// 39 | /// get the instance. 40 | /// 41 | /// 42 | /// 43 | /// 44 | public T Get(int index) where T : CTable 45 | { 46 | if (index >= this.s_lstData.Count) 47 | return default(T); 48 | return this.s_lstData[index] as T; 49 | } 50 | 51 | public CTable this[int index] 52 | { 53 | get { if (index >= s_lstData.Count||index<0) return null; return s_lstData[index]; } 54 | } 55 | 56 | /// 57 | /// get the enumerator. 58 | /// 59 | /// 60 | public IEnumerator GetEnumerator() 61 | { 62 | foreach (CTable item in s_lstData) 63 | yield return item; 64 | } 65 | 66 | /// 67 | /// Read the specified content. 68 | /// 69 | /// Content. 70 | public virtual void Read(string content) 71 | { 72 | this.s_lstData.Clear(); 73 | string[,] vecStr = SplitCsvGrid(content); 74 | for (int j = 2; j < vecStr.GetLength(1)-1; j++) 75 | { 76 | Type tableType = GetType(); 77 | CTable tb = ScriptableObject.CreateInstance(tableType) as CTable; 78 | FieldInfo[] fis = tableType.GetFields(BindingFlags.Public | BindingFlags.Instance); 79 | for (int i = 0; i < vecStr.GetLength(0)-1; i++) 80 | { 81 | FieldInfo f = fis[i]; 82 | Type t = f.FieldType; 83 | if (t.IsPrimitive) 84 | { 85 | if (t.Equals (typeof (int))) f.SetValue(tb,int.Parse(vecStr[i, j])); 86 | else if (t.Equals (typeof (uint))) f.SetValue(tb,uint.Parse(vecStr[i, j])); 87 | else if (t.Equals (typeof (float))) f.SetValue(tb,float.Parse(vecStr[i, j])); 88 | else if (t.Equals (typeof (double))) f.SetValue(tb,double.Parse(vecStr[i, j])); 89 | else if (t.Equals (typeof (long))) f.SetValue(tb,long.Parse(vecStr[i, j])); 90 | else if (t.Equals (typeof (ulong))) f.SetValue(tb, ulong.Parse(vecStr[i, j])); 91 | else if (t.Equals (typeof (bool))) f.SetValue(tb, bool.Parse(vecStr[i, j])); 92 | else if (t.Equals (typeof (byte))) f.SetValue(tb, byte.Parse(vecStr[i, j])); 93 | else if (t.Equals (typeof (sbyte))) f.SetValue(tb, sbyte.Parse(vecStr[i, j])); 94 | else if (t.Equals (typeof (short))) f.SetValue(tb, short.Parse(vecStr[i, j])); 95 | else if (t.Equals (typeof (ushort))) f.SetValue(tb, ushort.Parse(vecStr[i, j])); 96 | else if (t.Equals (typeof (char))) f.SetValue(tb, char.Parse(vecStr[i, j])); 97 | else if (t.Equals (typeof(string))) f.SetValue(tb, vecStr[i, j]); 98 | else 99 | { 100 | Debug.LogError(t.Name); 101 | } 102 | } else if( t.Equals(typeof(string))) 103 | { 104 | f.SetValue(tb,vecStr[i, j]); 105 | } 106 | 107 | } 108 | tb.Add(tb); 109 | } 110 | } 111 | 112 | /// 113 | /// add the model instance. 114 | /// 115 | /// 116 | public void Add(CTable model) 117 | { 118 | s_lstData.Add(model); 119 | } 120 | 121 | /// 122 | /// Remove this instance. 123 | /// 124 | public void Remove() 125 | { 126 | s_lstData.Clear(); 127 | } 128 | 129 | /// 130 | /// remove the index instance. 131 | /// 132 | /// 133 | public void Remove(int index) 134 | { 135 | if (index >= s_lstData.Count) 136 | return; 137 | s_lstData.RemoveAt(index); 138 | } 139 | 140 | /// 141 | /// remove the instance in the list. 142 | /// 143 | /// 144 | public void Remove(CTable model) 145 | { 146 | s_lstData.Remove(model); 147 | } 148 | 149 | // splits a CSV file into a 2D string array 150 | private string[,] SplitCsvGrid(string csvText) 151 | { 152 | string[] lines = csvText.Split("\n"[0]); 153 | Debug.Log("lines " + lines.Length); 154 | 155 | // finds the max width of row 156 | int width = 0; 157 | for (int i = 0; i < lines.Length; i++) 158 | { 159 | string[] row = SplitCsvLine(lines[i]); 160 | width = Mathf.Max(width, row.Length); 161 | } 162 | 163 | // creates new 2D string grid to output to 164 | string[,] outputGrid = new string[width + 1, lines.Length + 1]; 165 | for (int y = 0; y < lines.Length; y++) 166 | { 167 | string[] row = SplitCsvLine(lines[y]); 168 | for (int x = 0; x < row.Length; x++) 169 | { 170 | outputGrid[x, y] = row[x]; 171 | 172 | // This line was to replace "" with " in my output. 173 | // Include or edit it as you wish. 174 | outputGrid[x, y] = outputGrid[x, y].Replace("\"\"", "\""); 175 | } 176 | } 177 | 178 | return outputGrid; 179 | } 180 | 181 | /// 182 | /// 分离解析CSV行 183 | /// 184 | /// 185 | /// 186 | private string[] SplitCsvLine(string line) 187 | { 188 | return ( 189 | from System.Text.RegularExpressions.Match m in System.Text.RegularExpressions.Regex.Matches 190 | ( 191 | line,@"(((?(?=[,\r\n]+))|""(?([^""]|"""")+)""|(?[^,\r\n]+)),?)", 192 | System.Text.RegularExpressions.RegexOptions.ExplicitCapture) 193 | select m.Groups[1].Value).ToArray(); 194 | } 195 | } 196 | 197 | -------------------------------------------------------------------------------- /src/Table/SimpleJSON.cs: -------------------------------------------------------------------------------- 1 | //#define USE_SharpZipLib 2 | #if !UNITY_WEBPLAYER 3 | #define USE_FileIO 4 | #endif 5 | 6 | /* * * * * 7 | * A simple JSON Parser / builder 8 | * ------------------------------ 9 | * 10 | * It mainly has been written as a simple JSON parser. It can build a JSON string 11 | * from the node-tree, or generate a node tree from any valid JSON string. 12 | * 13 | * If you want to use compression when saving to file / stream / B64 you have to include 14 | * SharpZipLib ( http://www.icsharpcode.net/opensource/sharpziplib/ ) in your project and 15 | * define "USE_SharpZipLib" at the top of the file 16 | * 17 | * Written by Bunny83 18 | * 2012-06-09 19 | * 20 | * Features / attributes: 21 | * - provides strongly typed node classes and lists / dictionaries 22 | * - provides easy access to class members / array items / data values 23 | * - the parser ignores data types. Each value is a string. 24 | * - only double quotes (") are used for quoting strings. 25 | * - values and names are not restricted to quoted strings. They simply add up and are trimmed. 26 | * - There are only 3 types: arrays(JSONArray), objects(JSONClass) and values(JSONData) 27 | * - provides "casting" properties to easily convert to / from those types: 28 | * int / float / double / bool 29 | * - provides a common interface for each node so no explicit casting is required. 30 | * - the parser try to avoid errors, but if malformed JSON is parsed the result is undefined 31 | * 32 | * 33 | * 2012-12-17 Update: 34 | * - Added internal JSONLazyCreator class which simplifies the construction of a JSON tree 35 | * Now you can simple reference any item that doesn't exist yet and it will return a JSONLazyCreator 36 | * The class determines the required type by it's further use, creates the type and removes itself. 37 | * - Added binary serialization / deserialization. 38 | * - Added support for BZip2 zipped binary format. Requires the SharpZipLib ( http://www.icsharpcode.net/opensource/sharpziplib/ ) 39 | * The usage of the SharpZipLib library can be disabled by removing or commenting out the USE_SharpZipLib define at the top 40 | * - The serializer uses different types when it comes to store the values. Since my data values 41 | * are all of type string, the serializer will "try" which format fits best. The order is: int, float, double, bool, string. 42 | * It's not the most efficient way but for a moderate amount of data it should work on all platforms. 43 | * 44 | * * * * */ 45 | using System; 46 | using System.Collections; 47 | using System.Collections.Generic; 48 | using System.Linq; 49 | 50 | 51 | namespace Game.Table 52 | { 53 | public enum JSONBinaryTag 54 | { 55 | Array = 1, 56 | Class = 2, 57 | Value = 3, 58 | IntValue = 4, 59 | DoubleValue = 5, 60 | BoolValue = 6, 61 | FloatValue = 7, 62 | } 63 | 64 | public class JSONNode 65 | { 66 | #region common interface 67 | public virtual void Add(string aKey, JSONNode aItem){ } 68 | public virtual JSONNode this[int aIndex] { get { return null; } set { } } 69 | public virtual JSONNode this[string aKey] { get { return null; } set { } } 70 | public virtual string Value { get { return ""; } set { } } 71 | public virtual int Count { get { return 0; } } 72 | 73 | public virtual void Add(JSONNode aItem) 74 | { 75 | Add("", aItem); 76 | } 77 | 78 | public virtual JSONNode Remove(string aKey) { return null; } 79 | public virtual JSONNode Remove(int aIndex) { return null; } 80 | public virtual JSONNode Remove(JSONNode aNode) { return aNode; } 81 | 82 | public virtual IEnumerable Childs { get { yield break;} } 83 | public IEnumerable DeepChilds 84 | { 85 | get 86 | { 87 | foreach (var C in Childs) 88 | foreach (var D in C.DeepChilds) 89 | yield return D; 90 | } 91 | } 92 | 93 | public override string ToString() 94 | { 95 | return "JSONNode"; 96 | } 97 | public virtual string ToString(string aPrefix) 98 | { 99 | return "JSONNode"; 100 | } 101 | 102 | #endregion common interface 103 | 104 | #region typecasting properties 105 | public virtual int AsInt 106 | { 107 | get 108 | { 109 | int v = 0; 110 | if (int.TryParse(Value,out v)) 111 | return v; 112 | return 0; 113 | } 114 | set 115 | { 116 | Value = value.ToString(); 117 | } 118 | } 119 | public virtual float AsFloat 120 | { 121 | get 122 | { 123 | float v = 0.0f; 124 | if (float.TryParse(Value,out v)) 125 | return v; 126 | return 0.0f; 127 | } 128 | set 129 | { 130 | Value = value.ToString(); 131 | } 132 | } 133 | public virtual double AsDouble 134 | { 135 | get 136 | { 137 | double v = 0.0; 138 | if (double.TryParse(Value,out v)) 139 | return v; 140 | return 0.0; 141 | } 142 | set 143 | { 144 | Value = value.ToString(); 145 | } 146 | } 147 | public virtual bool AsBool 148 | { 149 | get 150 | { 151 | bool v = false; 152 | if (bool.TryParse(Value,out v)) 153 | return v; 154 | return !string.IsNullOrEmpty(Value); 155 | } 156 | set 157 | { 158 | Value = (value)?"true":"false"; 159 | } 160 | } 161 | public virtual JSONArray AsArray 162 | { 163 | get 164 | { 165 | return this as JSONArray; 166 | } 167 | } 168 | public virtual JSONClass AsObject 169 | { 170 | get 171 | { 172 | return this as JSONClass; 173 | } 174 | } 175 | 176 | 177 | #endregion typecasting properties 178 | 179 | #region operators 180 | public static implicit operator JSONNode(string s) 181 | { 182 | return new JSONData(s); 183 | } 184 | public static implicit operator string(JSONNode d) 185 | { 186 | return (d == null)?null:d.Value; 187 | } 188 | public static bool operator ==(JSONNode a, object b) 189 | { 190 | if (b == null && a is JSONLazyCreator) 191 | return true; 192 | return System.Object.ReferenceEquals(a,b); 193 | } 194 | 195 | public static bool operator !=(JSONNode a, object b) 196 | { 197 | return !(a == b); 198 | } 199 | public override bool Equals (object obj) 200 | { 201 | return System.Object.ReferenceEquals(this, obj); 202 | } 203 | public override int GetHashCode () 204 | { 205 | return base.GetHashCode(); 206 | } 207 | 208 | 209 | #endregion operators 210 | 211 | internal static string Escape(string aText) 212 | { 213 | string result = ""; 214 | foreach(char c in aText) 215 | { 216 | switch(c) 217 | { 218 | case '\\' : result += "\\\\"; break; 219 | case '\"' : result += "\\\""; break; 220 | case '\n' : result += "\\n" ; break; 221 | case '\r' : result += "\\r" ; break; 222 | case '\t' : result += "\\t" ; break; 223 | case '\b' : result += "\\b" ; break; 224 | case '\f' : result += "\\f" ; break; 225 | default : result += c ; break; 226 | } 227 | } 228 | return result; 229 | } 230 | 231 | public static JSONNode Parse(string aJSON) 232 | { 233 | Stack stack = new Stack(); 234 | JSONNode ctx = null; 235 | int i = 0; 236 | string Token = ""; 237 | string TokenName = ""; 238 | bool QuoteMode = false; 239 | while (i < aJSON.Length) 240 | { 241 | switch (aJSON[i]) 242 | { 243 | case '{': 244 | if (QuoteMode) 245 | { 246 | Token += aJSON[i]; 247 | break; 248 | } 249 | stack.Push(new JSONClass()); 250 | if (ctx != null) 251 | { 252 | TokenName = TokenName.Trim(); 253 | if (ctx is JSONArray) 254 | ctx.Add(stack.Peek()); 255 | else if (TokenName != "") 256 | ctx.Add(TokenName,stack.Peek()); 257 | } 258 | TokenName = ""; 259 | Token = ""; 260 | ctx = stack.Peek(); 261 | break; 262 | 263 | case '[': 264 | if (QuoteMode) 265 | { 266 | Token += aJSON[i]; 267 | break; 268 | } 269 | 270 | stack.Push(new JSONArray()); 271 | if (ctx != null) 272 | { 273 | TokenName = TokenName.Trim(); 274 | if (ctx is JSONArray) 275 | ctx.Add(stack.Peek()); 276 | else if (TokenName != "") 277 | ctx.Add(TokenName,stack.Peek()); 278 | } 279 | TokenName = ""; 280 | Token = ""; 281 | ctx = stack.Peek(); 282 | break; 283 | 284 | case '}': 285 | case ']': 286 | if (QuoteMode) 287 | { 288 | Token += aJSON[i]; 289 | break; 290 | } 291 | if (stack.Count == 0) 292 | throw new Exception("JSON Parse: Too many closing brackets"); 293 | 294 | stack.Pop(); 295 | if (Token != "") 296 | { 297 | TokenName = TokenName.Trim(); 298 | if (ctx is JSONArray) 299 | ctx.Add(Token); 300 | else if (TokenName != "") 301 | ctx.Add(TokenName,Token); 302 | } 303 | TokenName = ""; 304 | Token = ""; 305 | if (stack.Count>0) 306 | ctx = stack.Peek(); 307 | break; 308 | 309 | case ':': 310 | if (QuoteMode) 311 | { 312 | Token += aJSON[i]; 313 | break; 314 | } 315 | TokenName = Token; 316 | Token = ""; 317 | break; 318 | 319 | case '"': 320 | QuoteMode ^= true; 321 | break; 322 | 323 | case ',': 324 | if (QuoteMode) 325 | { 326 | Token += aJSON[i]; 327 | break; 328 | } 329 | if (Token != "") 330 | { 331 | if (ctx is JSONArray) 332 | ctx.Add(Token); 333 | else if (TokenName != "") 334 | ctx.Add(TokenName, Token); 335 | } 336 | TokenName = ""; 337 | Token = ""; 338 | break; 339 | 340 | case '\r': 341 | case '\n': 342 | break; 343 | 344 | case ' ': 345 | case '\t': 346 | if (QuoteMode) 347 | Token += aJSON[i]; 348 | break; 349 | 350 | case '\\': 351 | ++i; 352 | if (QuoteMode) 353 | { 354 | char C = aJSON[i]; 355 | switch (C) 356 | { 357 | case 't' : Token += '\t'; break; 358 | case 'r' : Token += '\r'; break; 359 | case 'n' : Token += '\n'; break; 360 | case 'b' : Token += '\b'; break; 361 | case 'f' : Token += '\f'; break; 362 | case 'u': 363 | { 364 | string s = aJSON.Substring(i+1,4); 365 | Token += (char)int.Parse(s, System.Globalization.NumberStyles.AllowHexSpecifier); 366 | i += 4; 367 | break; 368 | } 369 | default : Token += C; break; 370 | } 371 | } 372 | break; 373 | 374 | default: 375 | Token += aJSON[i]; 376 | break; 377 | } 378 | ++i; 379 | } 380 | if (QuoteMode) 381 | { 382 | throw new Exception("JSON Parse: Quotation marks seems to be messed up."); 383 | } 384 | return ctx; 385 | } 386 | 387 | public virtual void Serialize(System.IO.BinaryWriter aWriter) {} 388 | 389 | public void SaveToStream(System.IO.Stream aData) 390 | { 391 | var W = new System.IO.BinaryWriter(aData); 392 | Serialize(W); 393 | } 394 | 395 | #if USE_SharpZipLib 396 | public void SaveToCompressedStream(System.IO.Stream aData) 397 | { 398 | using (var gzipOut = new ICSharpCode.SharpZipLib.BZip2.BZip2OutputStream(aData)) 399 | { 400 | gzipOut.IsStreamOwner = false; 401 | SaveToStream(gzipOut); 402 | gzipOut.Close(); 403 | } 404 | } 405 | 406 | public void SaveToCompressedFile(string aFileName) 407 | { 408 | #if USE_FileIO 409 | System.IO.Directory.CreateDirectory((new System.IO.FileInfo(aFileName)).Directory.FullName); 410 | using(var F = System.IO.File.OpenWrite(aFileName)) 411 | { 412 | SaveToCompressedStream(F); 413 | } 414 | #else 415 | throw new Exception("Can't use File IO stuff in webplayer"); 416 | #endif 417 | } 418 | public string SaveToCompressedBase64() 419 | { 420 | using (var stream = new System.IO.MemoryStream()) 421 | { 422 | SaveToCompressedStream(stream); 423 | stream.Position = 0; 424 | return System.Convert.ToBase64String(stream.ToArray()); 425 | } 426 | } 427 | 428 | #else 429 | public void SaveToCompressedStream(System.IO.Stream aData) 430 | { 431 | throw new Exception("Can't use compressed functions. You need include the SharpZipLib and uncomment the define at the top of SimpleJSON"); 432 | } 433 | public void SaveToCompressedFile(string aFileName) 434 | { 435 | throw new Exception("Can't use compressed functions. You need include the SharpZipLib and uncomment the define at the top of SimpleJSON"); 436 | } 437 | public string SaveToCompressedBase64() 438 | { 439 | throw new Exception("Can't use compressed functions. You need include the SharpZipLib and uncomment the define at the top of SimpleJSON"); 440 | } 441 | #endif 442 | 443 | public void SaveToFile(string aFileName) 444 | { 445 | #if USE_FileIO 446 | System.IO.Directory.CreateDirectory((new System.IO.FileInfo(aFileName)).Directory.FullName); 447 | using(var F = System.IO.File.OpenWrite(aFileName)) 448 | { 449 | SaveToStream(F); 450 | } 451 | #else 452 | throw new Exception("Can't use File IO stuff in webplayer"); 453 | #endif 454 | } 455 | public string SaveToBase64() 456 | { 457 | using (var stream = new System.IO.MemoryStream()) 458 | { 459 | SaveToStream(stream); 460 | stream.Position = 0; 461 | return System.Convert.ToBase64String(stream.ToArray()); 462 | } 463 | } 464 | public static JSONNode Deserialize(System.IO.BinaryReader aReader) 465 | { 466 | JSONBinaryTag type = (JSONBinaryTag)aReader.ReadByte(); 467 | switch(type) 468 | { 469 | case JSONBinaryTag.Array: 470 | { 471 | int count = aReader.ReadInt32(); 472 | JSONArray tmp = new JSONArray(); 473 | for(int i = 0; i < count; i++) 474 | tmp.Add(Deserialize(aReader)); 475 | return tmp; 476 | } 477 | case JSONBinaryTag.Class: 478 | { 479 | int count = aReader.ReadInt32(); 480 | JSONClass tmp = new JSONClass(); 481 | for(int i = 0; i < count; i++) 482 | { 483 | string key = aReader.ReadString(); 484 | var val = Deserialize(aReader); 485 | tmp.Add(key, val); 486 | } 487 | return tmp; 488 | } 489 | case JSONBinaryTag.Value: 490 | { 491 | return new JSONData(aReader.ReadString()); 492 | } 493 | case JSONBinaryTag.IntValue: 494 | { 495 | return new JSONData(aReader.ReadInt32()); 496 | } 497 | case JSONBinaryTag.DoubleValue: 498 | { 499 | return new JSONData(aReader.ReadDouble()); 500 | } 501 | case JSONBinaryTag.BoolValue: 502 | { 503 | return new JSONData(aReader.ReadBoolean()); 504 | } 505 | case JSONBinaryTag.FloatValue: 506 | { 507 | return new JSONData(aReader.ReadSingle()); 508 | } 509 | 510 | default: 511 | { 512 | throw new Exception("Error deserializing JSON. Unknown tag: " + type); 513 | } 514 | } 515 | } 516 | 517 | #if USE_SharpZipLib 518 | public static JSONNode LoadFromCompressedStream(System.IO.Stream aData) 519 | { 520 | var zin = new ICSharpCode.SharpZipLib.BZip2.BZip2InputStream(aData); 521 | return LoadFromStream(zin); 522 | } 523 | public static JSONNode LoadFromCompressedFile(string aFileName) 524 | { 525 | #if USE_FileIO 526 | using(var F = System.IO.File.OpenRead(aFileName)) 527 | { 528 | return LoadFromCompressedStream(F); 529 | } 530 | #else 531 | throw new Exception("Can't use File IO stuff in webplayer"); 532 | #endif 533 | } 534 | public static JSONNode LoadFromCompressedBase64(string aBase64) 535 | { 536 | var tmp = System.Convert.FromBase64String(aBase64); 537 | var stream = new System.IO.MemoryStream(tmp); 538 | stream.Position = 0; 539 | return LoadFromCompressedStream(stream); 540 | } 541 | #else 542 | public static JSONNode LoadFromCompressedFile(string aFileName) 543 | { 544 | throw new Exception("Can't use compressed functions. You need include the SharpZipLib and uncomment the define at the top of SimpleJSON"); 545 | } 546 | public static JSONNode LoadFromCompressedStream(System.IO.Stream aData) 547 | { 548 | throw new Exception("Can't use compressed functions. You need include the SharpZipLib and uncomment the define at the top of SimpleJSON"); 549 | } 550 | public static JSONNode LoadFromCompressedBase64(string aBase64) 551 | { 552 | throw new Exception("Can't use compressed functions. You need include the SharpZipLib and uncomment the define at the top of SimpleJSON"); 553 | } 554 | #endif 555 | 556 | public static JSONNode LoadFromStream(System.IO.Stream aData) 557 | { 558 | using(var R = new System.IO.BinaryReader(aData)) 559 | { 560 | return Deserialize(R); 561 | } 562 | } 563 | public static JSONNode LoadFromFile(string aFileName) 564 | { 565 | #if USE_FileIO 566 | using(var F = System.IO.File.OpenRead(aFileName)) 567 | { 568 | return LoadFromStream(F); 569 | } 570 | #else 571 | throw new Exception("Can't use File IO stuff in webplayer"); 572 | #endif 573 | } 574 | public static JSONNode LoadFromBase64(string aBase64) 575 | { 576 | var tmp = System.Convert.FromBase64String(aBase64); 577 | var stream = new System.IO.MemoryStream(tmp); 578 | stream.Position = 0; 579 | return LoadFromStream(stream); 580 | } 581 | } // End of JSONNode 582 | 583 | public class JSONArray : JSONNode, IEnumerable 584 | { 585 | private List m_List = new List(); 586 | public override JSONNode this[int aIndex] 587 | { 588 | get 589 | { 590 | if (aIndex<0 || aIndex >= m_List.Count) 591 | return new JSONLazyCreator(this); 592 | return m_List[aIndex]; 593 | } 594 | set 595 | { 596 | if (aIndex<0 || aIndex >= m_List.Count) 597 | m_List.Add(value); 598 | else 599 | m_List[aIndex] = value; 600 | } 601 | } 602 | public override JSONNode this[string aKey] 603 | { 604 | get{ return new JSONLazyCreator(this);} 605 | set{ m_List.Add(value); } 606 | } 607 | public override int Count 608 | { 609 | get { return m_List.Count; } 610 | } 611 | public override void Add(string aKey, JSONNode aItem) 612 | { 613 | m_List.Add(aItem); 614 | } 615 | public override JSONNode Remove(int aIndex) 616 | { 617 | if (aIndex < 0 || aIndex >= m_List.Count) 618 | return null; 619 | JSONNode tmp = m_List[aIndex]; 620 | m_List.RemoveAt(aIndex); 621 | return tmp; 622 | } 623 | public override JSONNode Remove(JSONNode aNode) 624 | { 625 | m_List.Remove(aNode); 626 | return aNode; 627 | } 628 | public override IEnumerable Childs 629 | { 630 | get 631 | { 632 | foreach(JSONNode N in m_List) 633 | yield return N; 634 | } 635 | } 636 | public IEnumerator GetEnumerator() 637 | { 638 | foreach(JSONNode N in m_List) 639 | yield return N; 640 | } 641 | public override string ToString() 642 | { 643 | string result = "[ "; 644 | foreach (JSONNode N in m_List) 645 | { 646 | if (result.Length > 2) 647 | result += ", "; 648 | result += N.ToString(); 649 | } 650 | result += " ]"; 651 | return result; 652 | } 653 | public override string ToString(string aPrefix) 654 | { 655 | string result = "[ "; 656 | foreach (JSONNode N in m_List) 657 | { 658 | if (result.Length > 3) 659 | result += ", "; 660 | result += "\n" + aPrefix + " "; 661 | result += N.ToString(aPrefix+" "); 662 | } 663 | result += "\n" + aPrefix + "]"; 664 | return result; 665 | } 666 | public override void Serialize (System.IO.BinaryWriter aWriter) 667 | { 668 | aWriter.Write((byte)JSONBinaryTag.Array); 669 | aWriter.Write(m_List.Count); 670 | for(int i = 0; i < m_List.Count; i++) 671 | { 672 | m_List[i].Serialize(aWriter); 673 | } 674 | } 675 | } // End of JSONArray 676 | 677 | public class JSONClass : JSONNode, IEnumerable 678 | { 679 | private Dictionary m_Dict = new Dictionary(); 680 | public override JSONNode this[string aKey] 681 | { 682 | get 683 | { 684 | if (m_Dict.ContainsKey(aKey)) 685 | return m_Dict[aKey]; 686 | else 687 | return new JSONLazyCreator(this, aKey); 688 | } 689 | set 690 | { 691 | if (m_Dict.ContainsKey(aKey)) 692 | m_Dict[aKey] = value; 693 | else 694 | m_Dict.Add(aKey,value); 695 | } 696 | } 697 | public override JSONNode this[int aIndex] 698 | { 699 | get 700 | { 701 | if (aIndex < 0 || aIndex >= m_Dict.Count) 702 | return null; 703 | return m_Dict.ElementAt(aIndex).Value; 704 | } 705 | set 706 | { 707 | if (aIndex < 0 || aIndex >= m_Dict.Count) 708 | return; 709 | string key = m_Dict.ElementAt(aIndex).Key; 710 | m_Dict[key] = value; 711 | } 712 | } 713 | public override int Count 714 | { 715 | get { return m_Dict.Count; } 716 | } 717 | 718 | 719 | public override void Add(string aKey, JSONNode aItem) 720 | { 721 | if (!string.IsNullOrEmpty(aKey)) 722 | { 723 | if (m_Dict.ContainsKey(aKey)) 724 | m_Dict[aKey] = aItem; 725 | else 726 | m_Dict.Add(aKey, aItem); 727 | } 728 | else 729 | m_Dict.Add(Guid.NewGuid().ToString(), aItem); 730 | } 731 | 732 | public override JSONNode Remove(string aKey) 733 | { 734 | if (!m_Dict.ContainsKey(aKey)) 735 | return null; 736 | JSONNode tmp = m_Dict[aKey]; 737 | m_Dict.Remove(aKey); 738 | return tmp; 739 | } 740 | public override JSONNode Remove(int aIndex) 741 | { 742 | if (aIndex < 0 || aIndex >= m_Dict.Count) 743 | return null; 744 | var item = m_Dict.ElementAt(aIndex); 745 | m_Dict.Remove(item.Key); 746 | return item.Value; 747 | } 748 | public override JSONNode Remove(JSONNode aNode) 749 | { 750 | try 751 | { 752 | var item = m_Dict.Where(k => k.Value == aNode).First(); 753 | m_Dict.Remove(item.Key); 754 | return aNode; 755 | } 756 | catch 757 | { 758 | return null; 759 | } 760 | } 761 | 762 | public override IEnumerable Childs 763 | { 764 | get 765 | { 766 | foreach(KeyValuePair N in m_Dict) 767 | yield return N.Value; 768 | } 769 | } 770 | 771 | public IEnumerator GetEnumerator() 772 | { 773 | foreach(KeyValuePair N in m_Dict) 774 | yield return N; 775 | } 776 | public override string ToString() 777 | { 778 | string result = "{"; 779 | foreach (KeyValuePair N in m_Dict) 780 | { 781 | if (result.Length > 2) 782 | result += ", "; 783 | result += "\"" + Escape(N.Key) + "\":" + N.Value.ToString(); 784 | } 785 | result += "}"; 786 | return result; 787 | } 788 | public override string ToString(string aPrefix) 789 | { 790 | string result = "{ "; 791 | foreach (KeyValuePair N in m_Dict) 792 | { 793 | if (result.Length > 3) 794 | result += ", "; 795 | result += "\n" + aPrefix + " "; 796 | result += "\"" + Escape(N.Key) + "\" : " + N.Value.ToString(aPrefix+" "); 797 | } 798 | result += "\n" + aPrefix + "}"; 799 | return result; 800 | } 801 | public override void Serialize (System.IO.BinaryWriter aWriter) 802 | { 803 | aWriter.Write((byte)JSONBinaryTag.Class); 804 | aWriter.Write(m_Dict.Count); 805 | foreach(string K in m_Dict.Keys) 806 | { 807 | aWriter.Write(K); 808 | m_Dict[K].Serialize(aWriter); 809 | } 810 | } 811 | } // End of JSONClass 812 | 813 | public class JSONData : JSONNode 814 | { 815 | private string m_Data; 816 | public override string Value 817 | { 818 | get { return m_Data; } 819 | set { m_Data = value; } 820 | } 821 | public JSONData(string aData) 822 | { 823 | m_Data = aData; 824 | } 825 | public JSONData(float aData) 826 | { 827 | AsFloat = aData; 828 | } 829 | public JSONData(double aData) 830 | { 831 | AsDouble = aData; 832 | } 833 | public JSONData(bool aData) 834 | { 835 | AsBool = aData; 836 | } 837 | public JSONData(int aData) 838 | { 839 | AsInt = aData; 840 | } 841 | 842 | public override string ToString() 843 | { 844 | return "\"" + Escape(m_Data) + "\""; 845 | } 846 | public override string ToString(string aPrefix) 847 | { 848 | return "\"" + Escape(m_Data) + "\""; 849 | } 850 | public override void Serialize (System.IO.BinaryWriter aWriter) 851 | { 852 | var tmp = new JSONData(""); 853 | 854 | tmp.AsInt = AsInt; 855 | if (tmp.m_Data == this.m_Data) 856 | { 857 | aWriter.Write((byte)JSONBinaryTag.IntValue); 858 | aWriter.Write(AsInt); 859 | return; 860 | } 861 | tmp.AsFloat = AsFloat; 862 | if (tmp.m_Data == this.m_Data) 863 | { 864 | aWriter.Write((byte)JSONBinaryTag.FloatValue); 865 | aWriter.Write(AsFloat); 866 | return; 867 | } 868 | tmp.AsDouble = AsDouble; 869 | if (tmp.m_Data == this.m_Data) 870 | { 871 | aWriter.Write((byte)JSONBinaryTag.DoubleValue); 872 | aWriter.Write(AsDouble); 873 | return; 874 | } 875 | 876 | tmp.AsBool = AsBool; 877 | if (tmp.m_Data == this.m_Data) 878 | { 879 | aWriter.Write((byte)JSONBinaryTag.BoolValue); 880 | aWriter.Write(AsBool); 881 | return; 882 | } 883 | aWriter.Write((byte)JSONBinaryTag.Value); 884 | aWriter.Write(m_Data); 885 | } 886 | } // End of JSONData 887 | 888 | internal class JSONLazyCreator : JSONNode 889 | { 890 | private JSONNode m_Node = null; 891 | private string m_Key = null; 892 | 893 | public JSONLazyCreator(JSONNode aNode) 894 | { 895 | m_Node = aNode; 896 | m_Key = null; 897 | } 898 | public JSONLazyCreator(JSONNode aNode, string aKey) 899 | { 900 | m_Node = aNode; 901 | m_Key = aKey; 902 | } 903 | 904 | private void Set(JSONNode aVal) 905 | { 906 | if (m_Key == null) 907 | { 908 | m_Node.Add(aVal); 909 | } 910 | else 911 | { 912 | m_Node.Add(m_Key, aVal); 913 | } 914 | m_Node = null; // Be GC friendly. 915 | } 916 | 917 | public override JSONNode this[int aIndex] 918 | { 919 | get 920 | { 921 | return new JSONLazyCreator(this); 922 | } 923 | set 924 | { 925 | var tmp = new JSONArray(); 926 | tmp.Add(value); 927 | Set(tmp); 928 | } 929 | } 930 | 931 | public override JSONNode this[string aKey] 932 | { 933 | get 934 | { 935 | return new JSONLazyCreator(this, aKey); 936 | } 937 | set 938 | { 939 | var tmp = new JSONClass(); 940 | tmp.Add(aKey, value); 941 | Set(tmp); 942 | } 943 | } 944 | public override void Add (JSONNode aItem) 945 | { 946 | var tmp = new JSONArray(); 947 | tmp.Add(aItem); 948 | Set(tmp); 949 | } 950 | public override void Add (string aKey, JSONNode aItem) 951 | { 952 | var tmp = new JSONClass(); 953 | tmp.Add(aKey, aItem); 954 | Set(tmp); 955 | } 956 | public static bool operator ==(JSONLazyCreator a, object b) 957 | { 958 | if (b == null) 959 | return true; 960 | return System.Object.ReferenceEquals(a,b); 961 | } 962 | 963 | public static bool operator !=(JSONLazyCreator a, object b) 964 | { 965 | return !(a == b); 966 | } 967 | public override bool Equals (object obj) 968 | { 969 | if (obj == null) 970 | return true; 971 | return System.Object.ReferenceEquals(this, obj); 972 | } 973 | public override int GetHashCode () 974 | { 975 | return base.GetHashCode(); 976 | } 977 | 978 | public override string ToString() 979 | { 980 | return ""; 981 | } 982 | public override string ToString(string aPrefix) 983 | { 984 | return ""; 985 | } 986 | 987 | public override int AsInt 988 | { 989 | get 990 | { 991 | JSONData tmp = new JSONData(0); 992 | Set(tmp); 993 | return 0; 994 | } 995 | set 996 | { 997 | JSONData tmp = new JSONData(value); 998 | Set(tmp); 999 | } 1000 | } 1001 | public override float AsFloat 1002 | { 1003 | get 1004 | { 1005 | JSONData tmp = new JSONData(0.0f); 1006 | Set(tmp); 1007 | return 0.0f; 1008 | } 1009 | set 1010 | { 1011 | JSONData tmp = new JSONData(value); 1012 | Set(tmp); 1013 | } 1014 | } 1015 | public override double AsDouble 1016 | { 1017 | get 1018 | { 1019 | JSONData tmp = new JSONData(0.0); 1020 | Set(tmp); 1021 | return 0.0; 1022 | } 1023 | set 1024 | { 1025 | JSONData tmp = new JSONData(value); 1026 | Set(tmp); 1027 | } 1028 | } 1029 | public override bool AsBool 1030 | { 1031 | get 1032 | { 1033 | JSONData tmp = new JSONData(false); 1034 | Set(tmp); 1035 | return false; 1036 | } 1037 | set 1038 | { 1039 | JSONData tmp = new JSONData(value); 1040 | Set(tmp); 1041 | } 1042 | } 1043 | public override JSONArray AsArray 1044 | { 1045 | get 1046 | { 1047 | JSONArray tmp = new JSONArray(); 1048 | Set(tmp); 1049 | return tmp; 1050 | } 1051 | } 1052 | public override JSONClass AsObject 1053 | { 1054 | get 1055 | { 1056 | JSONClass tmp = new JSONClass(); 1057 | Set(tmp); 1058 | return tmp; 1059 | } 1060 | } 1061 | } // End of JSONLazyCreator 1062 | 1063 | public static class JSON 1064 | { 1065 | public static JSONNode Parse(string aJSON) 1066 | { 1067 | return JSONNode.Parse(aJSON); 1068 | } 1069 | } 1070 | } -------------------------------------------------------------------------------- /src/Table/TableMgr.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System; 3 | using System.Reflection; 4 | using System.Collections; 5 | using System.Collections.Generic; 6 | using Game.Table; 7 | 8 | 9 | // TableManager.cs 10 | // Author: Lu Zexi 11 | // 2014-07-23 12 | 13 | 14 | 15 | /// 16 | /// Table管理类 17 | /// 18 | public class TableMgr 19 | { 20 | private Dictionary> m_mapData = new Dictionary>(); 21 | 22 | private static TableMgr s_cInstance; 23 | public static TableMgr sInstance 24 | { 25 | get 26 | { 27 | if (s_cInstance == null) 28 | { 29 | s_cInstance = new TableMgr(); 30 | } 31 | return s_cInstance; 32 | } 33 | } 34 | 35 | public TableMgr() 36 | { 37 | // 38 | } 39 | 40 | /// 41 | /// get the Table. 42 | /// 43 | /// 44 | /// 45 | public T GetTable() where T : CTable , new() 46 | { 47 | T t = new T(); 48 | if( t.Count > 0 ) 49 | { 50 | t = t.Get(0); 51 | return t; 52 | } 53 | return default(T); 54 | } 55 | 56 | /// 57 | /// get the map data 58 | /// 59 | /// 60 | /// 61 | internal List Get(string name) 62 | { 63 | if (!this.m_mapData.ContainsKey(name)) 64 | return null; 65 | return this.m_mapData[name]; 66 | } 67 | 68 | /// 69 | /// add the data. 70 | /// 71 | /// 72 | /// 73 | internal void Add(string name, List lst) 74 | { 75 | this.m_mapData.Add(name, lst); 76 | } 77 | 78 | /// 79 | /// remove table 80 | /// 81 | /// 82 | internal void Remove(string name) 83 | { 84 | if (!this.m_mapData.ContainsKey(name)) 85 | return; 86 | this.m_mapData.Remove(name); 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /src/View/CView.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | using System.Collections; 4 | using System.Collections.Generic; 5 | using UnityEngine; 6 | 7 | 8 | // ViewBase.cs 9 | // Author: Lu Zexi 10 | // 2014-07-05 11 | 12 | 13 | 14 | /// 15 | /// The View Base 16 | /// 17 | public class CView : MonoBehaviour 18 | { 19 | public Dictionary m_mapRes = new Dictionary(); 20 | 21 | /// 22 | /// Init this instance. 23 | /// 24 | public virtual void Init() 25 | { 26 | Type t = GetType(); 27 | FieldInfo[] fis = t.GetFields(BindingFlags.Public | BindingFlags.Instance); 28 | foreach (FieldInfo f in fis) 29 | { 30 | Type fieldType = f.FieldType; 31 | if( fieldType == typeof(List) ) 32 | { 33 | List lst = new List(); 34 | for( int i = 1 ;;i++) 35 | { 36 | Transform tra = FIND_CHILD(this.transform , f.Name+i); 37 | if(tra== null) 38 | { 39 | f.SetValue(this,lst); 40 | break; 41 | } 42 | lst.Add(tra.gameObject); 43 | } 44 | } 45 | else if( fieldType == typeof(GameObject) || fieldType.IsSubclassOf(typeof(MonoBehaviour))) 46 | { 47 | Transform tra = FIND_CHILD(this.transform , f.Name); 48 | if(tra != null) 49 | { 50 | if( fieldType == typeof(GameObject) ) 51 | { 52 | var com = tra.gameObject; 53 | f.SetValue(this,com); 54 | } 55 | else 56 | { 57 | var com = tra.GetComponent(fieldType); 58 | f.SetValue(this,com); 59 | } 60 | } 61 | } 62 | } 63 | } 64 | 65 | 66 | protected Transform FIND_CHILD( Transform parent , string childName ) 67 | { 68 | foreach( Transform item in parent ) 69 | { 70 | if(item.name == childName) 71 | { 72 | return item; 73 | } 74 | else 75 | { 76 | Transform res = FIND_CHILD(item , childName); 77 | if(res != null ) 78 | return res; 79 | } 80 | } 81 | return null; 82 | } 83 | } 84 | --------------------------------------------------------------------------------