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

标签: Unity3d

Powered by emlog  蜀ICP备18021003号   sitemap

川公网安备 51019002001593号