UIDataManager.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.Linq;
  5. using System;
  6. using System.Reflection;
  7. public class UIDataManager
  8. {
  9. private static UIDataManager _instance;
  10. public static UIDataManager Instance
  11. {
  12. get
  13. {
  14. if (_instance == null)
  15. {
  16. _instance = new UIDataManager();
  17. }
  18. return _instance;
  19. }
  20. }
  21. private UIAllData data;
  22. public UIDataManager()
  23. {
  24. data = new UIAllData();
  25. }
  26. public void Init()
  27. {
  28. MessageCenterController.Instance.Register(GameEnum.MESSAGE_BACK_UI_CHANGE_DATA, SocketDataBack);
  29. }
  30. public void ClearAll()
  31. {
  32. data.ClearAll();
  33. }
  34. public void Reset()
  35. {
  36. MessageCenterController.Instance.UnRegister(GameEnum.MESSAGE_BACK_UI_CHANGE_DATA, SocketDataBack);
  37. }
  38. public string GetDataStr()
  39. {
  40. return "-- " + data.ToString()+ " --";
  41. }
  42. private List<SimpleUIdata> dataList = new List<SimpleUIdata>();
  43. private List<System.Object> dataList1 = new List<System.Object>();
  44. private void SocketDataBack(System.Object datas)
  45. {
  46. Engine.Net.NetMsg iMsg = datas as Engine.Net.NetMsg;
  47. SocketUIChangeData data = new SocketUIChangeData();
  48. iMsg.UnPackMsg(ref data);
  49. dataList.Clear();
  50. for (int i = 0; i < data.ct; i++)
  51. {
  52. SimpleUIdata s_data = new SimpleUIdata();
  53. iMsg.UnPackMsg(ref s_data);
  54. dataList.Add(s_data);
  55. }
  56. dataList1.Clear();
  57. for (int i = 0; i < data.ct; i++)
  58. {
  59. string className = dataList[i].DataClassName;//这个是对应的自定义数据的类名
  60. Type type = Type.GetType(className);
  61. var s_data = Activator.CreateInstance(type);
  62. iMsg.UnPackMsg(ref s_data);
  63. dataList1.Add(s_data);
  64. }
  65. RefreshTongbu(dataList.ToArray(), dataList1.ToArray(), (UIDataMode)data.changeType);
  66. }
  67. //打开UI
  68. public void RefreshUI(SimpleUIdata uIdata, System.Object uiCanshu, UIDataMode mode)
  69. {
  70. if (!GamePlayerData.Instance.IsFangzhu())
  71. {
  72. return;
  73. }
  74. var res = true;
  75. switch (mode)
  76. {
  77. case UIDataMode.Default:
  78. break;
  79. case UIDataMode.Add:
  80. res = data.AddUI(uIdata, uiCanshu);
  81. break;
  82. case UIDataMode.Del:
  83. res = data.DelUI(uIdata);
  84. break;
  85. case UIDataMode.Refresh:
  86. //data.DelUI(uIdata);
  87. break;
  88. }
  89. if(res)
  90. Broadcast(new SimpleUIdata[1] { uIdata }, new System.Object[1] { uiCanshu }, mode);
  91. }
  92. //这里是学生的同步 同步有3种 一种是覆盖 一种是增加 一种是删除
  93. public void RefreshTongbu(SimpleUIdata[] uIdata, System.Object[] datas, UIDataMode mode)
  94. {
  95. if(GamePlayerData.Instance.IsFangzhu())
  96. {
  97. return;
  98. }
  99. //CDebug.Log(mode.ToString());
  100. switch(mode)
  101. {
  102. case UIDataMode.Default:
  103. data.Reassign(uIdata, datas);
  104. break;
  105. case UIDataMode.Add:
  106. data.AddUI(uIdata[0], datas[0]);
  107. break;
  108. case UIDataMode.Del:
  109. data.DelUI(uIdata[0]);
  110. break;
  111. case UIDataMode.Refresh:
  112. data.UpdateUI(uIdata[0], datas[0]);
  113. break;
  114. }
  115. }
  116. private void Broadcast(SimpleUIdata[] datas, System.Object[] canshus, UIDataMode changeType)
  117. {
  118. SocketUIChangeData socket_data = new SocketUIChangeData();
  119. socket_data.changeType = (int)changeType;
  120. socket_data.ct = (short)datas.Length;
  121. socket_data.datas = datas;
  122. socket_data.canshus = canshus;
  123. MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_SEND_UI_CHANGE_DATA, socket_data);
  124. }
  125. public SocketUIChangeData GetCurAllUIDataToStudent()
  126. {
  127. SocketUIChangeData socket_data = new SocketUIChangeData();
  128. socket_data.changeType = (int)UIDataMode.Default;
  129. socket_data.datas = this.data.GetUIData();
  130. socket_data.canshus = this.data.GetCanshuData();
  131. socket_data.ct = (short)socket_data.datas.Length;
  132. return socket_data;
  133. }
  134. public System.Object GetDataByKeyId(string key)
  135. {
  136. return data.GetCanshuDataByKey(key);
  137. }
  138. }
  139. public class UIAllData
  140. {
  141. //完全开放式的
  142. private Dictionary<string, SimpleUIdata> uiList = new Dictionary<string, SimpleUIdata>();
  143. //UI的位置信息
  144. //private Dictionary<string, SimpleUIdata> uiPointDict = new Dictionary<string, SimpleUIdata>();
  145. //UI的参数信息
  146. private Dictionary<string, System.Object> uiDataDict = new Dictionary<string, System.Object>();
  147. public override string ToString()
  148. {
  149. var str = "";
  150. foreach(string key in uiList.Keys)
  151. {
  152. str += string.Format( "<{0}-{1}>" ,(UIType) uiList[key].UITypeId , uiList[key].UIDataType);
  153. }
  154. return str;
  155. }
  156. public void ClearAll()
  157. {
  158. uiList.Clear();
  159. uiDataDict.Clear();
  160. }
  161. public SimpleUIdata[] GetUIData()
  162. {
  163. return uiList.Values.ToArray();
  164. }
  165. public System.Object[] GetCanshuData()
  166. {
  167. return uiDataDict.Values.ToArray();
  168. }
  169. public System.Object GetCanshuDataByKey(string key)
  170. {
  171. System.Object res;
  172. if(uiDataDict.TryGetValue(key, out res))
  173. {
  174. return res;
  175. }
  176. CDebug.Log("这里有严重的错误" + key);
  177. return null;
  178. }
  179. public void Reassign(SimpleUIdata[] data, System.Object[] datas)
  180. {
  181. uiList.Clear();
  182. //CDebug.Log("当前同步到的UI窗口数量是" + data.Length);
  183. for(int i = 0; i< data.Length; i++)
  184. {
  185. AddUI(data[i], datas[i]);
  186. }
  187. }
  188. public bool AddUI(SimpleUIdata data, System.Object canshu)
  189. {
  190. if (!uiList.ContainsKey(data.GetUIKey()))
  191. {
  192. uiList.Add(data.GetUIKey(), data);
  193. uiDataDict.Add(data.GetUIKey(), canshu);
  194. if (!GamePlayerData.Instance.IsFangzhu())
  195. AddBack(data);
  196. return true;
  197. }
  198. return false;
  199. }
  200. public bool UpdateUI(SimpleUIdata data, System.Object canshu)
  201. {
  202. if (uiList.ContainsKey(data.GetUIKey()))
  203. {
  204. uiDataDict[data.GetUIKey()] =canshu;
  205. if (!GamePlayerData.Instance.IsFangzhu())
  206. MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_TONGBU_UI_CHANGE, data);
  207. return true;
  208. }
  209. return false;
  210. }
  211. public bool DelUI(SimpleUIdata data)
  212. {
  213. if (uiList.ContainsKey(data.GetUIKey()))
  214. {
  215. uiDataDict.Remove(data.GetUIKey());
  216. if (!GamePlayerData.Instance.IsFangzhu())
  217. DelBack(data);
  218. return uiList.Remove(data.GetUIKey());
  219. }
  220. CDebug.Log("没找到要删除的" + data.GetUIKey());
  221. return false;
  222. }
  223. private void AddBack(SimpleUIdata data)
  224. {
  225. switch ((UIType)data.UITypeId)
  226. {
  227. case UIType.Default:
  228. break;
  229. case UIType.Pannel:
  230. MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_SHOW_PANNEL_BY_TYPE, data.UIDataType);
  231. break;
  232. case UIType.Pop:
  233. MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_SHOW_POP_BY_TYPE, new ShowPopData((PopType)data.UIDataType, uiDataDict[data.GetUIKey()]));
  234. break;
  235. case UIType.Bar:
  236. MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_CHAPTER_PLAY_BTN_CLICK, uiDataDict[data.GetUIKey()]);
  237. break;
  238. }
  239. }
  240. private void DelBack(SimpleUIdata data)
  241. {
  242. switch ((UIType)data.UITypeId)
  243. {
  244. case UIType.Default:
  245. break;
  246. case UIType.Pannel:
  247. MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_HIDE_PANNEL_BY_TYPE, data.UIDataType);
  248. break;
  249. case UIType.Pop:
  250. MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_HIDE_POP_BY_TYPE, data.UIDataType);
  251. break;
  252. case UIType.Bar:
  253. MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_HIDE_BAR_BY_TYPE, data.UIDataType);
  254. break;
  255. }
  256. }
  257. }
  258. public class SimpleUIdata
  259. {
  260. public int UITypeId;// 0表示普通UI 1表示pannel 2 pop 3tip
  261. public int UIDataType;// UI内容相关的枚举类型
  262. public Vector3 Pos;//位置
  263. public Vector3 Eurl;//朝向
  264. public string DataClassName;//UI窗口的参数自定义类名
  265. public string GetUIKey()
  266. {
  267. return UITypeId + "_" + UIDataType;
  268. }
  269. }
  270. //基础的UI窗口
  271. public class NormalUIData
  272. {
  273. public string str = "";
  274. public int value;
  275. }
  276. public class SocketUIChangeData
  277. {
  278. public int changeType;//UIDataMode 枚举对应
  279. public short ct;//数据的数量
  280. public SimpleUIdata[] datas;
  281. public System.Object[] canshus;
  282. }
  283. public class ShowPopData
  284. {
  285. public PopType t;//弹框类型
  286. public System.Object data;//pop参数
  287. public ShowPopData(PopType t, System.Object d)
  288. {
  289. this.t = t;
  290. this.data = d;
  291. }
  292. }
  293. public enum UIType
  294. {
  295. Default,//默认的普通UI
  296. Pannel,//
  297. Pop,
  298. Bar,//侧边栏
  299. Tip,
  300. }
  301. public enum UIDataMode
  302. {
  303. Default,//覆盖
  304. Add,//增加
  305. Del,//删除
  306. Refresh,//刷新数据
  307. }