using System;
using System.Collections.Generic;
using System.Xml.Linq;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.Rendering.PostProcessing;
///
/// UI管理器
///
public partial class UIManager : MBSingletonManager
{
///
/// UI节点
///
private Transform uiNode;
///
/// UI相机
///
[HideInInspector]public Camera uICamera;
///
/// 管理所有UI画布组
///
private CanvasGroup allUICG;
public PostProcessVolume postProcessVolume;
public PostProcessLayer processLayer;
///
/// UI列表
///
public List uIList = new List();
private void Awake()
{
Init();
}
///
/// 初始化
///
private void Init()
{
uiNode = transform.Find("UINode");
uICamera = transform.Find("UICamera").GetComponent();
allUICG=GetComponent();
}
///
/// 异步展示UI
///
public void AsynShowUI(UnityAction callback = null) where T : UIBaseClass
{
string uIName = typeof(T).Name;
T uI;
uI = GetUI();
if (uI != null)
{
uI.gameObject.SetActive(true);
uI.ShowPanelAnimation();
uI.OnShow();
callback?.Invoke(uI);
}
else
{
AsynGenerateUI(uIName, (uIBaseClass) =>
{
uI = uIBaseClass;
UICorrection(uI.transform, uIName);
uI.ShowPanelAnimation();
uI.OnShow();
uIList.Add(uI);
callback?.Invoke(uI);
});
}
}
///
/// 在Resources加载UI
///
///
///
public void ResourcesShowUI(UnityAction callback = null) where T : UIBaseClass
{
string uIName = typeof(T).Name;
T uI;
uI = GetUI();
if (uI != null)
{
uI.gameObject.SetActive(true);
uI.ShowPanelAnimation();
uI.OnShow();
callback?.Invoke(uI);
}
else
{
uI = Instantiate(Resources.Load(uIName)).GetComponent();
UICorrection(uI.transform, uIName);
uI.ShowPanelAnimation();
uI.OnShow();
uIList.Add(uI);
callback?.Invoke(uI);
}
}
///
/// 异步展示UI
///
public void AsynShowUI(string uIName,UnityAction callback)
{
UIBaseClass uI = uIList.Find(s => s.name == uIName);
if (uI != null)
{
uI.gameObject.SetActive(true);
uI.ShowPanelAnimation();
uI.OnShow();
callback?.Invoke(uI);
}
else
{
AsynGenerateUI(uIName, (uIBaseClass) =>
{
uI = uIBaseClass;
UICorrection(uI.transform, uIName);
uI.ShowPanelAnimation();
uI.OnShow();
uIList.Add(uI);
callback?.Invoke(uI);
});
}
}
///
/// 预加载UI
///
public void PreloadedUI()where T : UIBaseClass
{
string uIName = typeof(T).Name;
T uI;
AsynGenerateUI(uIName, (uiObj) =>
{
uI = uiObj;
UICorrection(uI.transform, uIName);
uI.gameObject.SetActive(false);
uI.OnShow();
uIList.Add(uI);
});
}
///
/// 关闭UI
///
public void HideUI() where T : UIBaseClass
{
UIBaseClass uI;
uI = GetUI();
if (uI != null)
{
uI.OnHide();
uI.HidePanel(() =>
{
uI.gameObject.SetActive(false);
});
}
else
{
Debug.LogError("关闭_没有这个UI:" + name);
}
}
///
/// 关闭UI
///
public void HideUI(string uIName)
{
UIBaseClass uI = uIList.Find(s => s.name == uIName);
if (uI != null)
{
uI.OnHide();
uI.HidePanel(() =>
{
uI.gameObject.SetActive(false);
});
}
else
{
Debug.LogError("关闭_没有这个UI:" + name);
}
}
///
/// 关闭所有UI
///
/// 保留的UI
public void HideAll(List retainUI)
{
bool shouldRetain=false;
for (int i = uIList.Count - 1; i >= 0; i--)
{
if (retainUI != null && retainUI.Contains(uIList[i].name))
{
shouldRetain = true;
}
if (!shouldRetain)
{
uIList[i].HidePanel(() =>
{
uIList[i].gameObject.SetActive(false);
});
}
}
}
///
/// 删除UI
///
public void DeleteUI() where T : UIBaseClass
{
UIBaseClass uI;
uI = GetUI();
if (uI != null)
{
uI.OnDelete();
if (uI.gameObject.activeInHierarchy)
{
uI.HidePanel(() =>
{
uIList.Remove(uI);
Destroy(uI.gameObject);
});
}
else
{
uIList.Remove(uI);
Destroy(uI.gameObject);
}
}
}
///
/// 删除UI
///
public void DeleteUI(string uIName)
{
UIBaseClass uI= uIList.Find(s => s.name == uIName);
if (uI != null)
{
uI.OnDelete();
if (uI.gameObject.activeInHierarchy)
{
uI.HidePanel(() =>
{
uIList.Remove(uI);
Destroy(uI.gameObject);
});
}
else
{
uIList.Remove(uI);
Destroy(uI.gameObject);
}
}
else
{
Debug.LogError("删除_没有这个UI:" + name);
}
}
///
/// 删除所有UI
///
/// 保留UI
public void DestroyAll(List retainUI)
{
bool shouldRetain;
for (int i = uIList.Count - 1; i >= 0; i--)
{
shouldRetain = retainUI.Contains(uIList[i].name);
if (!shouldRetain)
{
uIList[i].HidePanel(() =>
{
Destroy(uIList[i].gameObject);
uIList.RemoveAt(i);
});
}
}
}
///
/// 得到UI
///
public T GetUI() where T : UIBaseClass
{
string uIName = typeof(T).Name;
for (int i = 0; i < uIList.Count; i++)
{
if (uIName == uIList[i].name)
{
return uIList[i] as T;
}
}
return null;
}
///
/// 异步生成UI
///
private void AsynGenerateUI(string name, UnityAction callback)
{
YooAssetManager.Instance.LoadAssetAsync(PackagesName.GameData, name, (uiObj) =>
{
T uiBaseClass = Instantiate(uiObj).GetComponent();
callback(uiBaseClass);
});
}
///
///给控件添加自定义事件监听
///
/// 控件
/// 监听类型
/// 回调
public void AddCustomEventListener(UIBehaviour control,EventTriggerType type,UnityAction callBack)
{
EventTrigger trigger = control.GetComponent();
if(trigger==null)
{
trigger = control.gameObject.AddComponent();
}
EventTrigger.Entry entry = new EventTrigger.Entry();
entry.eventID = type;
entry.callback.AddListener(callBack);
trigger.triggers.Add(entry);
}
///
/// 移除控件的自定义事件监听
///
/// 控件
/// 监听类型
/// 回调
public void RemoveCustomEventListener(UIBehaviour control, EventTriggerType type)
{
EventTrigger trigger = control.GetComponent();
if (trigger != null)
{
// 遍历当前的所有触发器事件
foreach (var entry in trigger.triggers)
{
if (entry.eventID == type)
{
// 移除回调
entry.callback.RemoveAllListeners();
// 如果没有其他回调函数,移除该事件条目
if (entry.callback.GetPersistentEventCount() == 0)
{
trigger.triggers.Remove(entry);
break; // 一旦移除成功,跳出循环
}
}
}
}
}
///
/// UI矫正
///
private void UICorrection(Transform uI, string uIName)
{
uI.name = uIName;
uI.transform.SetParent(uiNode);
uI.transform.localPosition = Vector3.zero;
uI.transform.localScale = Vector3.one;
(uI.transform as RectTransform).offsetMax = Vector3.zero;
(uI.transform as RectTransform).offsetMin = Vector3.zero;
}
///
/// 全部失效或开启
///
public void AllLapseInitiate(bool isAllLapse)
{
allUICG.alpha = isAllLapse==true?1:0;
}
///
/// 设置Voluem
///
///
public void SetPostProcessVolume(bool isShow)
{
postProcessVolume.isGlobal = isShow;
}
///
/// 删除后处理
///
public void DesProcess()
{
Destroy(postProcessVolume);
Destroy(processLayer);
}
}