_xiaofang/xiaofang/Assets/Res/gsj/scripts/chronograph.cs
2024-12-20 10:10:19 +08:00

51 lines
1.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class chronograph : MonoBehaviour
{
private Image image;
public float duration = 3f; // 进度条的持续时间(秒)
private float timer = 0f; // 计时器
private bool isRunning = false; // 进度条是否运行
// Start is called before the first frame update
void Start()
{
image=transform.Find("Image/Image").GetComponent<Image>();
image.fillAmount = 0f; // 初始时进度条为空
StartProgressBar(); // 启动进度条
}
// Update is called once per frame
void Update()
{
if (isRunning)
{
timer += Time.deltaTime;
// 计算填充值0 到 1
float progress = timer / duration;
// 更新 Image 的填充量
image.fillAmount = Mathf.Clamp01(progress);
// 判断进度条是否完成
if (timer >= duration)
{
isRunning = false;
Debug.Log("进度条完成!");
}
}
}
// 启动进度条的方法
public void StartProgressBar()
{
timer = 0f;
isRunning = true;
if (image != null)
{
image.fillAmount = 0f; // 重置进度条
}
}
}