using System; using System.Collections.Generic; using System.IO; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; using Object = UnityEngine.Object; /// /// UI管理 /// 作者:徐振升 /// 最后更新:2018-07-17 15:30 /// 联系方式:QQ:359059686 /// 项目没有UI管理,此脚本目的为了方便UI管理,目前管理注册中的页面,请勿随意修改。 /// public static class UIManager { private static Dictionary templates = new Dictionary(); private static Dictionary Views = new Dictionary(); private static Dictionary UIPath = new Dictionary(); static UIManager() { //①手动注册UI //RegisterUI("UI/XLogUIView"); //②自动注册UI //请把UIView名称与预设名称相同,放在Resource文件根目录下。如自动注册XLogUIView=Resource/XLogUIView //私聊界面 RegisterUI("UI/UIViewPrivateChat"); //公聊界面 RegisterUI("UI/UIViewPublicChat"); //器材库界面 RegisterUI("UI/UIViewEquipLib"); //数量选择界面 RegisterUI("UI/UIViewNumberSelect"); //背包界面 RegisterUI("UI/UIViewBag"); //建议界面 RegisterUI("UI/UIViewSuggest"); //角色报告 RegisterUI("UI/UIViewReport"); //问卷调查 RegisterUI("UI/UIViewQuestionnaire"); //评分记录 RegisterUI("UI/UIViewScore"); //队伍分析 RegisterUI("UI/UIViewAnalyze"); } // 注册UI public static void RegisterUI(string prefabPath) where T : UIView { if (!UIPath.ContainsKey(typeof(T).ToString())) { UIPath.Add(typeof(T).ToString(), prefabPath); } } public static string Normalize(string name) { int num = name.IndexOf('.'); if (num < 0) { return name; } return name.Substring(0, num); } // 加载UI private static void LoadView(string viewName) where T : UIView { string tempKey = typeof(T).ToString(); if (!UIPath.ContainsKey(tempKey)) { // 没有注册先注册 RegisterUI(typeof(T).ToString()); } string tempPath = Normalize(UIPath[tempKey]); WeakReference value; GameObject gameObject; if (templates.TryGetValue(tempPath, out value) && value.IsAlive) { gameObject = (GameObject)value.Target; } else { gameObject = Resources.Load(tempPath); if (gameObject != null) { gameObject.SetActive(false); templates[tempPath] = new WeakReference(gameObject); } else { // Resource根目录没有发现UIView的Prefab,请添加。 throw new Exception(string.Format("<<{0}>> prefab not found in Resource folder.", tempPath)); } } if (gameObject != null && gameObject.GetComponent() != null) { //实例化 GameObject ui = GameObject.Instantiate(gameObject); ui.name = gameObject.name; //记录初始大小 Vector3 anchorPos3D = ui.GetComponent().anchoredPosition3D; Vector3 anchorPos = ui.GetComponent().anchoredPosition; Vector3 sizeDel = ui.GetComponent().sizeDelta; Vector3 scale = ui.GetComponent().localScale; //设置父对象 T view = ui.GetComponent(); Transform parent = GetUICanvas().transform; ui.transform.SetParent(parent); //还原初始大小 ui.GetComponent().anchoredPosition3D = anchorPos3D; ui.GetComponent().anchoredPosition = anchorPos; ui.GetComponent().sizeDelta = sizeDel; ui.GetComponent().localScale = scale; view.ViewName = viewName; Views.Add(viewName, view); } } private static GameObject GetUICanvas() { GameObject gameObject = GameObject.Find("Canvas"); if (gameObject == null || gameObject.GetComponent() == null) { gameObject = CreateNewUI(); } return gameObject; } [Obsolete("暂时弃用,使用GetUICanvas")] // 获取对象目录 private static Transform GetUIElementRoot(UIViewType type) { GameObject gameObject = GameObject.Find("Canvas"); if (gameObject == null || gameObject.GetComponent() == null) { gameObject = CreateNewUI(); } Transform transform = gameObject.transform.Find(type.ToString()); if (transform == null) { CreateUIElementRoot("Normal", gameObject); CreateUIElementRoot("Popup", gameObject); transform = gameObject.transform.Find(type.ToString()); } return transform; } // 创建新的UI目录 public static GameObject CreateNewUI() { GameObject gameObject = new GameObject("Canvas"); gameObject.layer = LayerMask.NameToLayer("UI"); Canvas canvas = gameObject.AddComponent(); canvas.renderMode = RenderMode.ScreenSpaceOverlay; gameObject.AddComponent(); gameObject.AddComponent(); CreateEventSystem(); return gameObject; } // 创建UI子目录 private static void CreateUIElementRoot(string name, GameObject obj) { GameObject gameObject = new GameObject(name); gameObject.layer = LayerMask.NameToLayer("UI"); RectTransform rect = gameObject.AddComponent(); gameObject.transform.SetParent(obj.transform); rect.sizeDelta = Vector2.zero; rect.localScale = Vector3.one; rect.localPosition = Vector3.zero; rect.SetAnchor(AnchorPresets.StretchAll); } // 创建UIEvent private static void CreateEventSystem() { EventSystem eventSystem = Object.FindObjectOfType(); if (eventSystem == null) { GameObject gameObject = new GameObject("EventSystem"); eventSystem = gameObject.AddComponent(); gameObject.AddComponent(); } } // 显示页面 public static void ShowView() where T : UIView { string viewName = typeof(T).ToString(); ShowView(viewName); } // 显示指定名称页面 public static void ShowView(string viewName) where T : UIView { //没有加载页面先加载 if (!Views.ContainsKey(viewName)) { LoadView(viewName); } Views[viewName].Show(); } // 获取页面 public static T GetView() where T : UIView { string viewName = typeof(T).ToString(); return GetView(viewName); } // 获取指定名称页面 public static T GetView(string viewName) where T : UIView { //没有页面先加载 if (!Views.ContainsKey(viewName)) { LoadView(viewName); } return Views[viewName] as T; } // 隐藏页面 public static void HideView() where T : UIView { string viewName = typeof(T).ToString(); HideView(viewName); } // 隐藏指定名称页面 public static void HideView(string viewName) where T : UIView { //没有加载页面先加载 if (!Views.ContainsKey(viewName)) { LoadView(viewName); } Views[viewName].Hide(); } // 隐藏页面 public static void DestroyView() where T : UIView { string viewName = typeof(T).ToString(); DestroyView(viewName); } // 销毁页面 public static void DestroyView(string viewName) { if (Views != null && Views.ContainsKey(viewName) && viewName != null) { Views.Remove(viewName); } } public static void ResertUIManager() { if (templates.Count > 0) { templates.Clear(); } if (Views.Count > 0) { Views.Clear(); } if (UIPath.Count > 0) { UIPath.Clear(); } } }