using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
using System;
public class Message : MonoBehaviour
{
public float dropDuration = 0.5f; // 掉落持续时间
public float persisterDuration = 1f; // 持续显示时间
public float dorpHeight;
public static Message instance;
Vector3 originPosition;
Image image;
Text messgaeText;
private void Awake()
{
if(instance == null )
{
instance = this;
}
else
{
Destroy(gameObject);
}
}
void Start()
{
originPosition=transform.position;
image = transform.GetComponent<Image>();
messgaeText = transform.GetComponentInChildren<Text>();
}
public void ShowMessgae(string message,Action action=null)
{
Original();
Sequence s = DOTween.Sequence();
s.AppendCallback(() => messgaeText.text =message);
s.Append(transform.DOLocalMoveY(dorpHeight, dropDuration,true).SetEase(Ease.OutCubic));
//延迟调用函数
s.Append(DOVirtual.DelayedCall(persisterDuration, Hide));
s.AppendCallback(()=>action?.Invoke());
}
void Hide()
{
image.DOFade(0f, 0.5f).SetEase(Ease.OutCubic);
messgaeText.DOFade(0f, 0.5f).SetEase(Ease.OutCubic);
}
void Original()
{
Debug.Log("恢复原样");
transform.position = originPosition;
image.DOFade(1, 0.1f).SetEase(Ease.OutCubic);
messgaeText.DOFade(1, 0.1f).SetEase(Ease.OutCubic);
}
}
消息提示框放在屏幕外