效果图

实现代码
using UnityEngine;
using UnityEngine.UI;
public class Loading : MonoBehaviour
{
public Image jinduImage; // 填充图片
public RectTransform guangTransform; // 光点对象
public RectTransform jinduRect; // jindu的RectTransform
private float duration = 3f; // 总加载时间
private float currentTime = 0f; // 当前已用时间
private bool guangShown = false; // 光点是否已显示
private bool isCompleted = false; // 是否加载完成
void Start()
{
guangTransform.gameObject.SetActive(false); // 初始隐藏光点
}
void Update()
{
if (!isCompleted)
{
currentTime += Time.deltaTime;
float progress = Mathf.Clamp01(currentTime / duration);
jinduImage.fillAmount = progress;
// 0.3秒后显示光点
if (!guangShown && currentTime >= 0.3f)
{
guangTransform.gameObject.SetActive(true);
guangShown = true;
}
// 判断是否加载完成
if (currentTime >= duration)
{
isCompleted = true;
OnLoadingComplete(); // 加载完成后执行方法
}
}
// 光点位置更新
if (guangTransform.gameObject.activeSelf)
{
float fillAmount = jinduImage.fillAmount;
float width = jinduRect.rect.width;
float x = (fillAmount - 0.55f) * width;
Vector2 anchoredPos = guangTransform.anchoredPosition;
anchoredPos.x = x;
guangTransform.anchoredPosition = anchoredPos;
}
}
// 加载完成后调用这个函数
void OnLoadingComplete()
{
Debug.Log("加载完成!");
// 你可以在这里执行任何操作,比如切换场景、显示按钮、触发动画等
}
}