UGUI—菊花风格Loading

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

一、工程截图

11111.png

代码

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// 转菊花 Load...
/// </summary>
public class WaitLoading : 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.parent = m_ImpellorParent;
            UGUITool.SetAnchoredPosition(rt, 0, 0);
            UGUITool.SetAnchoredRotate(rt, Vector3.forward, i * angle);
            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号   sitemap

川公网安备 51019002001593号