using System; using UnityEngine; using UnityEngine.UI; using System.Collections.Generic; namespace SK.Framework { public class UI : MonoBehaviour { private static UI instance; private Dictionary viewDic; public static UI Instance { get { if (instance == null) { UI res = Resources.Load("UI"); if (null == res) { Debug.LogError("加载UI预制体失败"); } else { instance = Instantiate(res); instance.name = "[SKFramework.UI]"; instance.viewDic = new Dictionary(); string[] levelNames = Enum.GetNames(typeof(ViewLevel)); for (int i = levelNames.Length - 1; i >= 0; i--) { string levelName = levelNames[i]; var levelInstance = new GameObject(levelName); levelInstance.layer = LayerMask.NameToLayer("UI"); levelInstance.transform.SetParent(instance.transform, false); RectTransform rectTransform = levelInstance.AddComponent(); rectTransform.sizeDelta = instance.GetComponent().referenceResolution; rectTransform.anchorMin = Vector2.zero; rectTransform.anchorMax = Vector2.one; rectTransform.offsetMin = rectTransform.offsetMax = Vector2.zero; rectTransform.SetAsFirstSibling(); } DontDestroyOnLoad(instance); } } return instance; } } public static Canvas Canvas { get { return Instance.GetComponent(); } } public static Camera Camera { get { return Instance.GetComponentInChildren(); } } public static Vector2 Resolution { get { return Instance.GetComponent().referenceResolution; } } /// /// 加载视图 /// /// 视图命名 /// 视图资源路径 /// 视图层级 /// 视图 /// 视图数据 /// 是否立即显示 /// 成功加载返回true 否则返回false public bool LoadView(string viewName, string viewResourcePath, ViewLevel level, out IUIView view, IViewData data = null, bool instant = false) { if (!viewDic.TryGetValue(viewName, out view)) { GameObject viewPrefab = Resources.Load(viewResourcePath); if (null != viewPrefab) { var instance = Instantiate(viewPrefab); instance.transform.SetParent(transform.GetChild((int)level), false); instance.name = viewName; view = instance.GetComponent(); view.Name = viewName; view.Init(data, instant); viewDic.Add(viewName, view); return true; } } return false; } /// /// 显示视图 /// /// 视图名称 /// 视图数据 /// 是否立即显示 /// 视图 public IUIView ShowView(string viewName, IViewData data = null, bool instant = false) { if (viewDic.TryGetValue(viewName, out IUIView view)) { view.Show(data, instant); } return view; } /// /// 隐藏视图 /// /// 视图名称 /// 是否立即隐藏 /// 视图 public IUIView HideView(string viewName, bool instant = false) { if (viewDic.TryGetValue(viewName, out IUIView view)) { view.Hide(instant); } return view; } /// /// 获取视图 /// /// 视图名称 /// 视图 public IUIView GetView(string viewName) { viewDic.TryGetValue(viewName, out IUIView view); return view; } /// /// 卸载视图 /// /// 视图名称 /// 是否立即卸载 /// 成功卸载返回true 否则返回false public bool UnloadView(string viewName, bool instant = false) { if (viewDic.TryGetValue(viewName, out IUIView view)) { viewDic.Remove(viewName); view.Unload(instant); return true; } return false; } /// /// 卸载所有视图 /// public void UnloadAll() { List views = new List(); foreach (var kv in viewDic) { views.Add(kv.Value); } for (int i = 0; i < views.Count; i++) { views[i].Unload(true); views.RemoveAt(i); i--; } viewDic.Clear(); } /// /// 从字典中移除 /// /// 视图名称 public void Remove(string viewName) { if (viewDic.ContainsKey(viewName)) { viewDic.Remove(viewName); } } } }