Multi-message prompt animation based on uui and dotween:

Prerequisite: The DOTween plug-in must be included. Of course, if you do not use the plug-in to do easing animation, you can also use animation animation to customize the corresponding easing effect: Effect: In fact, this is quite simple, using
two
Insert image description here
queues (one of which I still Using list), one is the message display queue, and the other is the message cache object pool (hidden).
I use the prefab _layer loaded by Resources.Load,
which is my custom-defined message animation layer (a Canvas)

The code is relatively simple, so I won’t go into details:

using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using view;

public class MessageMgr : MonoSingleton<MessageMgr>
{
    //消息本体
    private Text itemText;
    /// <summary>
    /// 消息列表
    /// </summary>
    private List<MessageItem> ItemList;
    /// <summary>
    /// 回收列表
    /// </summary>
    private Queue<MessageItem> QueText;
    private GameObject _layer;
    private Vector2 StartPos;
    //文本高度
    public float texHeight;
    public float MessLine;
    private int maxMsg;
    private void Awake()
    {
        itemText = Resources.Load<Text>(UrlUntil.Instance.getUIPath() + "MessItem");
        ItemList = new List<MessageItem>();
        QueText = new Queue<MessageItem>();
        //5添加到消息层
        _layer = UIMgr.Instance.getGameObjectType(UIType.MESSAGE);
        //StartPos = new Vector2(App.Instance.ScreenW / 2, App.Instance.ScreenH / 2);
        StartPos = new Vector2(0, 0);
        texHeight = 30;
        //消息间隔
        MessLine = 15;
        //最多显示消息
        maxMsg = 10;
    }
    private MessageItem GetItem()
    {
        MessageItem item = null;
        //如果队列中有就返回出去
        if (QueText.Count > 0)
        {
            item = QueText.Dequeue();
        }
        //没有就复制一个
        else
        {
            //超过限制就取最上面的一个
            if (ItemList.Count >= maxMsg)
            {
                item = ItemList[0];
                ItemList.RemoveAt(0);
            }
            else
            {
                item = new MessageItem(Instantiate(itemText));
            }
        }
        item.transform.gameObject.SetActive(true);
        ItemList.Add(item);
        return item;
    }
    /// <summary>
    /// 显示相对应的消息
    /// </summary>
    /// <param name="message"></param>
    /// <param name="color"></param>
    public void showMessage(string message,Color color=default(Color))
    {
        MessageItem item = GetItem();
        if (item == null) return;
        if (item.transform.parent == null)
        {
            item.transform.SetParent(_layer.transform,false);
        }
        //Debug.Log(item.transform.localPosition + "---" + StartPos);
        item.transform.localPosition = StartPos;

        item.show(message, QueText, ItemList, color);
        updataMsgY();
    }
    /// <summary>
    /// 坐标移动
    /// </summary>
    private void updataMsgY()
    {
        for (int i = 0; i < ItemList.Count; i++)
        {
            float pos = StartPos.y + texHeight * (ItemList.Count - i) + MessLine;
            ItemList[i].UpDataY(pos, i);
        }
    }
}

Item message class:
Before playing the easing animation, you must kill the animation and clear the animation queue.
Some parameters inside can be promoted to member variables according to your own needs.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;

public class MessageItem 
{
    public string msgKey;
    Text text;
    public Sequence twQue;
    public MessageItem(Text tex)
    {
        text = tex;
    }
    public Transform transform
    {
        get { return text.transform; }
    }
    /// <summary>
    /// 更新Y坐标
    /// </summary>
    /// <param name="movY"></param>
    public void UpDataY(float movY,int id)
    {
        //移动前必须杀死动画
        text.transform.DOKill();
        text.transform.DOLocalMoveY(movY, 0.5f);
    }
    /// <summary>
    /// 3秒隐身
    /// </summary>
    public void show(string msg, Queue<MessageItem> que, List<MessageItem> list,Color color)
    {
        setMsg(msg, color);
        text.color = new Color(text.color.r, text.color.g, text.color.b, 1);

        Color tagcol = text.color * new Color(1, 1, 1, 0);
        //动画队列
        //DOTween.Kill(twQue);
        twQue.Kill();
        twQue = DOTween.Sequence();  
        //twQue.SetId(list.IndexOf(this));
        //间隔3秒隐身
        twQue.AppendInterval(3);
        twQue.Append(text.DOColor(tagcol, 0.5f));
        //隐身后添加到队列
        twQue.AppendCallback(() =>
        {
            Debug.Log("DOTween动画播放完");
            text.gameObject.SetActive(false);
            if (!que.Contains(this))
            {
                que.Enqueue(this);
                list.Remove(this);
            }
        });
    }
    
    /// <summary>
    /// 设置文本信息
    /// </summary>
    /// <param name="msg"></param>
    private void setMsg(string msg,Color color)
    {
        this.text.text = msg;
        this.text.color = color;
    }
}

Guess you like

Origin blog.csdn.net/QO_GQ/article/details/119615880