From bb917a44e530f58edfe35ecc73a17ffd00b2f3ef Mon Sep 17 00:00:00 2001
From: yyl <yyl>
Date: 星期二, 10 六月 2025 19:04:59 +0800
Subject: [PATCH] 18 子 2D卡牌客户端搭建 / 2D卡牌客户端搭建 UI动画审查跟问题修复
---
Main/System/UIBase/UIBase.cs | 192 +++++++++++++++++++++++++----------------------
Main/Manager/UIManager.cs | 11 +-
Main/System/Main/MainWin.cs | 4
3 files changed, 110 insertions(+), 97 deletions(-)
diff --git a/Main/Manager/UIManager.cs b/Main/Manager/UIManager.cs
index 71cc381..6ef3fd8 100644
--- a/Main/Manager/UIManager.cs
+++ b/Main/Manager/UIManager.cs
@@ -3,6 +3,7 @@
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
+using DG.Tweening;
/// <summary>
/// UI绠$悊鍣� - 璐熻矗绠$悊鎵�鏈塙I鐣岄潰鐨勬樉绀恒�侀殣钘忓拰灞傜骇
@@ -64,23 +65,25 @@
#endregion
#region 鍒濆鍖�
-
+
/// <summary>
/// 鍒濆鍖朥I绠$悊鍣�
/// </summary>
public override void Init()
{
base.Init();
-
+
// 鍒濆鍖朥I鏍硅妭鐐�
InitUIRoot();
-
+
// 鍒濆鍖栫紦瀛�
layerSortingOrderCache.Clear();
layerTransformCache.Clear();
uiInstanceCounter.Clear();
-
+
+ DOTween.SetTweensCapacity(500, 50);
Debug.Log("UI绠$悊鍣ㄥ垵濮嬪寲瀹屾垚");
+
}
/// <summary>
diff --git a/Main/System/Main/MainWin.cs b/Main/System/Main/MainWin.cs
index d89f3e8..262f519 100644
--- a/Main/System/Main/MainWin.cs
+++ b/Main/System/Main/MainWin.cs
@@ -28,13 +28,13 @@
{
base.InitComponent();
- windowBackground = transform.Find("RawImgBackground").gameObject;
+ windowBackground = _rectTransform.Find("RawImgBackground").gameObject;
bottomTabButtons = new Button[5];
for (int i = 1; i <= 5; i++)
{
string buttonName = "Buttons/Button" + i;
- bottomTabButtons[i-1] = transform.Find(buttonName).GetComponent<Button>();
+ bottomTabButtons[i-1] = _rectTransform.Find(buttonName).GetComponent<Button>();
#if UNITY_EDITOR
//娴嬭瘯浠g爜
#endif
diff --git a/Main/System/UIBase/UIBase.cs b/Main/System/UIBase/UIBase.cs
index f507f0e..42d62ae 100644
--- a/Main/System/UIBase/UIBase.cs
+++ b/Main/System/UIBase/UIBase.cs
@@ -45,7 +45,7 @@
[SerializeField][HideInInspector] public int maxIdleRounds = 20;
// 鍔ㄧ敾鐩稿叧
- [SerializeField] public UIAnimationType openAnimationType = UIAnimationType.ScaleInOut;
+ [SerializeField] public UIAnimationType openAnimationType = UIAnimationType.None;
[SerializeField] public UIAnimationType closeAnimationType = UIAnimationType.None;
[SerializeField]/*[HideInInspector]*/ public float animeDuration = 0.2f;
[SerializeField][HideInInspector] public Ease animationEase = Ease.OutQuad; // 纭繚浣跨敤 DG.Tweening.Ease
@@ -53,6 +53,8 @@
// 杩愯鏃剁姸鎬�
[HideInInspector] public int lastUsedRound = 0;
[HideInInspector] public UIBase parentUI;
+
+ [HideInInspector] public GameObject rootNode; // 鏍硅妭鐐�
// 瀛怳I绠$悊
[HideInInspector] public List<UIBase> childrenUI = new List<UIBase>();
@@ -77,10 +79,9 @@
// 缁勪欢寮曠敤
protected Canvas canvas;
protected CanvasGroup canvasGroup;
- protected RectTransform rectTransform;
+ protected RectTransform _rectTransform;
// 鍔ㄧ敾鐩稿叧
- protected Vector3 originalScale;
protected Vector3 originalPosition;
protected Sequence currentAnimation;
@@ -92,19 +93,16 @@
protected virtual void Awake()
{
- // 纭繚 DOTween 宸插垵濮嬪寲
- DOTween.SetTweensCapacity(500, 50);
-
+ CreateRootNode();
// 闃叉鏈変汉涓嶅啓base.InitComponent寮曞彂閿欒 鎵�浠ユ媶鍒�
InitComponentInternal();
// 鍦ˋwake涓繘琛屽熀鏈垵濮嬪寲
InitComponent();
// 淇濆瓨鍘熷鍊肩敤浜庡姩鐢�
- if (rectTransform != null)
+ if (_rectTransform != null)
{
- originalScale = rectTransform.localScale;
- originalPosition = rectTransform.anchoredPosition;
+ originalPosition = _rectTransform.anchoredPosition;
}
ApplySettings();
@@ -115,6 +113,31 @@
screenMask.transform.localScale = Vector3.one;
screenMask.transform.localPosition = Vector3.zero;
screenMask.transform.SetAsFirstSibling();
+ }
+ }
+
+ private void CreateRootNode()
+ {
+ List<Transform> children = new List<Transform>();
+ foreach (Transform child in transform)
+ {
+ children.Add(child);
+ }
+
+ rootNode = new GameObject("WindowRoot");
+ rootNode.transform.SetParent(transform, false);
+ rootNode.layer = LayerMask.NameToLayer("UI");
+ _rectTransform = rootNode.AddMissingComponent<RectTransform>();
+ _rectTransform.anchorMin = new Vector2(0.5f, 0.5f);
+ _rectTransform.anchorMax = new Vector2(0.5f, 0.5f);
+ _rectTransform.pivot = new Vector2(0.5f, 0.5f);
+ _rectTransform.sizeDelta = new Vector2(750, 1334); // 榛樿澶у皬锛屽彲浠ユ牴鎹渶瑕佽皟鏁�
+ _rectTransform.anchoredPosition = Vector2.zero;
+ _rectTransform.localScale = Vector3.one;
+
+ foreach (Transform child in children)
+ {
+ child.SetParent(rootNode.transform, false);
}
}
@@ -188,9 +211,6 @@
}
canvasScaler = GetComponent<CanvasScaler>();
-
- // 鑾峰彇RectTransform缁勪欢
- rectTransform = GetComponent<RectTransform>();
}
// 鑾峰彇蹇呰鐨勭粍浠�
@@ -485,15 +505,13 @@
// 鎾斁鎵撳紑鍔ㄧ敾
protected virtual void PlayOpenAnimation()
{
+
+
if (openAnimationType == UIAnimationType.None)
{
// 鏃犲姩鐢伙紝鐩存帴鍚敤浜や簰
- if (canvasGroup != null)
- {
- canvasGroup.alpha = 1f;
- canvasGroup.interactable = true;
- canvasGroup.blocksRaycasts = true;
- }
+ // 鍒濆鍊艰瀹氬ソ
+ _ResetToBegin();
return;
}
@@ -509,74 +527,62 @@
canvasGroup.interactable = false;
canvasGroup.blocksRaycasts = false;
}
+ if (canvasScaler != null)
+ {
+ canvasScaler.scaleFactor = 1f;
+ }
break;
case UIAnimationType.ScaleInOut:
- if (rectTransform != null)
+ if (canvasGroup != null)
{
- canvasScaler.scaleFactor = 0.3f;
+ canvasGroup.alpha = 1f;
canvasGroup.interactable = false;
canvasGroup.blocksRaycasts = false;
+ }
+ if (canvasScaler != null)
+ {
+ canvasScaler.scaleFactor = 0.3f;
}
break;
case UIAnimationType.SlideFromTop:
- if (rectTransform != null)
+ _ResetToBegin();
+ if (_rectTransform != null)
{
Vector2 startPos = originalPosition;
- startPos.y = Screen.height;
- rectTransform.anchoredPosition = startPos;
- }
- if (canvasGroup != null)
- {
- canvasGroup.alpha = 0f;
- canvasGroup.interactable = false;
- canvasGroup.blocksRaycasts = false;
+ startPos.y = originalPosition.y + Screen.height;
+ _rectTransform.anchoredPosition = startPos;
}
break;
case UIAnimationType.SlideFromBottom:
- if (rectTransform != null)
+ _ResetToBegin();
+ if (_rectTransform != null)
{
Vector2 startPos = originalPosition;
- startPos.y = -Screen.height;
- rectTransform.anchoredPosition = startPos;
- }
- if (canvasGroup != null)
- {
- canvasGroup.alpha = 0f;
- canvasGroup.interactable = false;
- canvasGroup.blocksRaycasts = false;
+ startPos.y = originalPosition.y-Screen.height;
+ _rectTransform.anchoredPosition = startPos;
}
break;
case UIAnimationType.SlideFromLeft:
- if (rectTransform != null)
+ _ResetToBegin();
+ if (_rectTransform != null)
{
Vector2 startPos = originalPosition;
- startPos.x = -Screen.width;
- rectTransform.anchoredPosition = startPos;
- }
- if (canvasGroup != null)
- {
- canvasGroup.alpha = 0f;
- canvasGroup.interactable = false;
- canvasGroup.blocksRaycasts = false;
+ startPos.x = originalPosition.x-Screen.width;
+ _rectTransform.anchoredPosition = startPos;
}
break;
case UIAnimationType.SlideFromRight:
- if (rectTransform != null)
+ _ResetToBegin();
+ if (_rectTransform != null)
{
Vector2 startPos = originalPosition;
- startPos.x = Screen.width;
- rectTransform.anchoredPosition = startPos;
- }
- if (canvasGroup != null)
- {
- canvasGroup.alpha = 0f;
- canvasGroup.interactable = false;
- canvasGroup.blocksRaycasts = false;
+ startPos.x = originalPosition.x+Screen.width;
+ _rectTransform.anchoredPosition = startPos;
}
break;
}
@@ -597,9 +603,9 @@
break;
case UIAnimationType.ScaleInOut:
- if (rectTransform != null)
+ if (_rectTransform != null)
{
- currentAnimation.Append(DOVirtual.Float(0.3f, originalScale.x, animeDuration, (value) => {canvasScaler.scaleFactor = value;}).SetEase(animationEase));
+ currentAnimation.Append(DOVirtual.Float(0.3f, 1f, animeDuration, (value) => {canvasScaler.scaleFactor = value;}).SetEase(animationEase));
}
break;
@@ -607,13 +613,9 @@
case UIAnimationType.SlideFromBottom:
case UIAnimationType.SlideFromLeft:
case UIAnimationType.SlideFromRight:
- if (rectTransform != null)
+ if (_rectTransform != null)
{
- currentAnimation.Append(rectTransform.DOAnchorPos(originalPosition, animeDuration).SetEase(animationEase));
- }
- if (canvasGroup != null)
- {
- currentAnimation.Join(canvasGroup.DOFade(1f, animeDuration).SetEase(animationEase));
+ currentAnimation.Append(_rectTransform.DOAnchorPos(originalPosition, animeDuration).SetEase(animationEase));
}
break;
}
@@ -651,6 +653,30 @@
}
}
+ // private void Update()
+ // {
+ // Debug.LogError(_rectTransform.parent.name + ":" + _rectTransform.anchoredPosition);
+ // }
+
+ private void _ResetToBegin()
+ {
+ if (canvasGroup != null)
+ {
+ canvasGroup.alpha = 1f;
+ canvasGroup.interactable = false;
+ canvasGroup.blocksRaycasts = false;
+ }
+ if (canvasScaler != null)
+ {
+ canvasScaler.scaleFactor = 1f;
+ }
+
+ if (_rectTransform != null)
+ {
+ _rectTransform.anchoredPosition = originalPosition;
+ }
+ }
+
protected virtual void OnOpenAnimationComplete()
{
@@ -678,66 +704,50 @@
case UIAnimationType.FadeInOut:
if (canvasGroup != null)
{
- currentAnimation.Append(canvasGroup.DOFade(0f, animeDuration).SetEase(animationEase));
+ currentAnimation.Append(canvasGroup.DOFade(0.1f, animeDuration).SetEase(animationEase));
}
break;
case UIAnimationType.ScaleInOut:
- if (rectTransform != null)
+ if (_rectTransform != null)
{
- currentAnimation.Append(DOVirtual.Float(originalScale.x, 0.3f, animeDuration, (value) => {canvasScaler.scaleFactor = value;}).SetEase(animationEase));
+ currentAnimation.Append(DOVirtual.Float(1f, 0.3f, animeDuration, (value) => {canvasScaler.scaleFactor = value;}).SetEase(animationEase));
}
break;
case UIAnimationType.SlideFromTop:
- if (rectTransform != null)
+ if (_rectTransform != null)
{
Vector2 endPos = originalPosition;
endPos.y = Screen.height;
- currentAnimation.Append(rectTransform.DOAnchorPos(endPos, animeDuration).SetEase(animationEase));
- }
- if (canvasGroup != null)
- {
- currentAnimation.Join(canvasGroup.DOFade(0f, animeDuration).SetEase(animationEase));
+ currentAnimation.Append(_rectTransform.DOAnchorPos(endPos, animeDuration).SetEase(animationEase));
}
break;
case UIAnimationType.SlideFromBottom:
- if (rectTransform != null)
+ if (_rectTransform != null)
{
Vector2 endPos = originalPosition;
endPos.y = -Screen.height;
- currentAnimation.Append(rectTransform.DOAnchorPos(endPos, animeDuration).SetEase(animationEase));
- }
- if (canvasGroup != null)
- {
- currentAnimation.Join(canvasGroup.DOFade(0f, animeDuration).SetEase(animationEase));
+ currentAnimation.Append(_rectTransform.DOAnchorPos(endPos, animeDuration).SetEase(animationEase));
}
break;
case UIAnimationType.SlideFromLeft:
- if (rectTransform != null)
+ if (_rectTransform != null)
{
Vector2 endPos = originalPosition;
endPos.x = -Screen.width;
- currentAnimation.Append(rectTransform.DOAnchorPos(endPos, animeDuration).SetEase(animationEase));
- }
- if (canvasGroup != null)
- {
- currentAnimation.Join(canvasGroup.DOFade(0f, animeDuration).SetEase(animationEase));
+ currentAnimation.Append(_rectTransform.DOAnchorPos(endPos, animeDuration).SetEase(animationEase));
}
break;
case UIAnimationType.SlideFromRight:
- if (rectTransform != null)
+ if (_rectTransform != null)
{
Vector2 endPos = originalPosition;
endPos.x = Screen.width;
- currentAnimation.Append(rectTransform.DOAnchorPos(endPos, animeDuration).SetEase(animationEase));
- }
- if (canvasGroup != null)
- {
- currentAnimation.Join(canvasGroup.DOFade(0f, animeDuration).SetEase(animationEase));
+ currentAnimation.Append(_rectTransform.DOAnchorPos(endPos, animeDuration).SetEase(animationEase));
}
break;
}
--
Gitblit v1.8.0