| | |
| | | [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 |
| | |
| | | // 运行时状态 |
| | | [HideInInspector] public int lastUsedRound = 0; |
| | | [HideInInspector] public UIBase parentUI; |
| | | |
| | | [HideInInspector] public GameObject rootNode; // 根节点 |
| | | |
| | | // 子UI管理 |
| | | [HideInInspector] public List<UIBase> childrenUI = new List<UIBase>(); |
| | |
| | | // 组件引用 |
| | | protected Canvas canvas; |
| | | protected CanvasGroup canvasGroup; |
| | | protected RectTransform rectTransform; |
| | | protected RectTransform _rectTransform; |
| | | |
| | | // 动画相关 |
| | | protected Vector3 originalScale; |
| | | protected Vector3 originalPosition; |
| | | protected Sequence currentAnimation; |
| | | |
| | |
| | | |
| | | 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(); |
| | |
| | | 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); |
| | | } |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | canvasScaler = GetComponent<CanvasScaler>(); |
| | | |
| | | // 获取RectTransform组件 |
| | | rectTransform = GetComponent<RectTransform>(); |
| | | } |
| | | |
| | | // 获取必要的组件 |
| | |
| | | // 播放打开动画 |
| | | protected virtual void PlayOpenAnimation() |
| | | { |
| | | |
| | | |
| | | if (openAnimationType == UIAnimationType.None) |
| | | { |
| | | // 无动画,直接启用交互 |
| | | if (canvasGroup != null) |
| | | { |
| | | canvasGroup.alpha = 1f; |
| | | canvasGroup.interactable = true; |
| | | canvasGroup.blocksRaycasts = true; |
| | | } |
| | | // 初始值设定好 |
| | | _ResetToBegin(); |
| | | return; |
| | | } |
| | | |
| | |
| | | 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; |
| | | } |
| | |
| | | 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; |
| | | |
| | |
| | | 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; |
| | | } |
| | |
| | | } |
| | | } |
| | | |
| | | // 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() |
| | | { |
| | | |
| | |
| | | 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; |
| | | } |