UGUI—Toast

作者:追风剑情 发布于:2019-10-12 16:11 分类:Unity3d

示例:

工程截图

2222.png3333.png

Toast.cs

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

/// <summary>
/// 类似Android的Toast飘字提示
/// UGUITool.cs 参见: http://www.devacg.com/?post=1049
/// </summary>
public class Toast : MonoBehaviour
{
    [SerializeField] private Image m_Background;
    [SerializeField] private Text m_Text;
    [SerializeField] private float m_StartY = 0f;
    [SerializeField] private float m_Duration = 0.3f;
    [SerializeField] private float m_KeepTime = 3f;
    [SerializeField] private float m_Speed = 500f;
    [SerializeField] private AnimationCurve m_Curve;
    [SerializeField] private bool m_IgnoreTimeScale = true;
    [SerializeField] private bool play = false;

    private bool m_Play;
    private float m_ElapsedTime = 0f;
    private RectTransform rectTransform;

    private string TEST_MSG = "当前已强化到最高等级";

    private void Awake()
    {
        rectTransform = m_Background.GetComponent<RectTransform>();
    }

    void Update()
    {
        if (play)
        {
            play = false;
            MakeText(TEST_MSG);
        }

        if (!m_Play)
            return;

        float dt = m_IgnoreTimeScale ? Time.unscaledDeltaTime : Time.deltaTime;
        m_ElapsedTime += dt;
        var percentage = Mathf.Clamp01(m_ElapsedTime / m_Duration);
        var scale = m_Curve.Evaluate(percentage);
        float dy = m_Speed * scale * dt;
        UGUITool.SetAnchoredPositionOffsetY(rectTransform, dy);

        if (percentage >= 1)
        {
            m_Play = false;
            StartCoroutine(DelayHide());
        }
    }

    IEnumerator DelayHide()
    {
        yield return new WaitForSeconds(m_KeepTime);
        m_Background.gameObject.SetActive(false);
    }

    // 飘字提示
    public void MakeText(string msg)
    {
        if (m_Text)
            m_Text.text = msg;
        m_ElapsedTime = 0;
        UGUITool.SetAnchoredPositionY(rectTransform, m_StartY);
        m_Play = true;
        m_Background.gameObject.SetActive(true);
    }
}

运行测试

111111.gif

第二版:支持多条Tips同时显示

11111111.png

222222.png

111111.png

UIToastTips.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 屏幕飘字提示
/// </summary>
public class UIToastTips : GameEventBehaviour
{
    [SerializeField]
    private GameObject background;
    [SerializeField]
    private GameObject itemTemplet;
    [SerializeField]
    private float moveSpeed = 1000;
    [SerializeField]
    private float gap = 10; //元素间隔
    //连续消息显示出来的间隔时间
    [SerializeField]
    private float messageInterval = 0.2f;
    private List<UIToastTipsItem> items = new List<UIToastTipsItem>();
    private Queue<Message> msgQueue = new Queue<Message>();
    private float t;

    private UIToastTipsItem GetToastTipsItem()
    {
        UIToastTipsItem item = null;
        for (int i = 0; i < items.Count; i++)
        {
            if (!items[i].gameObject.activeInHierarchy)
            {
                item = items[i];
                items.RemoveAt(i);
                break;
            }
        }

        if (item == null)
        {
            GameObject go = Instantiate<GameObject>(itemTemplet);
            go.SetActive(true);
            go.transform.SetParent(background.transform);
            go.transform.localPosition = Vector3.zero;
            go.transform.localRotation = Quaternion.identity;
            go.transform.localScale = Vector3.one;
            item = go.GetComponent<UIToastTipsItem>();
            item.Reset();
        }

        items.Insert(0, item);
        return item;
    }

    private void Sort()
    {
        int pre_i = -1;
        for (int i = 0; i < items.Count; i++)
        {
            if (items[i].IsHide || items[i].IsFadeout)
                continue;
            if (pre_i == -1)
            {
                //将第0号元素缓移到Y=0位置
                if (items[i].Y < 0)
                    items[i].Y += Time.deltaTime * moveSpeed;
                if (items[i].Y > 0)
                    items[i].Y = 0;
                pre_i = i;
                continue;
            }
            //考虑item的高度可能不同的情况
            items[i].Y = items[pre_i].Y + items[pre_i].Height / 2 + gap + items[i].Height / 2;
            pre_i = i;
        }
    }

