NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Framework/UI/UIWindow.cs

1043 lines
29 KiB
C#
Raw Normal View History

2023-08-22 19:08:45 +08:00
using System;
using System.Collections.Generic;
using System.Reflection;
2023-11-21 13:31:06 +08:00
using Cysharp.Threading.Tasks;
2024-08-12 19:31:50 +08:00
using DG.Tweening;
2023-10-19 15:00:19 +08:00
using Framework;
using PhxhSDK;
2023-08-22 19:08:45 +08:00
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
using UnityEngine.Serialization;
using Object = System.Object;
2024-05-10 19:18:40 +08:00
using UniTask = Cysharp.Threading.Tasks.UniTask;
2023-08-22 19:08:45 +08:00
2024-01-26 16:06:42 +08:00
public abstract class UIWindow
2023-08-22 19:08:45 +08:00
{
2024-01-26 16:06:42 +08:00
public GameObject gameObject;
public Transform transform;
public RectTransform rectTransform;
public string name;
2023-11-21 13:31:06 +08:00
private const string DEFAULT_SHOW_ANIM = "show";
private const string DEFAULT_HIDE_ANIM = "hide";
2023-08-22 19:08:45 +08:00
private string mWindowName;
private ScreenOrientation _lastOrientation;
private Vector2 _offsets; //x,y代表左右的偏移量
2023-08-22 19:08:45 +08:00
2024-08-28 15:08:46 +08:00
public delegate void OnBgClose();
private OnBgClose _onBgCloseFunc;
2025-03-13 13:25:04 +08:00
protected Dictionary<string, int> AsyncLoadedAssetList = new Dictionary<string, int>(10);
2024-01-26 16:06:42 +08:00
private bool _isNeedScreenAdaption;
private bool _bDoNotSetMainCamera = false; //不设置主相机
private bool _isCloseCbCalled;
2025-03-13 13:25:04 +08:00
private GameObject _goLeft;
private GameObject _goRight;
2024-12-09 15:56:56 +08:00
public bool IsUnEnabled; //是否不被激活
2025-04-16 12:24:10 +08:00
public bool IsClosing; //正在关闭
2024-12-09 15:56:56 +08:00
public bool IsNotSetMainCamera
{
get { return _bDoNotSetMainCamera; }
set { _bDoNotSetMainCamera = value; }
}
2024-01-26 16:06:42 +08:00
2023-08-22 19:08:45 +08:00
// 是否播放默认的打开对话框的声音
public bool isPlayDefaultOpenAudio = true;
// 是否播放默认的关闭对话框的声音
public bool isPlayDefaultCloseAudio = true;
2024-08-12 19:31:50 +08:00
private bool _isPlayingOpenAnim;
protected Sequence _openAnimSeq;
2024-08-12 19:31:50 +08:00
public bool IsShowOpenAnim;
2023-08-22 19:08:45 +08:00
public string WindowName
{
set { mWindowName = value; }
get { return mWindowName; }
}
2023-11-21 13:31:06 +08:00
2023-08-22 19:08:45 +08:00
[HideInInspector]
public UIWindowLayer uiWindowLayer;
2023-10-19 15:00:19 +08:00
[HideInInspector]
2023-08-22 19:08:45 +08:00
2024-06-03 17:06:54 +08:00
protected bool isCreated = false;
2024-04-29 15:39:46 +08:00
2024-06-03 17:06:54 +08:00
protected bool _isShow = false;
protected bool _isHide = false;
2024-01-24 15:42:01 +08:00
2024-01-26 16:06:42 +08:00
protected void RegisterScreenAdaption()
{
_lastOrientation = Screen.orientation;
InitScreenUIOffset();
UpdateScreenAdaption();
2024-01-26 16:06:42 +08:00
_isNeedScreenAdaption = true;
}
2024-05-08 16:16:50 +08:00
public async UniTask<T> LoadAssetAsync<T>(string path) where T : UnityEngine.Object
{
var asset = await AssetManager.Instance.LoadAssetAsync<T>(path);
if (gameObject)
2024-05-08 12:44:22 +08:00
{
if (!AsyncLoadedAssetList.ContainsKey(path))
{
AsyncLoadedAssetList.Add(path, 1);
}
else
{
AsyncLoadedAssetList[path]++ ;
}
//return asset;
}
return asset;
}
/// <summary>
/// 异步预加载
/// </summary>
/// <param name="path"></param>
public async UniTask PostPreload<T>(string path) where T : UnityEngine.Object
{
await AssetManager.Instance.PostPreload<T>(path);
if (!AsyncLoadedAssetList.ContainsKey(path))
AsyncLoadedAssetList.Add(path, 1);
else
AsyncLoadedAssetList[path]++;
}
public T GetPreLoadResult<T>(string path) where T : UnityEngine.Object
{
return AssetManager.Instance.GetPreLoadResult<T>(path);
}
protected void UnLoad()
{
2024-05-08 12:44:22 +08:00
foreach (var asset in AsyncLoadedAssetList)
{
2024-05-08 12:44:22 +08:00
var path = asset.Key;
var count = asset.Value;
for (int i = 0; i < count; i++)
{
AssetManager.Instance.Unload(path);
}
}
2024-05-08 12:44:22 +08:00
2024-04-28 15:47:12 +08:00
AsyncLoadedAssetList.Clear();
}
2024-01-26 16:06:42 +08:00
protected virtual void OnUpdate()
{
2024-01-26 16:06:42 +08:00
if (_isNeedScreenAdaption && _lastOrientation != Screen.orientation)
{
// Add your orientation-specific actions here
// Update the last orientation for the next frame
_lastOrientation = Screen.orientation;
UpdateScreenAdaption();
//左右分别找到指定节点移动,当前的方案
//刘海屏适配
}
}
2024-01-26 16:06:42 +08:00
public void Update()
{
OnUpdate();
}
protected virtual void OnUpdate(float dt)
{
if (_isNeedScreenAdaption && _lastOrientation != Screen.orientation)
{
// Add your orientation-specific actions here
// Update the last orientation for the next frame
_lastOrientation = Screen.orientation;
UpdateScreenAdaption();
//左右分别找到指定节点移动,当前的方案
//刘海屏适配
}
}
public void Update(float dt)
{
OnUpdate(dt);
}
2024-01-26 16:06:42 +08:00
#region MonoBehaviour Methods Override
protected void Destroy(GameObject obj)
{
UnityEngine.Object.Destroy(obj);
}
protected void DestroyImmediate(GameObject obj)
{
UnityEngine.Object.DestroyImmediate(obj);
}
protected void Destroy(Component cmp)
{
UnityEngine.Object.Destroy(cmp);
}
protected void DestroyImmediate(Component cmp)
{
UnityEngine.Object.DestroyImmediate(cmp);
}
protected GameObject Instantiate(GameObject prefab)
{
return UnityEngine.Object.Instantiate(prefab, transform);
}
protected GameObject Instantiate(GameObject prefab, Transform trans)
{
return UnityEngine.Object.Instantiate(prefab, trans);
}
#endregion
2024-01-24 15:42:01 +08:00
#region #ScreenAdaption
/// <summary>
/// 屏幕适配,目前方案是左右固定名称的节点下的各个物体需要适配,其他的不管
/// </summary>
protected void UpdateScreenAdaption()
{
2025-03-13 13:25:04 +08:00
// var goLeft = transform.Find("UI_LiuHaiLeft")?.gameObject;
// var goRight = transform.Find("UI_LiuHaiRight")?.gameObject;
if (_goLeft)
{
2025-03-13 13:25:04 +08:00
var rectTrans = _goLeft.GetComponent<RectTransform>();
var originPos = rectTrans.anchoredPosition;
rectTrans.anchoredPosition = new Vector2(_offsets.x, originPos.y);
}
2025-03-13 13:25:04 +08:00
if (_goRight)
{
2025-03-13 13:25:04 +08:00
var rectTrans = _goRight.GetComponent<RectTransform>();
var originPos = rectTrans.anchoredPosition;
rectTrans.anchoredPosition = new Vector2(_offsets.y, originPos.y);
}
}
protected void SetScreenOffsets(Vector2 offset)
{
_offsets = offset;
}
protected void InitScreenUIOffset()
{
var offset = DeviceHelper.GetWidthOffset();
2025-03-13 13:25:04 +08:00
_goLeft = transform.Find("UI_LiuHaiLeft")?.gameObject;
if (_goLeft)
{
2025-03-13 13:25:04 +08:00
var rectTrans = _goLeft.GetComponent<RectTransform>();
var originPos = rectTrans.anchoredPosition;
SetScreenOffsets(new Vector2(originPos.x + offset.x, 0));
}
2025-03-13 13:25:04 +08:00
_goRight = transform.Find("UI_LiuHaiRight")?.gameObject;
if (_goRight)
{
2025-03-13 13:25:04 +08:00
var rectTrans = _goRight.GetComponent<RectTransform>();
var originPos = rectTrans.anchoredPosition;
SetScreenOffsets(new Vector2(_offsets.x, originPos.x - offset.y));
}
}
2024-01-24 15:42:01 +08:00
#endregion
2024-05-09 14:37:29 +08:00
#region Window Life
2024-08-28 15:08:46 +08:00
private void _TryBindBgClose()
{
var trans = transform.Find("BgClose");
if (trans)
{
var btn = trans.GetComponent<Button>();
if (btn)
{
BindButton(btn, () =>
{
var clickAudio = btn.GetComponent<ClickAudio>();
if (clickAudio)
{
AudioManager.Instance.PlayAudio(clickAudio.AudioKey);
}
2024-08-29 14:31:45 +08:00
if (_onBgCloseFunc == null)
{
CloseWindow();
return;
}
2024-08-28 15:08:46 +08:00
_onBgCloseFunc?.Invoke();
});
}
}
}
2025-05-29 18:05:35 +08:00
public void CreateWindow(object data = null, bool isNotSetCamera = false,
UIManager.EShowAnimMode showAnimMode = UIManager.EShowAnimMode.Default)
2023-08-22 19:08:45 +08:00
{
_bDoNotSetMainCamera = isNotSetCamera;
2024-02-22 19:47:07 +08:00
PreLoad(data);
2024-05-10 19:14:47 +08:00
if (!isCreated)
{
OnInit();
2024-09-02 15:26:15 +08:00
RegisterScreenAdaption();
2024-08-28 15:08:46 +08:00
_TryBindBgClose();
2024-05-10 19:14:47 +08:00
isCreated = true;
}
2024-05-16 12:40:15 +08:00
_isShow = false;
_isHide = false;
2024-08-12 19:31:50 +08:00
2025-05-29 18:05:35 +08:00
if (showAnimMode == UIManager.EShowAnimMode.MustShow)
{
IsShowOpenAnim = true;
}
else if (showAnimMode == UIManager.EShowAnimMode.NotShow)
{
IsShowOpenAnim = false;
}
else
{
IsShowOpenAnim = UIManager.Instance.GetIsAutoPlayAnim(WindowName);
}
Show(data);
2024-01-24 15:42:01 +08:00
2023-08-22 19:08:45 +08:00
if (isPlayDefaultOpenAudio)
{
//AudioManager3.Instance.PlaySound("");
}
}
2023-08-22 19:08:45 +08:00
2024-08-12 19:31:50 +08:00
/// <summary>
/// 关闭窗口
/// </summary>
/// <param name="closeType"></param>
/// <param name="forceShowAnim"></param>强制设置动画,0不管1强制开-1强制不开
/// <param name="isOpenMainCamera"></param>
public async void CloseWindow(UIWindowCloseType closeType = UIWindowCloseType.HideAndDestroy, int forceShowAnim = 0,
bool isOpenMainCamera = true)
2023-08-22 19:08:45 +08:00
{
// if (UIManager.Instance.CheckWindowIsFullScreen(this))
// {
// CameraManager.Instance.SetMainCameraActive(isOpenMainCamera);
// }
2025-04-16 12:24:10 +08:00
IsClosing = true;
DebugUtil.LogG($"关闭UI:{WindowName}, closeType{closeType}");
if (UIManager.Instance.CheckWindowIsFullScreen(this))
{
if (!_bDoNotSetMainCamera)
{
CameraManager.Instance.SetMainCameraActive(isOpenMainCamera);
}
}
2024-05-10 19:14:47 +08:00
if (isPlayDefaultCloseAudio && isCreated)
2023-08-22 19:08:45 +08:00
{
//AudioManager3.Instance.PlaySound("");
}
2023-11-21 13:31:06 +08:00
if (closeType == UIWindowCloseType.HideAndDestroy)
2023-08-22 19:08:45 +08:00
{
2024-01-24 15:42:01 +08:00
var last = UIManager.Instance.GetLast();
if (last == this)
{
DebugUtil.LogG($"PopWindow{this.WindowName}");
2024-01-24 15:42:01 +08:00
UIManager.Instance.PopWindow();
}
2024-08-12 19:31:50 +08:00
bool isShowCloseAnim = IsShowOpenAnim;
if (forceShowAnim > 0)
{
isShowCloseAnim = true;
}
if (forceShowAnim < 0)
{
isShowCloseAnim = false;
}
2024-01-24 15:42:01 +08:00
_OnClose();
2024-08-12 19:31:50 +08:00
if (isShowCloseAnim)
{
UIManager.Instance.OnClick(true);
2024-08-12 19:31:50 +08:00
await AnimHide(() =>
{
DestroyWindow();
UIManager.Instance.RemoveWindow(mWindowName);
2025-04-16 12:24:10 +08:00
IsClosing = false;
2024-08-12 19:31:50 +08:00
});
}
else
{
UIManager.Instance.RemoveWindow(mWindowName);
2025-05-16 16:05:52 +08:00
DestroyWindow();
2025-04-16 12:24:10 +08:00
IsClosing = false;
2024-08-12 19:31:50 +08:00
}
2023-08-22 19:08:45 +08:00
}
2024-01-15 15:33:10 +08:00
else if(closeType == UIWindowCloseType.OnlyHide)
{
Hide();
2025-04-16 12:24:10 +08:00
IsClosing = false;
2024-01-15 15:33:10 +08:00
}
2024-01-24 15:42:01 +08:00
else if(closeType == UIWindowCloseType.HideAndPush)
2023-08-22 19:08:45 +08:00
{
Hide();
2025-04-16 12:24:10 +08:00
IsClosing = false;
2024-01-24 15:42:01 +08:00
//UIManager.Instance.PushWindow(this);
}
else
{
//强制清除所有界面时使用
2024-01-26 16:06:42 +08:00
DestroyWindow();
2024-01-24 15:42:01 +08:00
UIManager.Instance.RemoveWindow(mWindowName);
2025-04-16 12:24:10 +08:00
IsClosing = false;
2023-08-22 19:08:45 +08:00
}
}
2023-10-27 16:47:42 +08:00
2024-03-27 17:07:43 +08:00
public virtual async UniTask<bool> OnBack(object data = null)
2023-08-22 19:08:45 +08:00
{
2024-03-27 16:51:46 +08:00
if (UIManager.Instance.GetLast() != null)
{
2024-03-27 16:51:46 +08:00
OnDirectBack();
2024-03-27 17:07:43 +08:00
return true;
}
2024-03-27 16:51:46 +08:00
await OnBackApp();
2024-05-08 14:26:30 +08:00
2024-03-27 17:07:43 +08:00
return true;
2024-03-27 16:51:46 +08:00
}
protected void OnDirectBack()
{
CloseWindow();
}
protected async UniTask OnBackApp()
{
2024-05-16 12:40:15 +08:00
await UIManager.Instance.CreateAndOpenWindow(UINameConst.UIBackReturn);
2023-08-22 19:08:45 +08:00
}
2023-10-19 15:00:19 +08:00
public void ReloadWindow(object data)
{
2024-05-10 19:14:47 +08:00
if (isCreated)
2024-01-15 16:19:09 +08:00
{
OnReloadWindow(data);
return;
}
Show(data);
2023-10-19 15:00:19 +08:00
OnReloadWindow(data);
}
2024-05-10 19:14:47 +08:00
public bool IsCreated()
2023-08-22 19:08:45 +08:00
{
2024-05-10 19:14:47 +08:00
return isCreated;
}
2024-05-16 12:40:15 +08:00
public bool IsShow()
{
return _isShow;
}
public bool IsHide()
{
return _isHide;
}
2024-08-12 19:31:50 +08:00
private async void Show(object data)
{
2024-05-10 19:14:47 +08:00
// var toTrue = (isCreated == false);
// isCreated = true;
2024-12-09 15:56:56 +08:00
//未被唤醒不执行Show
if (IsUnEnabled)
{
return;
}
2023-08-22 19:08:45 +08:00
if (gameObject == null)
{
2023-10-19 15:00:19 +08:00
DebugUtil.Log("UI已被销毁:{0}", WindowName);
2023-08-22 19:08:45 +08:00
}
else
{
2024-08-12 19:31:50 +08:00
if (IsShowOpenAnim)
{
var canvasGroup = gameObject.GetComponent<CanvasGroup>();
if (!canvasGroup)
{
canvasGroup = gameObject.AddComponent<CanvasGroup>();
}
canvasGroup.alpha = 0;
}
2023-08-22 19:08:45 +08:00
gameObject.SetActive(true);
2024-08-12 19:31:50 +08:00
if (IsShowOpenAnim)
{
await AnimShow();
}
2023-08-22 19:08:45 +08:00
}
// if (toTrue)
// {
if (UIManager.Instance.WindowInfos.TryGetValue(WindowName, out var windowInfo))
{
// if (windowInfo.UIOpenType == UIOpenType.FullScreen)
// {
// if (CameraManager.Instance.CheckMainCameraIsActive())
// {
// CameraManager.Instance.SetMainCameraActive(false);
// }
// }
if (CameraManager.Instance.CheckMainCameraIsActive())
{
if (windowInfo.UIOpenType == UIOpenType.FullScreen)
{
//特殊处理主界面
if (!_bDoNotSetMainCamera && WindowName != UINameConst.UI_MainPanel)
{
CameraManager.Instance.SetMainCameraActive(false);
}
}
}
}
OnShowWindow(data);
2024-05-16 12:40:15 +08:00
_isShow = true;
_isHide = false;
//}
2023-08-22 19:08:45 +08:00
}
2023-12-29 15:03:59 +08:00
private void ShowAgain()
2023-08-22 19:08:45 +08:00
{
2024-02-26 18:19:36 +08:00
if (gameObject)
{
gameObject.SetActive(true);
}
//OnShow();
2023-12-29 15:03:59 +08:00
}
private void Hide(bool isOnlyHide = false)
{
gameObject?.SetActive(false);
2024-05-10 19:14:47 +08:00
//isOpen = false;
if (!isOnlyHide)
{
OnHideWindow();
2024-05-16 12:40:15 +08:00
_isHide = true;
_isShow = false;
if (UIManager.Instance.WindowInfos.TryGetValue(WindowName, out var windowInfo))
{
if (windowInfo.UIOpenType == UIOpenType.FullScreen)
{
if (!CameraManager.Instance.CheckMainCameraIsActive())
{
CameraManager.Instance.SetMainCameraActive(true);
}
}
}
}
2024-01-15 15:33:10 +08:00
}
2024-01-24 15:42:01 +08:00
private void _OnClose()
2024-01-15 15:33:10 +08:00
{
2024-01-24 15:42:01 +08:00
if (UIManager.Instance.UiWindowStack.Count <= 0)
{
2024-01-24 15:42:01 +08:00
return;
}
if (UIManager.Instance.CheckWindowIsFullScreen(this))
{
var window = UIManager.Instance.GetLastFullScreen();
2024-01-26 16:06:42 +08:00
if (window != null && window != this)
2024-01-24 15:42:01 +08:00
{
if (UIManager.Instance.CheckIsWindowInStack(this))
{
UIManager.Instance.ForceRemoveWindow(this);
DebugUtil.LogG("UI出栈");
2024-01-24 15:42:01 +08:00
}
}
for (int i = UIManager.Instance.UiWindowStack.Count - 1; i >= 0; i--)
{
var latestWindow = UIManager.Instance.UiWindowStack[i];
if (WindowName == UINameConst.UI_MainPanel || WindowName == UINameConst.UI_PVPDefendPrepare)
{
DebugUtil.LogG("---- UI_PVP Close");
}
if (latestWindow.WindowName == UINameConst.UI_MainPanel)
{
DebugUtil.LogG("----- UI_MainPanel Show!");
}
latestWindow.ShowWindow();
2024-01-24 15:42:01 +08:00
if (UIManager.Instance.CheckWindowIsFullScreen(latestWindow))
break;
}
}
else
{
UIManager.Instance.ForceRemoveWindow(this);
}
2023-08-22 19:08:45 +08:00
}
2024-01-26 16:06:42 +08:00
private void DestroyWindow()
2023-08-22 19:08:45 +08:00
{
DebugUtil.LogG($"DestroyWindow:{WindowName}");
2024-05-10 19:14:47 +08:00
OnHideWindow();
2024-04-29 15:39:46 +08:00
2024-05-10 19:14:47 +08:00
if (isCreated)
2024-04-29 15:39:46 +08:00
{
2024-01-24 15:42:01 +08:00
OnRelease();
2024-05-10 19:14:47 +08:00
isCreated = false;
}
2025-07-01 13:52:07 +08:00
// if (gameObject && UIManager.Instance.UIWindowObjDic.ContainsKey(gameObject))
// {
// UIManager.Instance.UIWindowObjDic.Remove(gameObject);
// }
if (gameObject)
{
if(UIManager.Instance.UIWindowObjDic.ContainsKey(gameObject))
{
UIManager.Instance.UIWindowObjDic.Remove(gameObject);
}
UnityEngine.Object.Destroy(gameObject);
}
AssetManager.Instance.Unload(string.Format(UIManager.UI_PREFAB_PATH, mWindowName), true);
var raycaster = UIRoot.Instance.gameObject.GetComponent<GraphicRaycaster>();
ClearRaycastResults(raycaster);
gameObject = null;
//Resources.UnloadUnusedAssets();
2023-08-22 19:08:45 +08:00
}
2024-05-09 14:37:29 +08:00
#endregion
2024-01-24 15:42:01 +08:00
2024-05-09 14:37:29 +08:00
#region Framework
2024-01-24 15:42:01 +08:00
/// <summary>
///预加载,传入参数,不能添加监听
/// </summary>
public virtual void PreLoad(object data = null)
{
}
2023-12-08 18:41:26 +08:00
2023-08-22 19:08:45 +08:00
/// <summary>
/// 打开界面时调用
/// </summary>
2023-10-19 15:00:19 +08:00
/// <param name="data"></param>
2024-05-28 13:40:44 +08:00
protected virtual void OnShowWindow(object data)
{
EventManager.Instance.Send(EventManager.EventName.UIOpen, WindowName);
}
2024-01-23 12:25:47 +08:00
//protected virtual void OnOpenWindow() { }
2023-08-22 19:08:45 +08:00
/// <summary>
/// 关闭界面时调用
/// </summary>
2024-05-28 13:40:44 +08:00
protected virtual void OnHideWindow()
{
EventManager.Instance.Send(EventManager.EventName.UIClose, WindowName);
}
2024-01-15 15:12:51 +08:00
2023-08-22 19:08:45 +08:00
/// <summary>
/// 重新加载界面时调用
/// </summary>
2023-10-19 15:00:19 +08:00
protected virtual void OnReloadWindow(object data) { }
2023-08-22 19:08:45 +08:00
/// <summary>
/// 私有Awake方法会在基类Awake执行后调用
/// </summary>
2024-01-23 12:25:47 +08:00
public abstract void OnInit();
2023-08-22 19:08:45 +08:00
2024-01-24 15:42:01 +08:00
/// <summary>
/// 对应OnInit在销毁掉界面时调用
/// </summary>
public virtual void OnRelease()
{
2024-10-30 14:49:17 +08:00
EventManager.Instance.Send(EventManager.EventName.UIDestroy, WindowName);
UnLoad();
2024-08-12 19:31:50 +08:00
_openAnimSeq?.Kill();
}
2024-01-24 15:42:01 +08:00
#region #Bind Component
2023-08-22 19:08:45 +08:00
public GameObject BindButton(string target, Action action, bool playAudio = true)
{
GameObject obj = transform.Find(target)?.gameObject;
if (obj != null)
{
Button button = obj.GetComponent<Button>();
if (button != null)
{
button.onClick.AddListener
(
delegate ()
{
if (UIManager.IsClickDisabled)
{
return;
}
UIManager.Instance.OnClick();
2023-08-22 19:08:45 +08:00
if (playAudio)
{
var clickAudio = button.GetComponent<ClickAudio>();
if (clickAudio)
{
AudioManager.Instance.PlayAudio(clickAudio.AudioKey);
}
2023-08-22 19:08:45 +08:00
}
action?.Invoke();
}
);
}
}
else
{
2023-10-19 15:00:19 +08:00
DebugUtil.LogError("未找到 {0}/{1}", gameObject.name, target);
2023-08-22 19:08:45 +08:00
}
return obj;
}
//增加一个可绑定对应变量的点击事件
//传入GameObject
public GameObject BindButton(GameObject obj, Action action, bool playAudio = true)
{
if (obj != null)
{
Button button = obj.GetComponent<Button>();
if (button != null)
{
button.onClick.AddListener
(
delegate ()
{
if (UIManager.IsClickDisabled)
{
return;
}
UIManager.Instance.OnClick();
2023-08-22 19:08:45 +08:00
if (playAudio)
{
var clickAudio = button.GetComponent<ClickAudio>();
if (clickAudio)
{
AudioManager.Instance.PlayAudio(clickAudio.AudioKey);
}
2023-08-22 19:08:45 +08:00
}
action?.Invoke();
}
);
}
}
else
{
2023-10-19 15:00:19 +08:00
DebugUtil.LogError("未找到 {0}/{1}", gameObject.name, obj.name);
2023-08-22 19:08:45 +08:00
}
return obj;
}
//传入参数
public GameObject BindButton<T>(GameObject obj, Action<T> action, T parameter, bool playAudio = true)
{
if (obj != null)
{
Button button = obj.GetComponent<Button>();
if (button != null)
{
button.onClick.AddListener
(
delegate ()
{
if (UIManager.IsClickDisabled)
{
return;
}
UIManager.Instance.OnClick();
2023-08-22 19:08:45 +08:00
if (playAudio)
{
//AudioManager.Instance.PlaySound(SfxNameConst.button_s);
}
action?.Invoke(parameter);
}
);
}
}
else
{
2023-10-19 15:00:19 +08:00
DebugUtil.LogError("未找到 {0}/{1}", gameObject.name, obj.name);
2023-08-22 19:08:45 +08:00
}
return obj;
}
//直接绑按钮
public GameObject BindButton(Button button, Action action, bool playAudio = true)
{
if (button != null)
{
//Button button = button.GetComponent<Button>();
if (button != null)
{
button.onClick.AddListener
(
delegate ()
{
if (UIManager.IsClickDisabled)
{
return;
}
UIManager.Instance.OnClick();
2023-08-22 19:08:45 +08:00
if (playAudio)
{
var clickAudio = button.GetComponent<ClickAudio>();
if (clickAudio)
{
AudioManager.Instance.PlayAudio(clickAudio.AudioKey);
}
2023-08-22 19:08:45 +08:00
//AudioManager.Instance.PlaySound(SfxNameConst.button_s);
}
action?.Invoke();
}
);
}
}
else
{
DebugUtil.LogError("传入按钮为空,请检查按钮绑定参数!!!");
//DebugUtil.LogError($"未找到 {gameObject.name}/{button.name}");
return null;
}
return button.gameObject;
}
//可传入参数
public GameObject BindButton<T>(Button button, Action<T> action, T parameter, bool playAudio = true)
{
if (button != null)
{
//Button button = obj.GetComponent<Button>();
if (button != null)
{
button.onClick.AddListener
(
delegate ()
{
if (UIManager.IsClickDisabled)
{
return;
}
UIManager.Instance.OnClick();
2023-08-22 19:08:45 +08:00
if (playAudio)
{
//AudioManager.Instance.PlaySound(SfxNameConst.button_s);
}
if (parameter == null)
{
DebugUtil.LogError("=== 传入参数为空! ===");
}
action?.Invoke(parameter);
}
);
}
}
else
{
DebugUtil.LogError("传入按钮为空,请检查按钮绑定参数!!!");
return null;
}
return button.gameObject;
}
2023-10-19 15:00:19 +08:00
public void ClearBind(Button button)
{
if (button != null)
{
button.onClick.RemoveAllListeners();
}
}
public void ClearBind(GameObject go)
{
var button = go.GetComponent<Button>();
if (button != null)
{
button.onClick.RemoveAllListeners();
}
}
2023-08-22 19:08:45 +08:00
public GameObject FindObj(string path)
{
GameObject obj = transform.Find(path)?.gameObject;
2023-10-19 15:00:19 +08:00
if (obj == null)
{
DebugUtil.LogError("{0}未找到 {1}/{2}", mWindowName, gameObject.name, path);
}
2023-08-22 19:08:45 +08:00
return obj;
}
2024-01-26 16:06:42 +08:00
public T GetComponent<T>(string path = "") where T : Behaviour
2023-08-22 19:08:45 +08:00
{
2023-10-19 15:00:19 +08:00
var component = transform.Find(path)?.GetComponent<T>();
if (component == null)
{
DebugUtil.LogError("{0}未找到组件{1} {2}", mWindowName, typeof(T).Name, path);
}
return component;
2023-08-22 19:08:45 +08:00
}
public T AddComponent<T>(string path = "") where T : Behaviour
{
var trans = transform.Find(path);
if (trans)
{
var component = trans.GetComponent<T>();
if (component == null)
{
component = trans.gameObject.AddComponent<T>();
//DebugUtil.LogError("{0}未找到组件{1} {2}", mWindowName, typeof(T).Name, path);
}
return component;
}
DebugUtil.LogError("{0}未找到路径: {1}", mWindowName, path);
return null;
}
2024-01-24 15:42:01 +08:00
#endregion
2023-12-29 15:03:59 +08:00
public void HideWindow(bool isOnlyHide = false)
{
Hide(isOnlyHide);
}
2024-05-16 12:40:15 +08:00
public void ShowWindowActive()
2023-08-22 19:08:45 +08:00
{
2023-12-29 15:03:59 +08:00
ShowAgain();
2023-08-22 19:08:45 +08:00
}
2024-05-16 12:40:15 +08:00
public void ShowWindow(object data = null)
{
Show(data);
}
2024-08-28 15:08:46 +08:00
protected void AddBgClose(OnBgClose onBgCloseFunc)
{
_onBgCloseFunc += onBgCloseFunc;
}
protected virtual void OnCloseClick()
{
}
public static void ClearRaycastResults(GraphicRaycaster gRaycaster)
{
if(gRaycaster != null)
{
var fieldInfo = gRaycaster.GetType().GetField("m_RaycastResults", BindingFlags.NonPublic | BindingFlags.IgnoreCase | BindingFlags.Instance);
if (fieldInfo != null)
{
List<Graphic> list = fieldInfo.GetValue(gRaycaster) as List<Graphic>;
if(list != null)
{
list.Clear();
}
}
}
}
2024-05-09 14:37:29 +08:00
#endregion
2024-01-24 15:42:01 +08:00
#region #Anim
2024-08-20 13:58:29 +08:00
public virtual async UniTask PlayAnimation(bool isShow, Action callBack = null)
2023-11-21 13:31:06 +08:00
{
2024-08-12 19:31:50 +08:00
var canvasGroup = gameObject.GetComponent<CanvasGroup>();
if (!canvasGroup)
{
canvasGroup = gameObject.AddComponent<CanvasGroup>();
}
var time = 0.3f;
_isPlayingOpenAnim = true;
_openAnimSeq = DOTween.Sequence();
if (isShow)
{
canvasGroup.alpha = 0;
var tw0 = canvasGroup.DOFade(1, time);
_openAnimSeq.Append(tw0);
_openAnimSeq.AppendCallback(() =>
{
_isPlayingOpenAnim = false;
callBack?.Invoke();
_openAnimSeq.Kill();
_openAnimSeq = null;
2024-08-12 19:31:50 +08:00
});
}
else
2024-08-12 19:31:50 +08:00
{
canvasGroup.alpha = 1;
var tw1 = canvasGroup.DOFade(0, time);
_openAnimSeq.Append(tw1);
_openAnimSeq.AppendCallback(() =>
{
if (!_isCloseCbCalled)
{
_isPlayingOpenAnim = false;
callBack?.Invoke();
_isCloseCbCalled = true;
_openAnimSeq.Kill();
_openAnimSeq = null;
}
});
//保证销毁界面时全都销毁掉
await UniTask.Delay((int)time * 1000 + 10);
if (!_isCloseCbCalled)
{
_isPlayingOpenAnim = false;
callBack?.Invoke();
_openAnimSeq?.Kill();
_openAnimSeq = null;
}
}
2023-11-21 13:31:06 +08:00
}
2024-08-20 13:58:29 +08:00
public virtual async UniTask AnimShow(Action callBack = null)
2023-11-21 15:12:51 +08:00
{
2024-08-12 19:31:50 +08:00
await PlayAnimation(true, callBack);
2023-11-21 13:31:06 +08:00
}
2024-08-20 13:58:29 +08:00
public virtual async UniTask AnimHide(Action callBack = null)
2023-11-21 13:31:06 +08:00
{
2024-08-12 19:31:50 +08:00
await PlayAnimation(false, callBack);
2023-11-21 13:31:06 +08:00
}
2024-01-24 15:42:01 +08:00
#endregion
2023-08-22 19:08:45 +08:00
}