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(); if (panel == null) panel = gameObject; } /// /// 显示进度条并重置 /// public void Show() { if (panel != null) panel.SetActive(true); slider.value = 0f; targetProgress = 0f; } /// /// 隐藏进度条 /// public void Hide() { if (panel != null) panel.SetActive(false); } /// /// 设置目标进度(0~1) /// public void SetProgress(float progress) { tMP_Num.text = Mathf.RoundToInt(progress * 100f) + "%"; targetProgress = Mathf.Clamp01(progress); } private void Update() { // 平滑过渡到目标进度 if (slider != null) slider.value = Mathf.MoveTowards(slider.value, targetProgress, Time.deltaTime); } }