Files
5.24BuYu/Assets/MyGame/aa/UI/DefaultPanel/LoadPanel/LoadIngTemp/ProgressBar.cs
T
2026-05-01 06:26:50 +08:00

57 lines
1.4 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 TMPro;
using UnityEngine;
using UnityEngine.UI;
public class ProgressBar : MonoBehaviour
{
[SerializeField] private Slider slider; // Unity UI Slider 组件
[SerializeField] private GameObject panel; // 进度条面板(比如 JinDuTiao)
[SerializeField] private TextMeshProUGUI tMP_Num; // 进度条面板(比如 JinDuTiao)
private float targetProgress = 0f;
private void Awake()
{
if (slider == null)
slider = GetComponentInChildren<Slider>();
if (panel == null)
panel = gameObject;
}
/// <summary>
/// 显示进度条并重置
/// </summary>
public void Show()
{
if (panel != null) panel.SetActive(true);
slider.value = 0f;
targetProgress = 0f;
}
/// <summary>
/// 隐藏进度条
/// </summary>
public void Hide()
{
if (panel != null) panel.SetActive(false);
}
/// <summary>
/// 设置目标进度(0~1)
/// </summary>
public void SetProgress(float progress)
{
tMP_Num.text = (progress * 100).ToString()+"%";
targetProgress = Mathf.Clamp01(progress);
}
private void Update()
{
// 平滑过渡到目标进度
if (slider != null)
slider.value = Mathf.MoveTowards(slider.value, targetProgress, Time.deltaTime);
}
}