using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class UIManager : Singleton<UIManager>
{
//UI gameobjects
public Dictionary<E_UIType, GameObject> _dictUIs = new Dictionary<E_UIType, GameObject>();
//UI panels
public Dictionary<E_PanelType, GameObject> _dictPanels = new Dictionary<E_PanelType, GameObject>();
void Awake()
{
_instance = this;
}
#region 打开UI 关闭UI
//打开UI
public void OpenUI(E_UIType uiType, bool closeOtherUI, object args)
{
if (closeOtherUI == true)
{
CloseUIAll();
}
if (_dictUIs.ContainsKey(uiType))
{
_dictUIs[uiType].SetActive(true);
}
else
{
GameObject go = MonoBehaviour.Instantiate(Resources.Load("UIPrefabs/" + uiType.ToString())) as GameObject;
_dictUIs.Add(uiType, go);
}
BaseUI bu = _dictUIs[uiType].GetComponent<BaseUI>();
if (bu != null)
{
bu.UIState = E_UIState.Open;
Debug.Log("do set ui state");
}
if (args != null)
{
bu.LoadData(args);
}
Debug.Log("UIManager存了:" + _dictUIs.Count + " 个UI");
}
//打开UI 重载
public void OpenUI(E_UIType uiType, bool closeOtherUI)
{
OpenUI(uiType, closeOtherUI, null);
}
//打开UI 重载
public void OpenUI(E_UIType uiType)
{
OpenUI(uiType, false);
}
//关闭UI
public void CloseUI(E_UIType uiType)
{
if (!_dictUIs.ContainsKey(uiType)) return;
BaseUI bu = _dictUIs[uiType].GetComponent<BaseUI>();
if (bu != null)
{
bu.UIState = E_UIState.Close;
}
_dictUIs[uiType].SetActive(false);
//_dictUIs.Remove(uiType);
//Destroy(go);
Debug.Log("SetActive false 一个UI : " + uiType);
}
//关闭所有UI
public void CloseUIAll()
{
if (_dictUIs == null) return;
List<E_UIType> UIkeys = new List<E_UIType>(_dictUIs.Keys);
for (int i = 0; i < UIkeys.Count; i++)
{
CloseUI(UIkeys[i]);
}
_dictUIs.Clear();
}
public void ClearAllUI()
{
_dictUIs.Clear();
}
//拿到指定的UI
public GameObject GetUIObj(E_UIType uiType)
{
if (_dictUIs.ContainsKey(uiType))
{
return _dictUIs[uiType];
}
else
{
return null;
}
}
#endregion
#region 打开Panel 关闭Panel
//init Save Panels
public void SavePanel(E_PanelType type, GameObject panel)
{
if (!_dictPanels.ContainsKey(type))
{
_dictPanels.Add(type, panel);
Debug.Log("save panel : " + type);
}
}
//打开Panel
public void OpenPanel(E_PanelType type, bool CloseOthers, bool closeOthersButMain, E_PanelType dontCloseType)
{
//关闭其他Panel
if (!CloseOthers)
{
if (closeOthersButMain)
{
if (dontCloseType == E_PanelType.None)
CloseAllButMainPanel();
else
CloseAllButMainPanel(dontCloseType);
}
else
{
CloseAllPanel();
}
}
//打开Panel
if (_dictPanels.ContainsKey(type))
{
if (!_dictPanels[type].activeSelf)
{
_dictPanels[type].SetActive(true);
BasePanel bp = _dictPanels[type].GetComponent<BasePanel>();
if (bp != null)
{
bp.PanelState = E_PanelState.Open;
}
Debug.Log("open panel type : " + type);
}
}
}
//重载打开panel
public void OpenPanel(E_PanelType type, bool dontCloseOthers, bool closeOthersButMain)
{
OpenPanel(type, dontCloseOthers, closeOthersButMain, E_PanelType.None);
}
//重载打开panel
public void OpenPanel(E_PanelType type, E_PanelType dontClose)
{
OpenPanel(type, false, false);
}
//重载打开panel
public void OpenPanel(E_PanelType type, bool closeAssignPanel)
{
OpenPanel(type, false, false);
}
public void OpenPanel(E_PanelType type)
{
OpenPanel(type, true, false, E_PanelType.None);
}
//关闭Panel
public void ClosePanel(E_PanelType type)
{
if (_dictPanels.ContainsKey(type))
{
if (_dictPanels[type].activeSelf)
{
BasePanel bp = _dictPanels[type].GetComponent<BasePanel>();
if (bp != null)
{
bp.PanelState = E_PanelState.Close;
}
_dictPanels[type].SetActive(false);
Debug.Log("close panel type : " + type);
}
}
}
//关闭所有panel
public void CloseAllPanel()
{
CloseAllPanel(E_PanelType.None);
}
//关闭所有 but assignpanel
public void CloseAllPanel(E_PanelType type)
{
List<E_PanelType> list = new List<E_PanelType>(_dictPanels.Keys);
for (int i = 0; i < list.Count; i++)
{
if (list[i] == type)
continue;
ClosePanel(list[i]);
}
}
//关闭UIpanel 但是不关闭mian Panel
public void CloseAllButMainPanel()
{
List<E_PanelType> list = new List<E_PanelType>(_dictPanels.Keys);
for (int i = 0; i < list.Count; i++)
{
if (list[i] == E_PanelType.E_BtnPanel || list[i] == E_PanelType.E_MianFacePanel)
continue;
ClosePanel(list[i]);
}
}
//不关闭mian pane And assigen panel
public void CloseAllButMainPanel(E_PanelType type)
{
List<E_PanelType> list = new List<E_PanelType>(_dictPanels.Keys);
for (int i = 0; i < list.Count; i++)
{
if (list[i] == E_PanelType.E_BtnPanel || list[i] == E_PanelType.E_MianFacePanel || list[i] == type)
continue;
ClosePanel(list[i]);
}
}
//关闭UIpanel 但是不关闭指定 Panel
public void CloseOthersButAssignPanel(E_PanelType type)
{
CloseAllPanel(type);
}
//重新载入场景 清空所有
public void ClearDictPanel()
{
//if (_dictPanels.Count > 0)
//{
// List<E_PanelType> list = new List<E_PanelType>(_dictPanels.Keys);
// for (int i = 0; i < list.Count; i++)
// {
// MonoBehaviour.Destroy(_dictPanels[list[i]]);
// }
//}
_dictPanels.Clear();
}
//拿到指定panel
public GameObject GetPanelObj(E_PanelType type)
{
if (_dictPanels.ContainsKey(type))
{
return _dictPanels[type];
}
return null;
}
#endregion
}