    // 飘字提示
    public void MakeText(string msg, Color color)
    {
        var item = GetToastTipsItem();
        item.MakeText(msg, color);
        //计算新元素的初始位置
        if (items.Count <= 1 || items[1].IsHide) {
            item.Y = -(item.Height + gap);
        } else {
            item.Y = -(item.Height / 2 + items[1].Height / 2 + gap);
        }
        item.Y *= 2;//往下偏移
        Sort();
        transform.SetAsLastSibling();
    }

    protected override void OnAwake()
    {
        itemTemplet.SetActive(false);
        this.AddListener(GameEventType.INVOKE_UI_TOAST_TIPS, OnInvokeToastTips);
        this.AddListener(GameEventType.INVOKE_UI_TOAST_ERROR_TIPS, OnInvokeToastErrorTips);
    }

    protected override void OnUpdate()
    {
        if (msgQueue.Count > 0)
        {
            t += Time.deltaTime;
            if (t >= messageInterval)
            {
                t = 0;
                var msg = msgQueue.Dequeue();
                MakeText(msg.tips, msg.color);
            }
        }
        Sort();
    }

    private void OnInvokeToastTips(GameEventType type, object data)
    {
        string tips = data as string;
        Message msg = new Message();
        msg.color = Color.white;
        msg.tips = tips;
        msgQueue.Enqueue(msg);
    }

    private void OnInvokeToastErrorTips(GameEventType type, object data)
    {
        string tips = data as string;
        Message msg = new Message();
        msg.color = Color.red;
        msg.tips = tips;
        msgQueue.Enqueue(msg);
    }

    private class Message
    {
        public Color color;
        public string tips;
    }
}


UIToastTipsItem.cs

using System;
using System.Text.RegularExpressions;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// 屏幕飘字提示项
/// </summary>
public class UIToastTipsItem : MonoBehaviour
{
    [SerializeField]
    private RectTransform rectTransform;
    [SerializeField]
    private RectTransform textRectTransform;
    [SerializeField]
    private ContentSizeFitter m_ContentSizeFitter;
    [SerializeField]
    private Image m_Image;
    [SerializeField]
    private Text m_Text;
    [SerializeField]
    private float m_FadeinSpeed = 3f; //淡入速度
    [SerializeField]
    private float m_FadeoutSpeed = 3f;//淡出速度
    [SerializeField]
    private float m_MoveSpeed = 200f; //飘动速度
    [SerializeField]
    private float m_Duration = 1; //停留时间
    //最大文本宽度,超过此宽度自动换行
    [SerializeField]
    private float m_MaxTextWidth = 1200f;
    private Color m_ImageColor;
    private Color m_ToImageColor;
    private Color m_TextColor;
    private Color m_ToTextColor;
    private int step = -1;
    private float t = 0;

    public float Width
    {
        get
        {
            return rectTransform.rect.width;
        }
    }

    public float Height
    {
        get
        {
            return rectTransform.rect.height;
        }
    }

    public float Y
    {
        get
        {
            Vector2 pos = rectTransform.anchoredPosition;
            return pos.y;
        }
        set
        {
            Vector2 pos = rectTransform.anchoredPosition;
            pos.y = value;
            rectTransform.anchoredPosition = pos;
        }
    }

    public bool IsFadeout
    {
        get
        {
            return step == 2;
        }
    }

    private void Awake()
    {
        m_ImageColor = m_Image.color;
        m_ToImageColor = m_ImageColor;
        m_ToImageColor.a = 0;

        m_TextColor = m_Text.color;
        m_ToTextColor = m_TextColor;
        m_ToTextColor.a = 0;
    }

    public void Reset()
    {
        rectTransform.localPosition = Vector3.zero;
        m_ContentSizeFitter.horizontalFit = ContentSizeFitter.FitMode.PreferredSize;
    }

