uui と dotween に基づくマルチメッセージ プロンプト アニメーション:

前提条件: DOTween プラグインが含まれている必要があります。もちろん、イージング アニメーションを行うためにプラグインを使用しない場合でも、アニメーション アニメーションを使用して、対応するイージング効果をカスタマイズすることもできます。 効果: 実際、これは非常に簡単です。 、
2 つの
ここに画像の説明を挿入します
キュー (そのうちの 1 つはまだ使用リスト) を使用し、1 つはメッセージ表示キュー、もう 1 つはメッセージ キャッシュ オブジェクト プール (非表示) です。
Resources.Load によってロードされた prefab _layer を使用します。
これは、カスタム定義のメッセージ アニメーション レイヤー (Canvas) です。

コードは比較的単純なので、詳細は説明しません。

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);
        }
    }
}

アイテム メッセージ クラス:
イージング アニメーションを再生する前に、アニメーションを強制終了し、アニメーション キューをクリアする必要があります。
内部の一部のパラメーターは、必要に応じてメンバー変数にプロモートできます。

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;
    }
}

おすすめ

転載: blog.csdn.net/QO_GQ/article/details/119615880