首次提交
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using DG.Tweening;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class Toast : MonoBehaviour
|
||||
{
|
||||
public Text toastText;
|
||||
|
||||
private RectTransform _rectTransform;
|
||||
private CanvasGroup _canvasGroup;
|
||||
|
||||
private Vector2 _startPosition;
|
||||
private Sequence _sequence;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_rectTransform = transform as RectTransform;
|
||||
_canvasGroup = gameObject.AddComponent<CanvasGroup>();
|
||||
|
||||
if (_rectTransform != null) _startPosition = _rectTransform.anchoredPosition;
|
||||
|
||||
}
|
||||
|
||||
public void ShowToast(string str, float duration = 1.5f, float fadeOutDuration = 1.5f, float moveDistance = 250f)
|
||||
{
|
||||
toastText.text = str;
|
||||
|
||||
_sequence = DOTween.Sequence();
|
||||
_sequence
|
||||
.Append(_rectTransform.DOAnchorPosY(_startPosition.y + moveDistance, duration)) // 上升动画
|
||||
.Join(_canvasGroup.DOFade(0f, fadeOutDuration)) // 淡出动画
|
||||
.OnComplete(() => ToastPool.Instance.ReturnToPool(this)); // 动画结束后隐藏
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
// 重置透明度
|
||||
_canvasGroup.alpha = 1f;
|
||||
|
||||
// 重置位置
|
||||
_rectTransform.anchoredPosition = _startPosition;
|
||||
|
||||
_rectTransform.gameObject.SetActive(false);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user