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