123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using UnityEngine;
- using UnityEngine.UI;
- using YLBattle;
- /// <summary>
- /// 面板管理器
- /// </summary>
- public class PanelHelper : MonoSingleton<PanelHelper>
- {
- /// <summary>
- /// WindowsObjDict 列表
- /// </summary>
- private Dictionary<string, GameObject> WindowsObjDict = new Dictionary<string, GameObject>();
- /// <summary>
- /// 正在加载的界面
- /// </summary>
- private List<string> loadingUIs = new List<string>();
- /// <summary>
- /// 当前 最后打开的界面名称
- /// </summary>
- private string nowPanelName;
- /// <summary>
- /// 显示面板
- /// </summary>
- /// <param name="__name">面板名称</param>
- /// <param name="acCallBack">成功的回调</param>
- /// <param name="param">参数</param>
- /// <param name="sortingLayerOrder">显示序列</param>
- /// <param name="tran">父级</param>
- /// <param name="cameraPath">相机路径</param>
- public void ShowPanel(string __name, Action<GameObject> 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<GameBehavior>();
- 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<GameObject>(__name);
- GameObject obj = (GameObject)Instantiate(abGo);
- obj.name = nowPanelName;
- obj.AddComponent<FixUIShader>();
- 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<GameBehavior>();
- 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();
- }
- /// <summary>
- /// 显示面板
- /// </summary>
- /// <param name="__name">面板名称</param>
- /// <param name="acCallBack">成功的回调</param>
- /// <param name="param">参数</param>
- /// <param name="sortingLayerOrder">显示序列</param>
- /// <param name="tran">父级</param>
- /// <param name="cameraPath">相机路径</param>
- 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<FixUIShader>();
- 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;
- }
- /// <summary>
- /// 关闭指定panel
- /// </summary>
- /// <param name="_name">panel名称</param>
- 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);
- }
- }
- }
- /// <summary>
- /// 关闭所有panel
- /// </summary>
- public void CloseAllPanel(string excludeName = "")
- {
- List<string> KeyList = new List<string>(WindowsObjDict.Keys);
- for (int i = 0; i < KeyList.Count; i++)
- {
- if (excludeName != KeyList[i])
- {
- ClosePanel(KeyList[i]);
- }
- }
- }
- /// <summary>
- /// 卸载界面资源
- /// </summary>
- /// <param name="_name">界面名称</param>
- public void UnloadPanel(string _name)
- {
- ResourceHelper.Instance.UnloadAssetBundle(_name);
- if (WindowsObjDict.ContainsKey(_name))
- {
- Destroy(WindowsObjDict[_name]);
- WindowsObjDict.Remove(_name);
- }
- }
- /// <summary>
- /// 卸载所有加载的界面资源
- /// </summary>
- public void UnloadAllPanel()
- {
- List<string> KeyList = new List<string>(WindowsObjDict.Keys);
- for (int i = 0; i < KeyList.Count; i++)
- {
- UnloadPanel(KeyList[i]);
- }
- Resources.UnloadUnusedAssets();
- }
- /// <summary>
- /// 设置 UI 的字体
- /// </summary>
- /// <param name="uiObj"> UI对象 </param>
- public void SetFont(GameObject uiObj)
- {
- if (null == uiObj)
- {
- Debug.LogError("PanelHelper 设置字体,UI 为空,中断");
- return;
- }
- // 获取所有的 Text
- Text[] allText = uiObj.GetComponentsInChildren<Text>(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;
- }
- }
- /// <summary>
- /// 根据对应的语言,刷新UI上显示的文字
- /// </summary>
- /// <param name="uiObj"></param>
- public void SetLabelContentByLanaguage(GameObject uiObj)
- {
- Text[] allLabels = uiObj.transform.GetComponentsInChildren<Text>(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);
- }
- }
- }
- /// <summary>
- ///
- /// </summary>
- public void SetSortingLayerOrder(GameObject uiObj, int sortingLayerOrder = -1)
- {
- if (null == uiObj)
- {
- Debug.LogError("PanelHelper 设置canvas的order in Layer,UI 为空,中断");
- return;
- }
- Canvas canvas = uiObj.GetComponent<Canvas>();
- if (null == canvas)
- {
- Debug.LogError("PanelHelper 设置摄像机,获取画布组件失败,中断");
- return;
- }
- if (sortingLayerOrder > 0)
- {
- canvas.sortingOrder = sortingLayerOrder;
- }
- }
- /// <summary>
- /// 设置 UI 的摄像机
- /// </summary>
- /// <param name="uiObj"> UI对象 </param>
- /// <param name="camPath">指定相机位置</param>
- 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<Canvas>();
- if (null == canvas)
- {
- Debug.LogWarning("PanelHelper 设置摄像机,获取画布组件失败,中断");
- return;
- }
- canvas.worldCamera = cameraObj.GetComponent<Camera>();
- }
- /// <summary>
- /// 获取某个界面的显示状态
- /// </summary>
- /// <param name="panel_name"></param>
- /// <returns></returns>
- public bool GetPanelShowState(string _name)
- {
- if (WindowsObjDict.ContainsKey(_name))
- {
- return WindowsObjDict[_name].activeSelf;
- }
- return false;
- }
- #region Guide
- /// <summary>
- /// 是否存在某个物体
- /// </summary>
- /// <param name="ui"></param>
- /// <param name="path"></param>
- /// <param name="obj_name"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 卸载指定控件
- /// </summary>
- /// <param name="ui"></param>
- /// <param name="path"></param>
- /// <param name="obj_name"></param>
- 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);
- }
- }
- }
- }
- /// <summary>
- /// 给 UI 添加一个游戏物体
- /// </summary>
- /// <param name="go"> 游戏物体 </param>
- /// <param name="ui"> UI 的名称 </param>
- /// <param name="path"> 路径 </param>
- /// <param name="offset"> 偏移量 </param>
- /// <param name="ac_Find"> 回调,返回是否查找成功 </param>
- public void AttachObjToUI(GameObject go, string ui, string path, Vector3 offset, Action<GameObject> 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<Canvas>();
- if (canvas == null)
- {
- canvas = t.gameObject.AddComponent<Canvas>();
- }
- canvas.overrideSorting = true;
- canvas.sortingLayerID = 1;
- GraphicRaycaster GR = t.gameObject.GetComponent<GraphicRaycaster>();
- if (GR == null)
- {
- GR = t.gameObject.AddComponent<GraphicRaycaster>();
- }
- GR.enabled = true;
- t.gameObject.AddComponent<UI_GuideDelayCanvas>();
- 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<Canvas>();
- // t.gameObject.AddComponent<GraphicRaycaster>();
- // t.gameObject.AddComponent<UI_GuideDelayCanvas>();
- // }
- // // 查找成功
- // if (null != ac_Find)
- // {
- // ac_Find(t.gameObject);
- // }
- // }
- // else
- // {
- // // 查找失败
- // if (null != ac_Find)
- // {
- // ac_Find(null);
- // }
- // }
- // }, cameraPath);
- //}
- #endregion
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="go"></param>
- /// <param name="arraw"></param>
- public void UnloadObjToUI(GameObject arraw)
- {
- //Debug.LogError("UnloadObjToUI xx");
- Transform prtTran = arraw.transform.parent;
- if (prtTran == null)
- {
- return;
- }
- //回到原来的层级
- Canvas canvas = prtTran.GetComponent<Canvas>();
- if (null != canvas)
- {
- canvas.sortingLayerName = "Default";
- canvas.overrideSorting = false;
- }
- Renderer[] renders = prtTran.GetComponentsInChildren<Renderer>(true);
- for (int i = 0; i < renders.Length; i++)
- {
- renders[i].sortingLayerName = "Default";
- }
- UI_GuideDelayCanvas delay = prtTran.GetComponent<UI_GuideDelayCanvas>();
- if (null != delay)
- {
- delay.enabled = false;
- }
- MonoBehaviour.DestroyObject(arraw);
- }
- /// <summary>
- /// 高亮指定控件
- /// </summary>
- /// <param name="ui"></param>
- /// <param name="path"></param>
- public void OnLightGetComponent(string ui, string path, Action<bool> 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<Canvas>();
- if (canvas == null)
- {
- canvas = t.gameObject.AddComponent<Canvas>();
- }
- canvas.sortingOrder = 100;
- GraphicRaycaster GR = t.gameObject.GetComponent<GraphicRaycaster>();
- if (GR == null)
- {
- t.gameObject.AddComponent<GraphicRaycaster>();
- }
- t.gameObject.AddComponent<UI_GuideDelayCanvas>();
- if (ac != null)
- {
- ac(true);
- }
- return;
- }
- if (ac != null)
- {
- ac(false);
- }
- }
- /// <summary>
- /// 恢复(变暗)指定控件
- /// </summary>
- /// <param name="ui"></param>
- /// <param name="path"></param>
- public void OnDarkGetComponent(string ui, string path, Action<bool> 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<Renderer>(true);
- for (int i = 0; i < renders.Length; i++)
- {
- renders[i].sortingLayerName = "Default";
- }
- UI_GuideDelayCanvas delay = gameObject.GetComponent<UI_GuideDelayCanvas>();
- 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
- }
|