using System; using System.Collections.Generic; using System.Linq; using System.Text; using UnityEngine; using UnityEngine.UI; using YLBattle; /// /// 面板管理器 /// public class PanelHelper : MonoSingleton { /// /// WindowsObjDict 列表 /// private Dictionary WindowsObjDict = new Dictionary(); /// /// 正在加载的界面 /// private List loadingUIs = new List(); /// /// 当前 最后打开的界面名称 /// private string nowPanelName; /// /// 显示面板 /// /// 面板名称 /// 成功的回调 /// 参数 /// 显示序列 /// 父级 /// 相机路径 public void ShowPanel(string __name, Action acCallBack = null, object param = null, int sortingLayerOrder = -1, Transform tran = null, string cameraPath = "") { //Debug.Log("ShowPanel :" + _name); // UI_DBControlWindow.Instance().ResetCurrentType(EDBControlWindowType.Others); nowPanelName = __name; if (WindowsObjDict.ContainsKey(__name)) { if (WindowsObjDict[__name] == null) { WindowsObjDict.Remove(__name); } else { SetSortingLayerOrder(WindowsObjDict[__name], sortingLayerOrder); // 设置参数 GameBehavior behavior = WindowsObjDict[__name].GetComponent(); if (behavior != null) { behavior.SetParameter(param); behavior.enterAc = acCallBack; } if (WindowsObjDict[__name].active) { if (behavior != null) { behavior.BaseRefresh(); } } else { WindowsObjDict[__name].SetActive(true); } if (acCallBack != null) { acCallBack(WindowsObjDict[__name]); } return; } } // 改界面正在加载中,中断 if (loadingUIs.Contains(__name)) { return; } else { loadingUIs.Add(__name); } ResourceHelper.Instance.LoadAssetBundle(__name, (AssetBundle ab) => { loadingUIs.Remove(__name); if (ab == null) { Debug.LogError("loaderror:" + __name); return; } GameObject abGo = ab.LoadAsset(__name); GameObject obj = (GameObject)Instantiate(abGo); obj.name = nowPanelName; obj.AddComponent(); obj.SetActive(false); SetFont(obj); // 设置字体 SetCamera(obj, cameraPath); // 设置摄像机 SetLabelContentByLanaguage(obj); // SetSortingLayerOrder(obj, sortingLayerOrder); if (obj != null) { if (tran != null) { obj.transform.SetParent(tran); obj.transform.localPosition = Vector3.zero; obj.transform.localScale = Vector3.one; } // 设置 localScale 为 1 ,不然在编辑器的全屏模式下,scroll view 的滚动条会自己移动一段距离 obj.transform.localScale = Vector3.one; // 绑定脚本 if (Application.platform == RuntimePlatform.Android) { AssemblyHelper.Instance.BindScript(__name, obj); } else { Type tp = Type.GetType(__name); obj.AddComponent(tp); } // 设置参数 GameBehavior behavior = obj.GetComponent(); if (behavior != null) { behavior.SetParameter(param); behavior.enterAc = acCallBack; } if (obj.active) { behavior.BaseRefresh(); } else { obj.SetActive(true); // 返回所有使用此界面的回调 if (acCallBack != null) { acCallBack(obj); } } WindowsObjDict.Add(__name, obj); } else { Debug.LogWarning("读取界面失败 " + __name); } }); } internal void ShowPanel(string functionPanel, object p1, NpcInfoVo npcVo, object p2) { throw new NotImplementedException(); } /// /// 显示面板 /// /// 面板名称 /// 成功的回调 /// 参数 /// 显示序列 /// 父级 /// 相机路径 public void LoadPanel(string __name, Transform tran = null, string cameraPath = "") { if (WindowsObjDict.ContainsKey(__name)) { if (WindowsObjDict[__name] == null) { WindowsObjDict.Remove(__name); } } ResourceHelper.Instance.LoadAssetBundle(__name, (AssetBundle ab) => { if (ab == null) { Debug.LogError("loaderror:" + __name); return; } GameObject obj = (GameObject)Instantiate(ab.LoadAsset(__name)); obj.AddComponent(); obj.SetActive(false); SetFont(obj); // 设置字体 SetCamera(obj, cameraPath); // 设置摄像机 SetLabelContentByLanaguage(obj); // if (obj != null) { if (tran != null) { obj.transform.SetParent(tran); obj.transform.localPosition = Vector3.zero; obj.transform.localScale = Vector3.one; } // 设置 localScale 为 1 ,不然在编辑器的全屏模式下,scroll view 的滚动条会自己移动一段距离 obj.transform.localScale = Vector3.one; // 绑定脚本 if (Application.platform == RuntimePlatform.Android) { AssemblyHelper.Instance.BindScript(__name, obj); } else { Type tp = Type.GetType(__name); obj.AddComponent(tp); } WindowsObjDict.Add(__name, obj); } else { Debug.LogWarning("读取界面失败 " + __name); } }); } public string GetNowPanelName() { return this.nowPanelName; } /// /// 关闭指定panel /// /// panel名称 public void ClosePanel(string _name, bool unload = false) { if (unload) { UnloadPanel(_name); return; } if (WindowsObjDict.ContainsKey(_name)) { if (WindowsObjDict[_name] != null) { WindowsObjDict[_name].SetActive(false); } } } /// /// 关闭所有panel /// public void CloseAllPanel(string excludeName = "") { List KeyList = new List(WindowsObjDict.Keys); for (int i = 0; i < KeyList.Count; i++) { if (excludeName != KeyList[i]) { ClosePanel(KeyList[i]); } } } /// /// 卸载界面资源 /// /// 界面名称 public void UnloadPanel(string _name) { ResourceHelper.Instance.UnloadAssetBundle(_name); if (WindowsObjDict.ContainsKey(_name)) { Destroy(WindowsObjDict[_name]); WindowsObjDict.Remove(_name); } } /// /// 卸载所有加载的界面资源 /// public void UnloadAllPanel() { List KeyList = new List(WindowsObjDict.Keys); for (int i = 0; i < KeyList.Count; i++) { UnloadPanel(KeyList[i]); } Resources.UnloadUnusedAssets(); } /// /// 设置 UI 的字体 /// /// UI对象 public void SetFont(GameObject uiObj) { if (null == uiObj) { Debug.LogError("PanelHelper 设置字体,UI 为空,中断"); return; } // 获取所有的 Text Text[] allText = uiObj.GetComponentsInChildren(true); // 设置字体 for (int i = 0; i < allText.Length; i++) { if (allText[i].font == null) { allText[i].font = FontManager.Instance().CurFont; } else if (allText[i].font.fontNames.Contains("Arial")) { allText[i].font = FontManager.Instance().CurFont; } else { Debug.Log(uiObj + "界面中" + allText[i].name + "为特殊字体,无需更换!"); } ////allText[i].fontStyle = FontStyle.Normal; } } /// /// 根据对应的语言,刷新UI上显示的文字 /// /// public void SetLabelContentByLanaguage(GameObject uiObj) { Text[] allLabels = uiObj.transform.GetComponentsInChildren(true); string prefix = uiObj.name.Replace("(Clone)", ""); foreach (Text text in allLabels) { string key = prefix + "_" + text.name; if (GameCfg.CheckIsExistKey(key)) { text.text = GameCfg.GetStr(key); } } } /// /// /// public void SetSortingLayerOrder(GameObject uiObj, int sortingLayerOrder = -1) { if (null == uiObj) { Debug.LogError("PanelHelper 设置canvas的order in Layer,UI 为空,中断"); return; } Canvas canvas = uiObj.GetComponent(); if (null == canvas) { Debug.LogError("PanelHelper 设置摄像机,获取画布组件失败,中断"); return; } if (sortingLayerOrder > 0) { canvas.sortingOrder = sortingLayerOrder; } } /// /// 设置 UI 的摄像机 /// /// UI对象 /// 指定相机位置 public void SetCamera(GameObject uiObj, string camPath) { if (null == uiObj) { Debug.LogWarning("PanelHelper 设置摄像机,UI 为空,中断"); return; } GameObject cameraObj = null; if (string.IsNullOrEmpty(camPath) == false) { cameraObj = GameObject.Find(camPath); } else { cameraObj = GameObject.Find("UICamera"); } if (null == cameraObj) { Debug.LogWarning("PanelHelper 设置摄像机,找不到摄像机,中断"); return; } Canvas canvas = uiObj.GetComponent(); if (null == canvas) { Debug.LogWarning("PanelHelper 设置摄像机,获取画布组件失败,中断"); return; } canvas.worldCamera = cameraObj.GetComponent(); } /// /// 获取某个界面的显示状态 /// /// /// public bool GetPanelShowState(string _name) { if (WindowsObjDict.ContainsKey(_name)) { return WindowsObjDict[_name].activeSelf; } return false; } #region Guide /// /// 是否存在某个物体 /// /// /// /// /// public GameObject CheckOjbIsExist(string ui, string path, string obj_name = "") { if (WindowsObjDict.ContainsKey(ui)) { if (null != WindowsObjDict[ui]) { Transform t = WindowsObjDict[ui].transform.Find(path); if (t != null) { if (obj_name.Length <= 0) { return t.gameObject; } Transform novArrow = t.Find(obj_name); if (novArrow != null) { return novArrow.gameObject; } } } } return null; } /// /// 卸载指定控件 /// /// /// /// public void UnInstallObjToUI(string ui, string path, string obj_name) { if (WindowsObjDict.ContainsKey(ui)) { if (null != WindowsObjDict[ui]) { Transform t = WindowsObjDict[ui].transform.Find(path); Transform novArrow = t.Find(obj_name); if (novArrow != null) { MonoBehaviour.DestroyObject(novArrow.gameObject); } } } } /// /// 给 UI 添加一个游戏物体 /// /// 游戏物体 /// UI 的名称 /// 路径 /// 偏移量 /// 回调,返回是否查找成功 public void AttachObjToUI(GameObject go, string ui, string path, Vector3 offset, Action ac_Find, string cameraPath = "", bool isAbsolute = false) { Transform t = null; if (WindowsObjDict.ContainsKey(ui)) { if (null != WindowsObjDict[ui]) { WindowsObjDict[ui].SetActive(true); t = WindowsObjDict[ui].transform.Find(path); } } else { GameObject Trg = GameObject.Find(ui + "/" + path); if (Trg != null) { t = Trg.transform; } } if (null != t) { if (go != null) { go.transform.SetParent(t, false); go.transform.localPosition = offset; } // 新手引导 节点 该节点为 绝对事件 (只该指向UI 受事件响应) if (isAbsolute) { UI_GuideMarkWindow.Instance().OpenCover(); } Canvas canvas = t.gameObject.GetComponent(); if (canvas == null) { canvas = t.gameObject.AddComponent(); } canvas.overrideSorting = true; canvas.sortingLayerID = 1; GraphicRaycaster GR = t.gameObject.GetComponent(); if (GR == null) { GR = t.gameObject.AddComponent(); } GR.enabled = true; t.gameObject.AddComponent(); canvas.sortingOrder = 100; // 查找成功 if (null != ac_Find) { ac_Find(t.gameObject); } return; } //else //{ // Debug.LogError("查找失败..." + path); // if (go != null) // { // go.transform.SetParent(WindowsObjDict[ui].transform, false); // } // // 查找失败 // if (null != ac_Find) // { // ac_Find(null); // } //} // 查找失败 if (null != ac_Find) { ac_Find(null); } #region old //else //{ // PanelHelper.Instance.ShowPanel(ui, null, null, false, true, (GameObject uiObj) => // { // Transform t = WindowsObjDict[ui].transform.Find(path); // if (null != t) // { // go.transform.SetParent(t, false); // go.transform.localPosition = Vector3.zero; // go.transform.localPosition += offset; // // 新手引导 节点 该节点为 绝对事件 (只该指向UI 受事件响应) // if (isAbsolute) // { // UI_GuideMarkWindow.Instance().OpenCover(); // t.gameObject.AddComponent(); // t.gameObject.AddComponent(); // t.gameObject.AddComponent(); // } // // 查找成功 // if (null != ac_Find) // { // ac_Find(t.gameObject); // } // } // else // { // // 查找失败 // if (null != ac_Find) // { // ac_Find(null); // } // } // }, cameraPath); //} #endregion } /// /// /// /// /// public void UnloadObjToUI(GameObject arraw) { //Debug.LogError("UnloadObjToUI xx"); Transform prtTran = arraw.transform.parent; if (prtTran == null) { return; } //回到原来的层级 Canvas canvas = prtTran.GetComponent(); if (null != canvas) { canvas.sortingLayerName = "Default"; canvas.overrideSorting = false; } Renderer[] renders = prtTran.GetComponentsInChildren(true); for (int i = 0; i < renders.Length; i++) { renders[i].sortingLayerName = "Default"; } UI_GuideDelayCanvas delay = prtTran.GetComponent(); if (null != delay) { delay.enabled = false; } MonoBehaviour.DestroyObject(arraw); } /// /// 高亮指定控件 /// /// /// public void OnLightGetComponent(string ui, string path, Action ac, Vector3 offset, int angle, GameObject markOjb = null) { Transform t = null; if (WindowsObjDict.ContainsKey(ui)) { if (null != WindowsObjDict[ui]) { WindowsObjDict[ui].SetActive(true); t = WindowsObjDict[ui].transform.Find(path); } } else { GameObject Trg = GameObject.Find(ui + "/" + path); if (Trg != null) { t = Trg.transform; } } if (null != t) { if (markOjb != null) { markOjb.transform.SetParent(t, false); markOjb.transform.localPosition = offset; markOjb.transform.localRotation = new Quaternion(0, 0, angle, 0); } Canvas canvas = t.gameObject.GetComponent(); if (canvas == null) { canvas = t.gameObject.AddComponent(); } canvas.sortingOrder = 100; GraphicRaycaster GR = t.gameObject.GetComponent(); if (GR == null) { t.gameObject.AddComponent(); } t.gameObject.AddComponent(); if (ac != null) { ac(true); } return; } if (ac != null) { ac(false); } } /// /// 恢复(变暗)指定控件 /// /// /// public void OnDarkGetComponent(string ui, string path, Action ac, GameObject markOjb = null) { if (markOjb != null) { MonoBehaviour.DestroyObject(markOjb); } if (WindowsObjDict.ContainsKey(ui)) { if (null != WindowsObjDict[ui]) { Transform t = WindowsObjDict[ui].transform.Find(path); if (t != null) { //回到原来的层级 Canvas canvas = (Canvas)BattleStaticFunc.GetParentScript(t, "Canvas", true); if (null != canvas) { canvas.sortingLayerName = "Default"; canvas.overrideSorting = false; } Renderer[] renders = gameObject.GetComponentsInChildren(true); for (int i = 0; i < renders.Length; i++) { renders[i].sortingLayerName = "Default"; } UI_GuideDelayCanvas delay = gameObject.GetComponent(); if (null != delay) { delay.enabled = false; } if (ac != null) { ac(true); return; } } } } if (ac != null) { ac(false); } } public GameObject GetPanelByName(string panelName) { if (WindowsObjDict.ContainsKey(panelName)) if (WindowsObjDict.ContainsKey(panelName)) { return WindowsObjDict[panelName]; } return null; } #endregion }