一、工程截图
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;
}
}
运行效果