From 09bc892c7283df8757a07b646d5af21ddaa263d1 Mon Sep 17 00:00:00 2001
From: lcy <1459594991@qq.com>
Date: 星期四, 06 十一月 2025 18:22:34 +0800
Subject: [PATCH] 164 天子的考验-客户端
---
Main/System/UIBase/UIBase.cs | 254 +++++++++++++++++++++++++++++++++++---------------
1 files changed, 179 insertions(+), 75 deletions(-)
diff --git a/Main/System/UIBase/UIBase.cs b/Main/System/UIBase/UIBase.cs
index 53420e0..0f76fe6 100644
--- a/Main/System/UIBase/UIBase.cs
+++ b/Main/System/UIBase/UIBase.cs
@@ -39,24 +39,22 @@
// UI鍩烘湰灞炴��
[SerializeField] public UILayer uiLayer = UILayer.Mid;
[SerializeField][HideInInspector] public string uiName;
- [SerializeField] public bool isMainUI = false;
+ [SerializeField] public bool isMainUI = false; //鍚屾椂鍕鹃�塻upportParentChildRelation 浼氬綋浣滄柊鐨勭埗鑺傜偣锛屼絾涓嶄綔涓哄瓙鑺傜偣
- // 鏂板锛氭槸鍚︽敮鎸佺埗瀛愬叧绯�
- // 鐖跺瓙鍏崇郴:UI鎷ユ湁鐨勪笂涓嬬骇閾惧紡鍏崇郴
+ // 鏄惁鏀寔鐖跺瓙鍏崇郴锛屽嵆UI鎷ユ湁鐨勪笂涓嬬骇閾惧紡鍏崇郴
+ // 鎷ユ湁鐖跺瓙鍏崇郴 鍦ㄥ叧闂埗鐣岄潰鐨勬椂鍊� 瀛愮晫闈細杩炲悓涓�璧峰叧闂� 瀛愮晫闈㈡墦寮�鏃�
// 鍦ㄩ潪鐗瑰畾鎯呭喌涓� 閮借鎷ユ湁鐖跺瓙鍏崇郴 锛堜竴鑸潵璇村姛鑳介兘瑕佹湁鐖跺瓙鍏崇郴 渚嬪鐨勬槸渚嬪绯荤粺寮圭獥
- // 涓荤晫闈㈣繖绉嶄笉闇�瑕� 鍦ㄥ埗浣淯I浼� 鐩墠鐨勯渶姹傛槸 鍔熻兘鍏ㄩ儴鍋氬湪Middle灞傦級
- // 鎷ユ湁鐖跺瓙鍏崇郴 鍦ㄥ叧闂埗鐣岄潰鐨勬椂鍊� 瀛愮晫闈細杩炲悓涓�璧峰叧闂� 瀛愮晫闈㈡墦寮�鏃� 鐖剁晫闈㈢殑鈥滃洖鍚堟暟鈥濅細鍒锋柊 鍥炲悎鏁拌瑙佹寔涔呭寲鐩稿叧鐨勬敞閲�
- //
+ // 闄勫姞璇存槑锛氬姛鑳藉熀鏈仛鍦∕iddle灞�
[SerializeField] public bool supportParentChildRelation = true;
// 鎸佷箙鍖栫浉鍏�
[SerializeField] public bool isPersistent = false;
[SerializeField][HideInInspector] public int maxIdleRounds = 20;
+
// 鍔ㄧ敾鐩稿叧
[SerializeField] public UIAnimationType openAnimationType = UIAnimationType.None;
[SerializeField] public UIAnimationType closeAnimationType = UIAnimationType.None;
- [SerializeField] protected RectTransform _rectTransform; //鐣岄潰榛樿娣诲姞鏍硅妭鐐圭敤浜庤〃鐜扮晫闈㈠紑鍚叧闂姩鐢伙紝鎴栬�呰缃�傞厤鐢�
[SerializeField]/*[HideInInspector]*/ public float animeDuration = 0.2f;
[SerializeField]public TweenCurve scaleOverInOutCurve;
@@ -69,15 +67,22 @@
// 瀛怳I绠$悊
[HideInInspector] public List<UIBase> childrenUI = new List<UIBase>();
+ [Header("鎵�鏈塙I鎺掔増搴旇鍦ㄦ鑺傜偣鍐呭眰")]
+ [SerializeField] protected RectTransform _rectTransform; //鐣岄潰榛樿娣诲姞鏍硅妭鐐圭敤浜庤〃鐜扮晫闈㈠紑鍚叧闂姩鐢伙紝鎴栬�呰缃�傞厤鐢�
+
+ //閬僵缁勪欢鍦ㄧ晫闈㈠紑鍙戜腑鐢熸垚锛岄伄缃╁紑鍏冲拰鐐瑰嚮绌虹櫧涓哄叾涓殑缁勪欢鐗规��
// 鎵撳紑閬僵
+ [Header("閬僵(閫忔槑)寮�鍏�")]
[SerializeField] public bool openMask = false;
- // 鐐瑰嚮绌虹櫧鍖哄煙鍏抽棴鐣岄潰
+ // 榛樿鐐瑰嚮绌虹櫧鍖哄煙鍏抽棴鐣岄潰
+ [Header("鐐瑰嚮绌虹櫧鍏抽棴")]
[SerializeField] public bool clickEmptySpaceClose = false;
- private GameObject screenMask = null;
+ public GameObject screenMask = null;
private Button btnClickEmptyClose;
+ public Action btnClickEmptyCloseEvent = null; //鎻愪緵鐐瑰嚮绌虹櫧鍖哄煙鍏抽棴鐣岄潰鐨勫洖璋�
// 璺烵neLevelWin鑱斿姩 瀹為檯涓婃槸闇�瑕佺户鎵胯嚜OneLevelWin鎵嶈兘鐢熸晥鐨勫�� 浣跨敤闇�瑕佹敞鎰�
int m_FunctionOrder = 0;
@@ -100,35 +105,54 @@
protected Vector3 originalPosition;
protected Sequence currentAnimation;
- private CanvasScaler canvasScaler;
+ public CanvasScaler canvasScaler
+ {
+ get;
+ private set;
+ }
+ public const int SafeHeightUp = 50;
+ public const int SafeHeightDown = 30;
#endregion
#region Unity鐢熷懡鍛ㄦ湡
protected virtual void Awake()
{
- // 闃叉鏈変汉涓嶅啓base.InitComponent寮曞彂閿欒 鎵�浠ユ媶鍒�
- InitComponentInternal();
- // 鍦ˋwake涓繘琛屽熀鏈垵濮嬪寲
- InitComponent();
+ try
+ {
+ InitComponentInternal();
+ }
+ catch (Exception e)
+ {
+ Debug.LogError($"{uiName}鐣岄潰鐨処nitComponentInternal鎶ラ敊: {e.StackTrace}");
+ }
+
+ try
+ {
+ InitComponent();
+ }
+ catch (Exception e)
+ {
+ Debug.LogError($"{uiName}鐣岄潰鐨処nitComponent鎶ラ敊: {e.StackTrace}");
+ }
// 淇濆瓨鍘熷鍊肩敤浜庡姩鐢�
if (_rectTransform != null)
{
- originalPosition = _rectTransform.anchoredPosition;;
-
+ if (Screen.height / Screen.width > 1.8)//瀹藉睆闇�瑕侀�傞厤
+ {
+ //涓婁笅鍚勯棿闅擲afeWidth
+ _rectTransform.offsetMax = new Vector2(0, -SafeHeightUp); //涓�
+ _rectTransform.offsetMin = new Vector2(0, SafeHeightDown); //涓�
+ }
+ originalPosition = _rectTransform.anchoredPosition;
}
- ApplySettings();
-
- if (openMask)
- {
- screenMask = GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/ScreenMask"), transform);
- screenMask.transform.localScale = Vector3.one;
- screenMask.transform.localPosition = Vector3.zero;
+ if (screenMask != null)
screenMask.transform.SetAsFirstSibling();
- }
+ InitClickEmptySpaceBtn();
+
}
protected virtual void Start()
@@ -136,20 +160,38 @@
// 瀛愮被鍙互閲嶅啓姝ゆ柟娉曡繘琛岄澶栧垵濮嬪寲
}
- protected async UniTask ApplySettings()
+ protected async UniTask ApplyClickEmptySpaceClose()
{
if (clickEmptySpaceClose)
{
- //寤惰繜鍒涘缓浼氬鑷村眰绾у湪ScreenMask涔嬩笂
- GameObject goBtnESC = GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/ClickEmptyCloseMask"), transform);
- btnClickEmptyClose = goBtnESC.GetComponent<Button>();
- btnClickEmptyClose.AddListener(CloseWindow);
- btnClickEmptyClose.transform.SetAsFirstSibling();
- await UniTask.DelayFrame(5);
-
- btnClickEmptyClose = goBtnESC.GetComponent<Button>();
- btnClickEmptyClose.AddListener(CloseWindow);
+ //寤惰繜x甯у悗鍙偣鍑�,闃叉鐐瑰嚮杩囧揩绔嬪嵆鍏抽棴浜�
+ await UniTask.Delay(200);
+ btnClickEmptyClose.enabled = true;
}
+ }
+
+ private void InitClickEmptySpaceBtn()
+ {
+ if (!clickEmptySpaceClose)
+ {
+ return;
+ }
+
+ btnClickEmptyClose = screenMask.GetComponent<Button>();
+
+ btnClickEmptyClose.AddListener(() =>
+ {
+ if (btnClickEmptyCloseEvent != null)
+ {
+ btnClickEmptyCloseEvent();
+ }
+ else
+ {
+ CloseWindow();
+ }
+ });
+
+ btnClickEmptyClose.enabled = false;
}
@@ -185,6 +227,7 @@
// 璁剧疆Canvas灞炴��
canvas.overrideSorting = true;
canvas.worldCamera = CameraManager.uiCamera;
+ canvas.pixelPerfect = false;
canvas.sortingLayerID = SortingLayer.NameToID("UI"); // 纭繚浣跨敤姝g‘鐨勬帓搴忓眰
@@ -241,26 +284,55 @@
return;
}
- OnPreOpen();
- // 濡傛灉姝e湪鎾斁鍔ㄧ敾锛屽厛鍋滄
+ if ((clickEmptySpaceClose || openMask) && screenMask == null)
+ {
+ Debug.LogError($"鐣岄潰: {uiName} 缂哄皯閬僵 ");
+ return;
+ }
+
+ try
+ {
+ OnPreOpen();
+ }
+ catch (Exception e)
+ {
+ Debug.LogError($"{uiName}鐣岄潰鐨凮nPreOpen鎶ラ敊: {e.StackTrace}");
+ }
+
StopCurrentAnimation();
- // 閲嶇疆鍏抽棴鏍囪
isClosing = false;
-
gameObject.SetActive(true);
isActive = true;
- // 鏍规嵁鍔ㄧ敾绫诲瀷鎾斁鎵撳紑鍔ㄧ敾
PlayOpenAnimation();
-
+
// // 濡傛灉鍚庣画闇�瑕佺粺涓�澶勭悊鍒樻捣鎴栬�呭皬娓告垙鐨勭晫闈㈤�傞厤闂
// _rectTransform.offsetMin = new Vector2(0, 10); //涓嬫柟
// _rectTransform.offsetMax = new Vector2(0, -50); //涓婃柟
- OnOpen();
+ try
+ {
+ OnOpen();
+ }
+ catch (Exception e)
+ {
+ Debug.LogError($"{uiName}鐣岄潰鐨凮nOpen鎶ラ敊: {e.StackTrace}");
+ }
- ExecuteNextFrame(NextFrameAfterOpen);
+ ApplyClickEmptySpaceClose();
+
+ ExecuteNextFrame(() =>
+ {
+ try
+ {
+ NextFrameAfterOpen();
+ }
+ catch (Exception e)
+ {
+ Debug.LogError($"{uiName}鐣岄潰鐨凬extFrameAfterOpen鎶ラ敊: {e.StackTrace}");
+ }
+ });
}
protected virtual void NextFrameAfterOpen()
@@ -273,32 +345,50 @@
{
// 濡傛灉宸茬粡鍦ㄥ叧闂繃绋嬩腑锛岀洿鎺ヨ繑鍥�
if (isClosing) return;
-
- OnPreClose();
-
- // 濡傛灉姝e湪鎾斁鍔ㄧ敾锛屽厛鍋滄
+
+ if (clickEmptySpaceClose)
+ btnClickEmptyClose.enabled = false;
+
+ try
+ {
+ OnPreClose();
+ }
+ catch (Exception e)
+ {
+ Debug.LogError($"{uiName}鐣岄潰鐨凮nPreClose鎶ラ敊: {e.StackTrace}");
+ }
+
StopCurrentAnimation();
- // 璁剧疆鍏抽棴鏍囪
isClosing = true;
isActive = false;
-
- // 绂佺敤浜や簰浣嗕繚鎸佸彲瑙�
+
if (canvasGroup != null)
{
canvasGroup.blocksRaycasts = false;
}
- // 鏍规嵁鍔ㄧ敾绫诲瀷鎾斁鍏抽棴鍔ㄧ敾
PlayCloseAnimation();
- // 璋冪敤鍏抽棴鍥炶皟
- OnClose();
-
- // 濡傛灉娌℃湁鍏抽棴鍔ㄧ敾锛岀洿鎺ョ鐢ㄦ父鎴忓璞�
+ try
+ {
+ OnClose();
+ }
+ catch (Exception e)
+ {
+ Debug.LogError($"{uiName}鐣岄潰鐨凮nClose鎶ラ敊: {e.StackTrace}");
+ }
+
if (closeAnimationType == UIAnimationType.None)
{
- CompleteClose();
+ try
+ {
+ CompleteClose();
+ }
+ catch (Exception e)
+ {
+ Debug.LogError($"{uiName}鐣岄潰鐨凜ompleteClose鎶ラ敊: {e.StackTrace}");
+ }
}
// 鍚﹀垯鍦ㄥ姩鐢诲畬鎴愬悗绂佺敤娓告垙瀵硅薄锛堝湪PlayCloseAnimation涓鐞嗭級
}
@@ -313,6 +403,12 @@
public virtual void CloseWindow()
{
UIManager.Instance.CloseWindow(this, false);
+ }
+
+ public async UniTask DelayCloseWindow(int delayTime = 30)
+ {
+ await UniTask.Delay(delayTime);
+ CloseWindow();
}
// 鍒锋柊UI
@@ -344,24 +440,25 @@
/// <summary>
/// 鎾斁UI鐗规晥
/// </summary>
- /// <param name="effectName">鐗规晥璧勬簮鍚嶇О</param>
+ /// <param name="id">鐗规晥璧勬簮鍚嶇О</param>
/// <param name="parent">鐗规晥鐖惰妭鐐癸紝榛樿涓哄綋鍓峌I</param>
- /// <param name="autoDestroy">鏄惁鑷姩閿�姣侊紝榛樿涓簍rue</param>
- /// <param name="destroyDelay">鑷姩閿�姣佸欢杩熸椂闂达紝榛樿涓�5绉�</param>
/// <returns>鐗规晥娓告垙瀵硅薄</returns>
- public EffectPlayer PlayUIEffect(int id, Transform parent = null, bool autoDestroy = true, float destroyDelay = 5f)
+ public UIEffectPlayer PlayUIEffect(int id, Transform parent = null)
{
// 浣跨敤榛樿鍊�
if (parent == null) parent = transform;
-
- EffectPlayer player = parent.gameObject.AddComponent<EffectPlayer>();
- player.effectId = id;
- player.autoDestroy = autoDestroy;
- player.destroyDelay = destroyDelay;
- player.canvas = canvas;
+ return UIEffectPlayer.CreateEffect(id, parent, false);
+ }
- return player;
+ public int GetSortingOrder()
+ {
+ if (null != canvas)
+ {
+ return canvas.sortingOrder;
+ }
+
+ return 0;
}
#endregion
@@ -393,7 +490,8 @@
// 鎾斁鎵撳紑鍔ㄧ敾
protected virtual void PlayOpenAnimation()
{
-
+ //绂佺敤浜や簰浼氬紩璧风偣閫忛棶棰�, 鍚庣画瑙e喅鍙互鑰冭檻EventSystem
+ canvasGroup.blocksRaycasts = true;
if (openAnimationType == UIAnimationType.None)
{
@@ -412,7 +510,7 @@
if (canvasGroup != null)
{
canvasGroup.alpha = 0f;
- canvasGroup.blocksRaycasts = false;
+ // canvasGroup.blocksRaycasts = false;
}
if (_rectTransform != null)
{
@@ -424,7 +522,7 @@
if (canvasGroup != null)
{
canvasGroup.alpha = 1f;
- canvasGroup.blocksRaycasts = false;
+ // canvasGroup.blocksRaycasts = false;
}
if (_rectTransform != null)
{
@@ -475,7 +573,8 @@
if (canvasGroup != null)
{
canvasGroup.alpha = 1f;
- canvasGroup.blocksRaycasts = false;
+ // 绂佺敤浜や簰浼氬紩璧风偣閫忛棶棰�
+ // canvasGroup.blocksRaycasts = false;
}
if (_rectTransform != null)
{
@@ -556,7 +655,7 @@
}
catch (System.Exception e)
{
- Debug.LogError($"鎾斁鎵撳紑鍔ㄧ敾鏃跺嚭閿�: {e.Message}");
+ Debug.LogError($"鎾斁鎵撳紑鍔ㄧ敾鏃跺嚭閿�: {e.StackTrace}");
// 鍑洪敊鏃剁‘淇漊I鍙骞跺彲浜や簰
if (canvasGroup != null)
@@ -671,11 +770,16 @@
currentAnimation.OnComplete(() =>
{
isAnimating = false;
-
- // 鍔ㄧ敾瀹屾垚鍚庯紝瀹屾垚鍏抽棴杩囩▼
if (isClosing)
{
- CompleteClose();
+ try
+ {
+ CompleteClose();
+ }
+ catch (Exception e)
+ {
+ Debug.LogError($"{uiName}鐣岄潰鐨凜ompleteClose鎶ラ敊: {e.StackTrace}");
+ }
}
});
@@ -683,7 +787,7 @@
}
catch (System.Exception e)
{
- Debug.LogError($"鎾斁鍏抽棴鍔ㄧ敾鏃跺嚭閿�: {e.Message}");
+ Debug.LogError($"鎾斁鍏抽棴鍔ㄧ敾鏃跺嚭閿�: {e.StackTrace}");
// 鍑洪敊鏃剁洿鎺ュ畬鎴愬叧闂�
isAnimating = false;
--
Gitblit v1.8.0