_xiaofang/xiaofang/Assets/Script/UI/ZZZZZZ/Progress.cs
2024-12-26 18:03:45 +08:00

35 lines
905 B
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Progress : MonoBehaviour
{
public Image targetImage; // 要控制的图片
public float fillDuration = 1f; // 填充完成所需的时间(秒)
void Start()
{
//StartCoroutine(FillImage(targetImage, fillDuration));
}
public void Start1()
{
StartCoroutine(FillImage(targetImage, fillDuration));
}
private IEnumerator FillImage(Image image, float duration)
{
float elapsed = 0f; // 计时器
image.fillAmount = 0f; //强制为0
while (elapsed < duration)
{
elapsed += Time.deltaTime; // 累加帧间隔时间
image.fillAmount = Mathf.Clamp01(elapsed / duration); // 根据时间计算填充量
yield return null; // 等待下一帧
}
image.fillAmount = 1f; // 确保填充值最终为1
}
}