using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Linq; using System; using System.Reflection; public class UIDataManager { private static UIDataManager _instance; public static UIDataManager Instance { get { if (_instance == null) { _instance = new UIDataManager(); } return _instance; } } private UIAllData data; public UIDataManager() { data = new UIAllData(); } public void Init() { MessageCenterController.Instance.Register(GameEnum.MESSAGE_BACK_UI_CHANGE_DATA, SocketDataBack); } public void ClearAll() { data.ClearAll(); } public void Reset() { MessageCenterController.Instance.UnRegister(GameEnum.MESSAGE_BACK_UI_CHANGE_DATA, SocketDataBack); } public string GetDataStr() { return "-- " + data.ToString()+ " --"; } private List dataList = new List(); private List dataList1 = new List(); private void SocketDataBack(System.Object datas) { Engine.Net.NetMsg iMsg = datas as Engine.Net.NetMsg; SocketUIChangeData data = new SocketUIChangeData(); iMsg.UnPackMsg(ref data); dataList.Clear(); for (int i = 0; i < data.ct; i++) { SimpleUIdata s_data = new SimpleUIdata(); iMsg.UnPackMsg(ref s_data); dataList.Add(s_data); } dataList1.Clear(); for (int i = 0; i < data.ct; i++) { string className = dataList[i].DataClassName;//这个是对应的自定义数据的类名 Type type = Type.GetType(className); var s_data = Activator.CreateInstance(type); iMsg.UnPackMsg(ref s_data); dataList1.Add(s_data); } RefreshTongbu(dataList.ToArray(), dataList1.ToArray(), (UIDataMode)data.changeType); } //打开UI public void RefreshUI(SimpleUIdata uIdata, System.Object uiCanshu, UIDataMode mode) { if (!GamePlayerData.Instance.IsFangzhu()) { return; } var res = true; switch (mode) { case UIDataMode.Default: break; case UIDataMode.Add: res = data.AddUI(uIdata, uiCanshu); break; case UIDataMode.Del: res = data.DelUI(uIdata); break; case UIDataMode.Refresh: //data.DelUI(uIdata); break; } if(res) Broadcast(new SimpleUIdata[1] { uIdata }, new System.Object[1] { uiCanshu }, mode); } //这里是学生的同步 同步有3种 一种是覆盖 一种是增加 一种是删除 public void RefreshTongbu(SimpleUIdata[] uIdata, System.Object[] datas, UIDataMode mode) { if(GamePlayerData.Instance.IsFangzhu()) { return; } //CDebug.Log(mode.ToString()); switch(mode) { case UIDataMode.Default: data.Reassign(uIdata, datas); break; case UIDataMode.Add: data.AddUI(uIdata[0], datas[0]); break; case UIDataMode.Del: data.DelUI(uIdata[0]); break; case UIDataMode.Refresh: data.UpdateUI(uIdata[0], datas[0]); break; } } private void Broadcast(SimpleUIdata[] datas, System.Object[] canshus, UIDataMode changeType) { SocketUIChangeData socket_data = new SocketUIChangeData(); socket_data.changeType = (int)changeType; socket_data.ct = (short)datas.Length; socket_data.datas = datas; socket_data.canshus = canshus; MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_SEND_UI_CHANGE_DATA, socket_data); } public SocketUIChangeData GetCurAllUIDataToStudent() { SocketUIChangeData socket_data = new SocketUIChangeData(); socket_data.changeType = (int)UIDataMode.Default; socket_data.datas = this.data.GetUIData(); socket_data.canshus = this.data.GetCanshuData(); socket_data.ct = (short)socket_data.datas.Length; return socket_data; } public System.Object GetDataByKeyId(string key) { return data.GetCanshuDataByKey(key); } } public class UIAllData { //完全开放式的 private Dictionary uiList = new Dictionary(); //UI的位置信息 //private Dictionary uiPointDict = new Dictionary(); //UI的参数信息 private Dictionary uiDataDict = new Dictionary(); public override string ToString() { var str = ""; foreach(string key in uiList.Keys) { str += string.Format( "<{0}-{1}>" ,(UIType) uiList[key].UITypeId , uiList[key].UIDataType); } return str; } public void ClearAll() { uiList.Clear(); uiDataDict.Clear(); } public SimpleUIdata[] GetUIData() { return uiList.Values.ToArray(); } public System.Object[] GetCanshuData() { return uiDataDict.Values.ToArray(); } public System.Object GetCanshuDataByKey(string key) { System.Object res; if(uiDataDict.TryGetValue(key, out res)) { return res; } CDebug.Log("这里有严重的错误" + key); return null; } public void Reassign(SimpleUIdata[] data, System.Object[] datas) { uiList.Clear(); //CDebug.Log("当前同步到的UI窗口数量是" + data.Length); for(int i = 0; i< data.Length; i++) { AddUI(data[i], datas[i]); } } public bool AddUI(SimpleUIdata data, System.Object canshu) { if (!uiList.ContainsKey(data.GetUIKey())) { uiList.Add(data.GetUIKey(), data); uiDataDict.Add(data.GetUIKey(), canshu); if (!GamePlayerData.Instance.IsFangzhu()) AddBack(data); return true; } return false; } public bool UpdateUI(SimpleUIdata data, System.Object canshu) { if (uiList.ContainsKey(data.GetUIKey())) { uiDataDict[data.GetUIKey()] =canshu; if (!GamePlayerData.Instance.IsFangzhu()) MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_TONGBU_UI_CHANGE, data); return true; } return false; } public bool DelUI(SimpleUIdata data) { if (uiList.ContainsKey(data.GetUIKey())) { uiDataDict.Remove(data.GetUIKey()); if (!GamePlayerData.Instance.IsFangzhu()) DelBack(data); return uiList.Remove(data.GetUIKey()); } CDebug.Log("没找到要删除的" + data.GetUIKey()); return false; } private void AddBack(SimpleUIdata data) { switch ((UIType)data.UITypeId) { case UIType.Default: break; case UIType.Pannel: MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_SHOW_PANNEL_BY_TYPE, data.UIDataType); break; case UIType.Pop: MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_SHOW_POP_BY_TYPE, new ShowPopData((PopType)data.UIDataType, uiDataDict[data.GetUIKey()])); break; case UIType.Bar: MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_CHAPTER_PLAY_BTN_CLICK, uiDataDict[data.GetUIKey()]); break; } } private void DelBack(SimpleUIdata data) { switch ((UIType)data.UITypeId) { case UIType.Default: break; case UIType.Pannel: MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_HIDE_PANNEL_BY_TYPE, data.UIDataType); break; case UIType.Pop: MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_HIDE_POP_BY_TYPE, data.UIDataType); break; case UIType.Bar: MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_HIDE_BAR_BY_TYPE, data.UIDataType); break; } } } public class SimpleUIdata { public int UITypeId;// 0表示普通UI 1表示pannel 2 pop 3tip public int UIDataType;// UI内容相关的枚举类型 public Vector3 Pos;//位置 public Vector3 Eurl;//朝向 public string DataClassName;//UI窗口的参数自定义类名 public string GetUIKey() { return UITypeId + "_" + UIDataType; } } //基础的UI窗口 public class NormalUIData { public string str = ""; public int value; } public class SocketUIChangeData { public int changeType;//UIDataMode 枚举对应 public short ct;//数据的数量 public SimpleUIdata[] datas; public System.Object[] canshus; } public class ShowPopData { public PopType t;//弹框类型 public System.Object data;//pop参数 public ShowPopData(PopType t, System.Object d) { this.t = t; this.data = d; } } public enum UIType { Default,//默认的普通UI Pannel,// Pop, Bar,//侧边栏 Tip, } public enum UIDataMode { Default,//覆盖 Add,//增加 Del,//删除 Refresh,//刷新数据 }