chicken_dy/Assets/Scripts/Core/UIManager/UIManager.cs

146 lines
3.1 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class UIManager : S_MonoSingleton<UIManager>
{
public bool IsUICovered
{
get
{
return mUIList.Count > 0;
}
}
public Canvas UICanvas
{
get
{
if (mUICanvas == null)
{
mUICanvas = GetComponent<Canvas>();
}
return mUICanvas;
}
}
public Camera UICamera
{
get
{
if (mUICamera == null)
{
mUICamera = UICanvas.worldCamera;
}
return mUICamera;
}
}
private Canvas mUICanvas;
private Camera mUICamera;
private Dictionary<Type, BasePanel> mUIDic = new Dictionary<Type, BasePanel>();
private List<BasePanel> mUIList = new List<BasePanel>();
public T OpenUI<T>() where T : BasePanel
{
T tUI = GetUI<T>();
tUI.Open();
if (mUIList.Contains(tUI))
{
mUIList.Remove(tUI);
}
if (mUIList.Count > 0)
{
mUIList[mUIList.Count - 1].Focus(false);
}
mUIList.Add(tUI);
return tUI;
}
public T CloseUI<T>() where T : BasePanel
{
T tUI = GetUI<T>();
tUI.Close();
int tIndex = mUIList.IndexOf(tUI);
if (tIndex == mUIList.Count - 1)
{
mUIList.Remove(tUI);
if (mUIList.Count > 0)
{
mUIList[mUIList.Count - 1].Focus(true);
}
}
else
{
mUIList.Remove(tUI);
}
return tUI;
}
public T GetUI<T>() where T : BasePanel
{
T tUI = null;
if (!mUIDic.ContainsKey(typeof(T)))
{
mUIDic[typeof(T)] = GetComponentInChildren<T>(true);
}
tUI = mUIDic[typeof(T)] as T;
return tUI;
}
public void ShowTip(string pTip)
{
}
public static bool IsTouchUI()
{
if (EventSystem.current != null)
{
PointerEventData eventData = new PointerEventData(EventSystem.current);
eventData.position = Input.mousePosition;
List<RaycastResult> results = new List<RaycastResult>();
EventSystem.current.RaycastAll(eventData, results);
if (results == null || results.Count == 0)
{
return false;
}
return results.Count > 0 && results[0].gameObject.layer == 5;
}
return false;
}
public static List<GameObject> GetChildGobList(Transform pParent)
{
List<GameObject> tList = new List<GameObject>();
for (int i = 0; i < pParent.childCount; i++)
{
tList.Add(pParent.GetChild(i).gameObject);
}
return tList;
}
public static List<T> GetChildList<T>(Transform pParent)
{
List<T> tList = new List<T>();
for (int i = 0; i < pParent.childCount; i++)
{
tList.Add(pParent.GetChild(i).GetComponent<T>());
}
return tList;
}
}