using System; using System.Collections.Generic; using Cysharp.Threading.Tasks; using DG.Tweening; using Framework; using PhxhSDK; using UnityEngine; using UnityEngine.Events; using UnityEngine.UI; using UnityEngine.Serialization; using Object = System.Object; using UniTask = Cysharp.Threading.Tasks.UniTask; public abstract class UIWindow { public GameObject gameObject; public Transform transform; public RectTransform rectTransform; public string name; 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 delegate void OnBgClose(); private OnBgClose _onBgCloseFunc; protected Dictionary AsyncLoadedAssetList = new Dictionary(10); private bool _isNeedScreenAdaption; private bool _bDoNotSetMainCamera = false; //不设置主相机 private bool _isCloseCbCalled; private GameObject _goLeft; private GameObject _goRight; public bool IsUnEnabled; //是否不被激活 public bool IsNotSetMainCamera { get { return _bDoNotSetMainCamera; } set { _bDoNotSetMainCamera = value; } } // 是否播放默认的打开对话框的声音 public bool isPlayDefaultOpenAudio = true; // 是否播放默认的关闭对话框的声音 public bool isPlayDefaultCloseAudio = true; private bool _isPlayingOpenAnim; protected Sequence _openAnimSeq; public bool IsShowOpenAnim; public string WindowName { set { mWindowName = value; } get { return mWindowName; } } [HideInInspector] public UIWindowLayer uiWindowLayer; [HideInInspector] protected bool isCreated = false; protected bool _isShow = false; protected bool _isHide = false; protected void RegisterScreenAdaption() { _lastOrientation = Screen.orientation; InitScreenUIOffset(); UpdateScreenAdaption(); _isNeedScreenAdaption = true; } public async UniTask LoadAssetAsync(string path) where T : UnityEngine.Object { var asset = await AssetManager.Instance.LoadAssetAsync(path); if (gameObject) { if (!AsyncLoadedAssetList.ContainsKey(path)) { AsyncLoadedAssetList.Add(path, 1); } else { AsyncLoadedAssetList[path]++ ; } //return asset; } return asset; } /// /// 异步预加载 /// /// public async UniTask PostPreload(string path) where T : UnityEngine.Object { await AssetManager.Instance.PostPreload(path); if (!AsyncLoadedAssetList.ContainsKey(path)) AsyncLoadedAssetList.Add(path, 1); else AsyncLoadedAssetList[path]++; } public T GetPreLoadResult(string path) where T : UnityEngine.Object { return AssetManager.Instance.GetPreLoadResult(path); } protected void UnLoad() { foreach (var asset in AsyncLoadedAssetList) { var path = asset.Key; var count = asset.Value; for (int i = 0; i < count; i++) { AssetManager.Instance.Unload(path); } } AsyncLoadedAssetList.Clear(); } protected virtual void OnUpdate() { 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() { 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); } #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 #region #ScreenAdaption /// /// 屏幕适配,目前方案是左右固定名称的节点下的各个物体需要适配,其他的不管 /// protected void UpdateScreenAdaption() { // var goLeft = transform.Find("UI_LiuHaiLeft")?.gameObject; // var goRight = transform.Find("UI_LiuHaiRight")?.gameObject; 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(); _goLeft = transform.Find("UI_LiuHaiLeft")?.gameObject; if (_goLeft) { var rectTrans = _goLeft.GetComponent(); var originPos = rectTrans.anchoredPosition; SetScreenOffsets(new Vector2(originPos.x + offset.x, 0)); } _goRight = transform.Find("UI_LiuHaiRight")?.gameObject; if (_goRight) { var rectTrans = _goRight.GetComponent(); var originPos = rectTrans.anchoredPosition; SetScreenOffsets(new Vector2(_offsets.x, originPos.x - offset.y)); } } #endregion #region Window Life private void _TryBindBgClose() { var trans = transform.Find("BgClose"); if (trans) { var btn = trans.GetComponent