using System; using Cysharp.Threading.Tasks; using Framework; using PhxhSDK; using UnityEngine; using UnityEngine.Events; using UnityEngine.UI; using UnityEngine.Serialization; using Object = System.Object; public abstract class UIWindow : MonoBehaviour { private const string DEFAULT_SHOW_ANIM = "show"; private const string DEFAULT_HIDE_ANIM = "hide"; private string mWindowName; private ScreenOrientation _lastOrientation; private Vector2 _offsets; //x,y代表左右的偏移量 // 是否播放默认的打开对话框的声音 public bool isPlayDefaultOpenAudio = true; // 是否播放默认的关闭对话框的声音 public bool isPlayDefaultCloseAudio = true; public string WindowName { set { mWindowName = value; } get { return mWindowName; } } [HideInInspector] public UIWindowLayer uiWindowLayer; [HideInInspector] private bool isOpen = false; void Awake() { OnAwake(); } protected void OnStart() { _lastOrientation = Screen.orientation; InitScreenUIOffset(); UpdateScreenAdaption(); } protected void OnUpdate() { if (_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(); //左右分别找到指定节点移动,当前的方案 //刘海屏适配 } } /// /// 屏幕适配,目前方案是左右固定名称的节点下的各个物体需要适配,其他的不管 /// protected void UpdateScreenAdaption() { var goLeft = FindObj("UI_LiuHaiLeft"); var goRight = FindObj("UI_LiuHaiRight"); if (goLeft) { var rectTrans = goLeft.GetComponent(); var originPos = rectTrans.anchoredPosition; rectTrans.anchoredPosition = new Vector2(_offsets.x, originPos.y); } if (goRight) { var rectTrans = goRight.GetComponent(); 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(); var goLeft = FindObj("UI_LiuHaiLeft"); if (goLeft) { var rectTrans = goLeft.GetComponent(); var originPos = rectTrans.anchoredPosition; SetScreenOffsets(new Vector2(originPos.x + offset.x, 0)); } var goRight = FindObj("UI_LiuHaiRight"); if (goRight) { var rectTrans = goRight.GetComponent(); var originPos = rectTrans.anchoredPosition; SetScreenOffsets(new Vector2(_offsets.x, originPos.x - offset.y)); } } public void OpenWindow(object data) { Show(data); if (isPlayDefaultOpenAudio) { //AudioManager3.Instance.PlaySound(""); } } public void CloseWindow(UIWindowCloseType closeType = UIWindowCloseType.OnlyHide) { if (isPlayDefaultCloseAudio && isOpen) { //AudioManager3.Instance.PlaySound(""); } if (closeType == UIWindowCloseType.HideAndDestroy) { Destroy(); UIManager.Instance.RemoveWindow(mWindowName); } else { Hide(); if (closeType == UIWindowCloseType.HideAndPush) { UIManager.Instance.PushWindow(this); } } } public virtual bool OnBack() { //AudioManager.Instance.PlaySound(SfxNameConst.button_s); CloseWindow(UIWindowCloseType.OnlyHide); return true; } public void ReloadWindow() { Show(null); OnReloadWindow(); } public void ReloadWindow(object data) { Show(data); OnReloadWindow(data); } public bool IsOpen() { return isOpen; } private void Show(object data) { var toTrue = (isOpen == false); isOpen = true; if (gameObject == null) { DebugUtil.Log("UI已被销毁:{0}", WindowName); } else { gameObject.SetActive(true); } if (toTrue) { if (data == null) { OnOpenWindow(); } else { OnOpenWindow(data); } } } private void Hide() { if (isOpen) { OnCloseWindow(); } isOpen = false; gameObject.SetActive(false); } private void Destroy() { if (isOpen) { OnCloseWindow(); } isOpen = false; Destroy(gameObject); AssetManager.Instance.Unload(string.Format(UIManager.UI_PREFAB_PATH, mWindowName)); } /// /// 打开界面时调用 /// /// protected virtual void OnOpenWindow(object data) { } protected virtual void OnOpenWindow() { } /// /// 关闭界面时调用 /// protected virtual void OnCloseWindow() { } /// /// 重新加载界面时调用 /// protected virtual void OnReloadWindow(object data) { } protected virtual void OnReloadWindow() { } /// /// 私有Awake方法,会在基类Awake执行后调用 /// public abstract void OnAwake(); public GameObject BindButton(string target, Action action, bool playAudio = true) { GameObject obj = transform.Find(target)?.gameObject; if (obj != null) { Button button = obj.GetComponent