    private static readonly Regex RichColorRega = new Regex("<color=#([a-f0-9]{8})>", RegexOptions.IgnoreCase);
    //设置Text的alpha
    private void SetTextAlpha(float alpha)
    {
        //设置Text组件颜色
        Color color = m_Text.color;
        color.a = alpha;
        m_Text.color = color;

        //设置富文本颜色
        //[0,255]
        int alpha255 = (int)(alpha * 255f);
        //16进制
        string alphaHex = Convert.ToString(alpha255, 16);
        if (alphaHex.Length == 1)
            alphaHex = "0" + alphaHex;
        string str = m_Text.text;
        char[] chars = str.ToCharArray();
        Match match = RichColorRega.Match(str);
        Group groups = null;
        while (match.Success)
        {
            groups = match.Groups[1];
            //替换alpha值
            chars[groups.Index + 6] = alphaHex[0];
            chars[groups.Index + 7] = alphaHex[1];
            match = match.NextMatch();
        }
        str = new string(chars);
        m_Text.text = str;
    }

    //设置Image的alpha
    private void SetImageAlpha(float alpha)
    {
        Color color = m_Image.color;
        color.a = alpha;
        m_Image.color = color;
    }

    private void Update()
    {
        float alpha = 0f;
        switch (step)
        {
            case 0: //淡入阶段
                t += Time.deltaTime * m_FadeinSpeed;
                alpha = Mathf.Lerp(0, 1, t);
                SetTextAlpha(alpha);
                SetImageAlpha(alpha);
                if (t >= 1)
                {
                    t = 0;
                    step = 1;
                }
                break;
            case 1: //停留阶段
                t += Time.deltaTime;
                if (t >= m_Duration)
                {
                    t = 0;
                    step = 2;
                }
                break;
            case 2: //淡出阶段
                t += Time.deltaTime * m_FadeoutSpeed;
                alpha = Mathf.Lerp(1, 0, t);
                SetTextAlpha(alpha);
                SetImageAlpha(alpha);
                Y += Time.deltaTime * m_MoveSpeed;
                if (t >= 1)
                {
                    t = 0;
                    step = -1;
                    Hide();
                }
                break;
        }
    }

    public void PreferredSize()
    {
        //强制刷新UI,这样获取到的width,height才是最新值
        LayoutRebuilder.ForceRebuildLayoutImmediate(textRectTransform);
        float width = textRectTransform.rect.width;
        float height = textRectTransform.rect.height;
        //超出最大宽度,文本需要自动换行
        //Text属性需设置成 Horizontal Overflow: Wrap,Vertical Overflow: Overflow
        if (width > m_MaxTextWidth)
        {
            width = m_MaxTextWidth;
            m_ContentSizeFitter.horizontalFit = ContentSizeFitter.FitMode.Unconstrained;
            textRectTransform.sizeDelta = new Vector2(m_MaxTextWidth, height);
            LayoutRebuilder.ForceRebuildLayoutImmediate(textRectTransform);
            height = textRectTransform.rect.height;
        }
        Vector2 sizeDelta = new Vector2(width + 40, height + 10);
        rectTransform.sizeDelta = sizeDelta;
    }

    // 飘字提示
    public void MakeText(string msg, Color color)
    {
        if (m_Text == null)
            return;
        m_TextColor = color;
        m_ToTextColor = color;
        m_ToTextColor.a = 0;

        m_Image.color = m_ImageColor;
        m_Text.color = m_TextColor;
        m_Text.text = msg;
        //必须先激活,否则后面的布局计算无法生效
        Show();
        PreferredSize();
        Fadein();
    }

    private void Fadein()
    {
        t = 0;
        step = 0;
    }

    public void Show()
    {
        gameObject.SetActive(true);
    }

    public void Hide()
    {
        gameObject.SetActive(false);
    }

    public bool IsHide
    {
        get
        {
            return !gameObject.activeInHierarchy;
        }
    }

    private void OnDestroy()
    {
        this.CancelInvoke();
    }
}

11115.gif


标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号