165 lines
		
	
	
		
			4.1 KiB
		
	
	
	
		
			C#
		
	
	
	
		
		
			
		
	
	
			165 lines
		
	
	
		
			4.1 KiB
		
	
	
	
		
			C#
		
	
	
	
|  | using System; | |||
|  | using System.Collections; | |||
|  | using System.Collections.Generic; | |||
|  | using UnityEngine; | |||
|  | using UnityEngine.EventSystems; | |||
|  | 
 | |||
|  | namespace MMO | |||
|  | { | |||
|  |     public class MMOUIManager : MonoBehaviour | |||
|  |     { | |||
|  |         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, MMOUIBase> mUIDic = new Dictionary<Type, MMOUIBase>(); | |||
|  |         private List<MMOUIBase> mUIList = new List<MMOUIBase>(); | |||
|  | 
 | |||
|  |         public T OpenUI<T>() where T : MMOUIBase | |||
|  |         { | |||
|  |             T tUI = GetUI<T>(); | |||
|  |             tUI.gameObject.SetActive(true); | |||
|  |             tUI.OnOpen(); | |||
|  | 
 | |||
|  |             if (tUI.IsStack) | |||
|  |             { | |||
|  |                 if (mUIList.Contains(tUI)) | |||
|  |                 { | |||
|  |                     mUIList.Remove(tUI); | |||
|  |                 } | |||
|  |                 if (mUIList.Count > 0) | |||
|  |                 { | |||
|  |                     mUIList[mUIList.Count - 1].OnFocus(false); | |||
|  |                 } | |||
|  |                 mUIList.Add(tUI); | |||
|  |             } | |||
|  | 
 | |||
|  |             return tUI; | |||
|  |         } | |||
|  | 
 | |||
|  |         public T CloseUI<T>() where T : MMOUIBase | |||
|  |         { | |||
|  |             T tUI = GetUI<T>(); | |||
|  |             CloseUI(tUI); | |||
|  | 
 | |||
|  |             return tUI; | |||
|  |         } | |||
|  | 
 | |||
|  |         public void CloseUI(MMOUIBase pMMOUIBase) | |||
|  |         { | |||
|  |             pMMOUIBase.gameObject.SetActive(false); | |||
|  |             pMMOUIBase.OnClose(); | |||
|  | 
 | |||
|  |             if (pMMOUIBase.IsStack) | |||
|  |             { | |||
|  |                 int tIndex = mUIList.IndexOf(pMMOUIBase); | |||
|  |                 if (tIndex == mUIList.Count - 1) | |||
|  |                 { | |||
|  |                     mUIList.Remove(pMMOUIBase); | |||
|  |                     if (mUIList.Count > 0) | |||
|  |                     { | |||
|  |                         mUIList[mUIList.Count - 1].OnFocus(true); | |||
|  |                     } | |||
|  |                 } | |||
|  |                 else | |||
|  |                 { | |||
|  |                     mUIList.Remove(pMMOUIBase); | |||
|  |                 } | |||
|  |             } | |||
|  |         } | |||
|  | 
 | |||
|  |         public T GetUI<T>() where T : MMOUIBase | |||
|  |         { | |||
|  |             T tUI = null; | |||
|  |             if (!mUIDic.ContainsKey(typeof(T))) | |||
|  |             { | |||
|  |                 mUIDic[typeof(T)] = GetComponentInChildren<T>(true); | |||
|  |             } | |||
|  | 
 | |||
|  |             tUI = mUIDic[typeof(T)] as T; | |||
|  | 
 | |||
|  |             return tUI; | |||
|  |         } | |||
|  | 
 | |||
|  |         public void CloseAll() | |||
|  |         { | |||
|  |             for (int i = mUIList.Count - 1; i >= 0; i--) | |||
|  |             { | |||
|  |                 CloseUI(mUIList[i]); | |||
|  |             } | |||
|  |         } | |||
|  | 
 | |||
|  |         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; | |||
|  |         } | |||
|  |     } | |||
|  | } |