51 lines
1.3 KiB
C#
51 lines
1.3 KiB
C#
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; // 重置进度条
|
||
}
|
||
}
|
||
}
|