yyl
2025-06-10 bb917a44e530f58edfe35ecc73a17ffd00b2f3ef
18 子 2D卡牌客户端搭建 / 2D卡牌客户端搭建 UI动画审查跟问题修复
3个文件已修改
207 ■■■■ 已修改文件
Main/Manager/UIManager.cs 11 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Main/System/Main/MainWin.cs 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Main/System/UIBase/UIBase.cs 192 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Main/Manager/UIManager.cs
@@ -3,6 +3,7 @@
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using DG.Tweening;
/// <summary>
/// UI管理器 - 负责管理所有UI界面的显示、隐藏和层级
@@ -64,23 +65,25 @@
    #endregion
    #region 初始化
    /// <summary>
    /// 初始化UI管理器
    /// </summary>
    public override void Init()
    {
        base.Init();
        // 初始化UI根节点
        InitUIRoot();
        // 初始化缓存
        layerSortingOrderCache.Clear();
        layerTransformCache.Clear();
        uiInstanceCounter.Clear();
        DOTween.SetTweensCapacity(500, 50);
        Debug.Log("UI管理器初始化完成");
    }
    
    /// <summary>
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
            //测试代码
            #endif
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; // 根节点
    // 子UI管理
    [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();
        // 在Awake中进行基本初始化
        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;
            }