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

593 lines
15 KiB
C#
Raw Normal View History

2023-08-22 19:08:45 +08:00
using System;
2023-11-21 13:31:06 +08:00
using Cysharp.Threading.Tasks;
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-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-01-26 16:06:42 +08:00
private bool _isNeedScreenAdaption;
2023-08-22 19:08:45 +08:00
// 是否播放默认的打开对话框的声音
public bool isPlayDefaultOpenAudio = true;
// 是否播放默认的关闭对话框的声音
public bool isPlayDefaultCloseAudio = true;
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]
private bool isOpen = false;
2023-08-22 19:08:45 +08:00
2024-01-24 15:42:01 +08:00
private bool _isHideAndPush = false;
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-01-26 16:06:42 +08:00
protected virtual void OnUpdate()
{
2024-01-26 16:06:42 +08:00
if (_isNeedScreenAdaption && _lastOrientation != Screen.orientation)
{
//Debug.Log("Screen orientation has changed!");
// 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();
}
#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()
{
2023-12-29 18:22:22 +08:00
var goLeft = transform.Find("UI_LiuHaiLeft")?.gameObject;
var goRight = transform.Find("UI_LiuHaiRight")?.gameObject;
if (goLeft)
{
var rectTrans = goLeft.GetComponent<RectTransform>();
var originPos = rectTrans.anchoredPosition;
rectTrans.anchoredPosition = new Vector2(_offsets.x, originPos.y);
}
if (goRight)
{
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();
2023-12-29 18:22:22 +08:00
var goLeft = transform.Find("UI_LiuHaiLeft")?.gameObject;
if (goLeft)
{
var rectTrans = goLeft.GetComponent<RectTransform>();
var originPos = rectTrans.anchoredPosition;
SetScreenOffsets(new Vector2(originPos.x + offset.x, 0));
}
2023-12-29 18:22:22 +08:00
GameObject goRight = transform.Find("UI_LiuHaiRight")?.gameObject;
if (goRight)
{
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-01-24 15:42:01 +08:00
public void OpenWindow(object data = null)
2023-08-22 19:08:45 +08:00
{
2024-02-22 19:47:07 +08:00
PreLoad(data);
if (!isOpen)
{
OnInit();
}
Show(data);
2024-01-24 15:42:01 +08:00
2023-08-22 19:08:45 +08:00
if (isPlayDefaultOpenAudio)
{
//AudioManager3.Instance.PlaySound("");
}
}
2024-01-15 15:33:10 +08:00
public void CloseWindow(UIWindowCloseType closeType = UIWindowCloseType.HideAndDestroy)
2023-08-22 19:08:45 +08:00
{
2023-08-22 19:08:45 +08:00
if (isPlayDefaultCloseAudio && isOpen)
{
//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)
{
UIManager.Instance.PopWindow();
}
_OnClose();
2024-01-26 16:06:42 +08:00
DestroyWindow();
2023-11-21 13:31:06 +08:00
UIManager.Instance.RemoveWindow(mWindowName);
2023-08-22 19:08:45 +08:00
}
2024-01-15 15:33:10 +08:00
else if(closeType == UIWindowCloseType.OnlyHide)
{
Hide();
}
2024-01-24 15:42:01 +08:00
else if(closeType == UIWindowCloseType.HideAndPush)
2023-08-22 19:08:45 +08:00
{
Hide();
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);
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-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()
{
await UIManager.Instance.OpenWindow(UINameConst.UIBackReturn);
2023-08-22 19:08:45 +08:00
}
2023-10-19 15:00:19 +08:00
public void ReloadWindow(object data)
{
2024-01-15 16:19:09 +08:00
if (isOpen)
{
OnReloadWindow(data);
return;
}
Show(data);
2023-10-19 15:00:19 +08:00
OnReloadWindow(data);
}
public bool IsOpen()
2023-08-22 19:08:45 +08:00
{
return isOpen;
}
private void Show(object data)
{
var toTrue = (isOpen == false);
2023-08-22 19:08:45 +08:00
isOpen = true;
_isHideAndPush = false;
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
{
gameObject.SetActive(true);
}
// if (toTrue)
// {
OnShowWindow(data);
//}
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)
{
2024-01-15 15:33:10 +08:00
gameObject.SetActive(false);
if (!isOnlyHide)
{
OnHideWindow();
_isHideAndPush = 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);
}
}
for (int i = UIManager.Instance.UiWindowStack.Count - 1; i >= 0; i--)
{
var latestWindow = UIManager.Instance.UiWindowStack[i];
latestWindow.OpenWindow();
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
{
if (isOpen)
{
OnHideWindow();
2024-01-24 15:42:01 +08:00
OnRelease();
}
2023-08-22 19:08:45 +08:00
isOpen = false;
2024-01-26 16:06:42 +08:00
UnityEngine.Object.Destroy(gameObject);
2023-10-19 15:00:19 +08:00
AssetManager.Instance.Unload(string.Format(UIManager.UI_PREFAB_PATH, mWindowName));
2023-08-22 19:08:45 +08:00
}
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>
protected virtual void OnShowWindow(object data) { }
2024-01-23 12:25:47 +08:00
//protected virtual void OnOpenWindow() { }
2023-08-22 19:08:45 +08:00
/// <summary>
/// 关闭界面时调用
/// </summary>
protected virtual void OnHideWindow() { }
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(){}
#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 (playAudio)
{
//AudioManager.Instance.PlaySound(SfxNameConst.button_s);
}
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 (playAudio)
{
//AudioManager.Instance.PlaySound(SfxNameConst.button_s);
}
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 (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 (playAudio)
{
//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 (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
}
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);
}
public void ShowWindow()
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-01-24 15:42:01 +08:00
#region #Anim
2023-11-21 15:12:51 +08:00
public async UniTask PlayAnimation(string name)
2023-11-21 13:31:06 +08:00
{
}
2023-11-21 15:12:51 +08:00
public async UniTask AnimShow()
{
await PlayAnimation(DEFAULT_SHOW_ANIM);
2023-11-21 13:31:06 +08:00
}
2023-11-21 15:12:51 +08:00
public async UniTask AnimHide()
2023-11-21 13:31:06 +08:00
{
2023-11-21 15:12:51 +08:00
await PlayAnimation(DEFAULT_HIDE_ANIM);
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
}