You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
269 lines
8.1 KiB
269 lines
8.1 KiB
using System; |
|
using System.Collections.Generic; |
|
using UnityEngine; |
|
using UnityEngine.AddressableAssets; |
|
using UnityEngine.EventSystems; |
|
using UnityEngine.ResourceManagement.AsyncOperations; |
|
using UnityEngine.UI; |
|
/// <summary> |
|
/// UI管理1.1 |
|
/// 作者:徐振升 |
|
/// 最后更新:2020/07/25 修改适应本项目 |
|
/// 联系方式:QQ:359059686 |
|
/// </summary> |
|
|
|
public enum UIViewType |
|
{ |
|
Normal, Fixed, Popup,Icon |
|
} |
|
public class UIManager : Singleton<UIManager> |
|
{ |
|
private readonly Dictionary<string, UIView> UIViews = new Dictionary<string, UIView>(); |
|
#region Canvas |
|
///// <summary> |
|
///// 获取UI画布 |
|
///// </summary> |
|
public static GameObject GetCanvas() |
|
{ |
|
|
|
GameObject gameObject = GameObject.Find("MainCanvas"); |
|
if (gameObject == null || gameObject.GetComponent<Canvas>() == null) |
|
{ |
|
gameObject = CreateCanvas("MainCanvas"); |
|
} |
|
return gameObject; |
|
} |
|
// 创建新的UI目录 |
|
private static GameObject CreateCanvas(string name) |
|
{ |
|
GameObject gameObject = new GameObject(name) |
|
{ |
|
layer = LayerMask.NameToLayer("UI") |
|
}; |
|
Canvas canvas = gameObject.AddComponent<Canvas>(); |
|
//UICanvas = canvas; |
|
canvas.pixelPerfect = true; |
|
canvas.renderMode = RenderMode.ScreenSpaceOverlay; |
|
CanvasScaler canvasScaler = gameObject.AddComponent<CanvasScaler>(); |
|
canvasScaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize; |
|
canvasScaler.referenceResolution = new Vector2(1920, 1080); |
|
//canvasScaler.matchWidthOrHeight = 0f; |
|
gameObject.AddComponent<GraphicRaycaster>(); |
|
CreateUIRoot(UIViewType.Icon, gameObject.transform); |
|
CreateUIRoot(UIViewType.Fixed, gameObject.transform); |
|
CreateUIRoot(UIViewType.Normal, gameObject.transform); |
|
CreateUIRoot(UIViewType.Popup, gameObject.transform); |
|
//CreateEventSystem(); |
|
return gameObject; |
|
|
|
|
|
} |
|
// 创建UI子目录 |
|
private static Transform CreateUIRoot(UIViewType uiViewType, Transform transform) |
|
{ |
|
GameObject gameObject = new GameObject(uiViewType.ToString()) |
|
{ |
|
layer = LayerMask.NameToLayer("UI") |
|
}; |
|
RectTransform rect = gameObject.AddComponent<RectTransform>(); |
|
gameObject.transform.SetParent(transform); |
|
rect.sizeDelta = Vector2.zero; |
|
rect.localScale = Vector3.one; |
|
rect.localPosition = Vector3.zero; |
|
rect.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, 0, 0); |
|
rect.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 0, 0); |
|
rect.anchorMin = Vector2.zero; |
|
rect.anchorMax = Vector2.one; |
|
//rect.SetAnchor(AnchorPresets.StretchAll); |
|
return gameObject.transform; |
|
} |
|
//// 获取UI子目录 |
|
public static Transform GetUIRoot(UIViewType uiViewType) |
|
{ |
|
Transform canvas = GetCanvas().transform; |
|
Transform root = canvas.Find(uiViewType.ToString()); |
|
if (root == null) |
|
{ |
|
return CreateUIRoot(uiViewType, canvas); |
|
} |
|
return root; |
|
} |
|
// 创建UIEvent |
|
public static void CreateEventSystem() |
|
{ |
|
EventSystem eventSystem = UnityEngine.Object.FindObjectOfType<EventSystem>(); |
|
if (eventSystem == null) |
|
{ |
|
GameObject gameObject = new GameObject("EventSystem"); |
|
eventSystem = gameObject.AddComponent<EventSystem>(); |
|
gameObject.AddComponent<StandaloneInputModule>(); |
|
} |
|
} |
|
#endregion |
|
/// <summary> |
|
/// 添加页面 |
|
/// </summary> |
|
/// <param name="uiView"></param> |
|
public void AddView(UIView uiView) |
|
{ |
|
string viewName = FormattingViewName(uiView); |
|
uiView.gameObject.name = viewName; |
|
if (UIViews.ContainsKey(viewName)) |
|
{ |
|
Debug.Log($"[{viewName}]已存在"); |
|
//MessageBox.ShowInfo($"[{viewName}]已存在", Color.white); |
|
} |
|
else |
|
{ |
|
UIViews.Add(viewName, uiView); |
|
} |
|
} |
|
/// <summary> |
|
/// 移除页面 |
|
/// </summary> |
|
/// <param name="uiView"></param> |
|
public void RemoveView(UIView uiView) |
|
{ |
|
string viewName = FormattingViewName(uiView); |
|
if (UIViews.ContainsKey(viewName)) |
|
{ |
|
UIViews.Remove(viewName); |
|
} |
|
else |
|
{ |
|
Debug.Log($"[{viewName}]不存在"); |
|
//MessageBox.ShowInfo($"[{viewName}]不存在", Color.white); |
|
} |
|
} |
|
/// <summary> |
|
/// 格式化UI名称 |
|
/// </summary> |
|
/// <typeparam name="T"></typeparam> |
|
/// <param name="viewName"></param> |
|
private static string FormattingViewName(UIView uiView) |
|
{ |
|
return $"{uiView.GetType().ToString()}"; |
|
} |
|
private void LoadView<T>() |
|
where T : UIView |
|
{ |
|
string viewName = typeof(T).ToString(); |
|
if (string.IsNullOrEmpty(viewName)) return; |
|
var view = GetView<T>(); |
|
|
|
if (view != null) |
|
{ |
|
view.Show(); |
|
|
|
} |
|
else |
|
{ |
|
Addressables.LoadAssetAsync<GameObject>(viewName).Completed += Loaded => |
|
{ |
|
GameObject prefab = Loaded.Result; |
|
if (prefab != null) |
|
{ |
|
Transform parent = GetUIRoot(prefab.GetComponent<T>().ViewType); |
|
//实例化 |
|
GameObject gameObject = GameObject.Instantiate(prefab, parent); |
|
view = gameObject.GetComponent<T>(); |
|
view.Show(); |
|
} |
|
else |
|
{ |
|
Debug.Log($"加载{viewName}预设失败"); |
|
} |
|
}; |
|
} |
|
} |
|
|
|
private void LoadView<T>(Action callback, object data) |
|
where T : UIView |
|
{ |
|
string viewName = typeof(T).ToString(); |
|
if (string.IsNullOrEmpty(viewName)) return; |
|
var view = GetView<T>(); |
|
|
|
if (view != null) |
|
{ |
|
view.Show(data); |
|
callback?.Invoke(); |
|
} |
|
else |
|
{ |
|
Addressables.LoadAssetAsync<GameObject>(viewName).Completed += Loaded => |
|
{ |
|
GameObject prefab = Loaded.Result; |
|
if (prefab != null) |
|
{ |
|
Transform parent = GetUIRoot(prefab.GetComponent<T>().ViewType); |
|
//实例化 |
|
GameObject gameObject = GameObject.Instantiate(prefab, parent); |
|
view = gameObject.GetComponent<T>(); |
|
view.Show(data); |
|
callback?.Invoke(); |
|
} |
|
else |
|
{ |
|
Debug.Log($"加载{viewName}预设失败"); |
|
} |
|
}; |
|
} |
|
} |
|
|
|
/// <summary> |
|
/// 获取页面 |
|
/// </summary> |
|
/// <typeparam name="T">预设上脚本类型</typeparam> |
|
/// <param name="viewName">预设上脚本属性ViewName</param> |
|
/// <returns></returns> |
|
public T GetView<T>() |
|
where T : UIView |
|
{ |
|
Type t= typeof(T); |
|
string viewName = t.ToString(); |
|
if (UIViews.ContainsKey(viewName)) |
|
{ |
|
return UIViews[viewName] as T; |
|
} |
|
else |
|
{ |
|
return null; |
|
} |
|
} |
|
/// <summary> |
|
/// 如果该页面已经创建则显示该页面,否则创建该页面并显示 |
|
/// </summary> |
|
/// <param name="viewName"></param> |
|
public void Show<TView>() |
|
where TView : UIView |
|
{ |
|
LoadView<TView>(); |
|
//view.Show(); |
|
} |
|
public void Show<TView>(Action callback) where TView : UIView |
|
{ |
|
LoadView<TView>(callback,null); |
|
} |
|
/// <summary> |
|
/// 显示界面 |
|
/// </summary> |
|
/// <typeparam name="TView"></typeparam> |
|
/// <param name="data"></param> |
|
public void Show<TView>(object data) |
|
where TView : UIView |
|
{ |
|
LoadView<TView>(null,data); |
|
//view.Show(data); |
|
} |
|
/// <summary> |
|
/// 隐藏页面 |
|
/// </summary> |
|
/// <param name="viewName"></param> |
|
public void Hide<TView>() |
|
where TView : UIView |
|
{ |
|
TView view = GetView<TView>(); |
|
view?.Hide(); |
|
} |
|
}
|
|
|