UGUI—菊花风格Loading

作者:追风剑情 发布于:2020-7-1 13:48 分类:Unity3d

一、工程截图

111111.png

222222.png

WaitLoadingUI.cs

using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// 转菊花-加载界面
/// </summary>
public class WaitLoadingUI : GameEventBehaviour
{
    [SerializeField]
    private GameObject background;
    [SerializeField]
    private Text tipText;

    protected override void OnAwake()
    {
        this.AddListener(GameEventType.INVOKE_UI_WAIT_LOADING_SHOW, OnInvokeShow);
        this.AddListener(GameEventType.INVOKE_UI_WAIT_LOADING_HIDE, OnInvokeHide);
    }

    private void OnInvokeShow(GameEventType type, object data)
    {
        string tips = data as string;
        tipText.text = string.IsNullOrEmpty(tips) ? "加载中..." : tips;
        background.SetActive(true);
        SetTopDisplay();
    }

    private void OnInvokeHide(GameEventType type, object data)
    {
        background.SetActive(false);
    }

    // 显示到UI的最上层
    private void SetTopDisplay()
    {
        transform.SetAsLastSibling();
    }
}


WaitLoadingEffect.cs

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// 转菊花-加载特效
/// </summary>
public class WaitLoadingEffect : MonoBehaviour
{
    [SerializeField]
    private Transform m_ImpellorParent;
    [SerializeField]
    private int m_ImpellorCount = 14;//叶片个数
    [SerializeField]
    private Image m_Templet;
    [SerializeField]
    private int m_FrameRate = 30;
    [SerializeField]
    private Color m_StartColor = Color.white;
    [SerializeField]
    private Color m_EndColor = Color.gray;

    private List<Image> imageList = new List<Image>();
    private int startIndex = 0;
    private float deltaAlpha = 0;
    private float frameTime = 0.1f;
    private float t;

    void Start()
    {
        float angle = -360f / m_ImpellorCount;
        frameTime = 1.0f / m_FrameRate;
        deltaAlpha = 1.0f / m_ImpellorCount;
        for (int i = 0; i < m_ImpellorCount; i++)
        {
            Image image = Instantiate<Image>(m_Templet);
            image.color = m_EndColor;
            RectTransform rt = image.gameObject.GetComponent<RectTransform>();
            rt.SetParent(m_ImpellorParent);
            UGUITool.SetAnchoredPosition3D(rt, 0, 0, 0);
            UGUITool.SetAnchoredRotate(rt, Vector3.forward, i * angle);
            rt.localScale = Vector3.one;
            imageList.Add(image);
        }
        m_Templet.gameObject.SetActive(false);
    }

    void Update()
    {
        t += Time.deltaTime;
        if (t < frameTime)
            return;
        t = 0;

        for (int i = 0; i < m_ImpellorCount; i++)
        {
            int index = (startIndex + i) % m_ImpellorCount;
            Color color = Color.Lerp(m_EndColor, m_StartColor, i * deltaAlpha);
            imageList[index].color = color;
        }

        startIndex = (startIndex + 1) % m_ImpellorCount;
    }
}


运行效果

3335.gif

标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号