using System.Collections; using UnityEngine; using UnityEngine.UI; using TMPro; /// /// 进度条控制器 /// 提供3秒进度条动画和完成提示功能 /// 支持自定义进度时间、提示文本和控制操作 /// public class ProgressBarController : MonoBehaviour { [Header("UI组件引用")] [Tooltip("进度条Slider组件")] [SerializeField] private Slider progressBar; [Tooltip("文本显示组件(显示进度和完成提示)")] [SerializeField] private TMP_Text completionText; [Header("进度设置")] [Tooltip("进度持续时间(秒)")] [SerializeField] private float progressDuration = 3.0f; [Tooltip("进度过程中的提示文本")] [SerializeField] private string progressMessage = "正在提交中,请稍等..."; [Tooltip("完成提示文本")] [SerializeField] private string completionMessage = "已提交"; [Tooltip("是否在开始时自动播放")] [SerializeField] private bool autoStart = false; [Header("动画设置")] [Tooltip("进度更新频率(秒)")] [SerializeField] private float updateInterval = 0.1f; [Tooltip("是否显示百分比")] [SerializeField] private bool showPercentage = true; // 私有变量 private Coroutine progressCoroutine; private bool isProgressing = false; private float currentProgress = 0f; // 事件回调 public System.Action OnProgressStarted; public System.Action OnProgressCompleted; public System.Action OnProgressUpdated; /// /// 初始化组件 /// private void Awake() { InitializeComponents(); SetupUI(); } /// /// 组件启动时调用 /// private void Start() { if (autoStart) { StartProgress(); } } /// /// 初始化UI组件引用 /// private void InitializeComponents() { // 自动查找进度条组件 if (progressBar == null) { progressBar = GetComponentInChildren(); } // 自动查找文本组件 if (completionText == null) { completionText = GetComponentInChildren(); } // 验证必要组件 if (progressBar == null) { Debug.LogError($"ProgressBarController: 在 {gameObject.name} 中未找到Slider组件!"); } if (completionText == null) { Debug.LogWarning($"ProgressBarController: 在 {gameObject.name} 中未找到TMP_Text组件,文本将不会显示!"); } } /// /// 设置UI初始状态 /// private void SetupUI() { // 设置进度条初始值 if (progressBar != null) { progressBar.value = 0f; progressBar.minValue = 0f; progressBar.maxValue = 1f; } // 设置初始文本 UpdateProgressText(0f); } /// /// 开始进度动画 /// public void StartProgress() { if (isProgressing) { Debug.LogWarning("ProgressBarController: 进度条已在运行中!"); return; } Debug.Log("ProgressBarController: 开始进度动画"); isProgressing = true; currentProgress = 0f; // 触发开始事件 OnProgressStarted?.Invoke(); // 启动进度协程 if (progressCoroutine != null) { StopCoroutine(progressCoroutine); } progressCoroutine = StartCoroutine(ProgressCoroutine()); } /// /// 暂停进度动画 /// public void PauseProgress() { if (!isProgressing) { Debug.LogWarning("ProgressBarController: 进度条未在运行中!"); return; } Debug.Log("ProgressBarController: 暂停进度动画"); isProgressing = false; if (progressCoroutine != null) { StopCoroutine(progressCoroutine); progressCoroutine = null; } } /// /// 重置进度条 /// public void ResetProgress() { Debug.Log("ProgressBarController: 重置进度条"); // 停止当前进度 if (progressCoroutine != null) { StopCoroutine(progressCoroutine); progressCoroutine = null; } isProgressing = false; currentProgress = 0f; // 重置UI if (progressBar != null) { progressBar.value = 0f; } UpdateProgressText(0f); } /// /// 进度动画协程 /// private IEnumerator ProgressCoroutine() { float elapsedTime = 0f; while (elapsedTime < progressDuration && isProgressing) { // 使用Time.deltaTime实现平滑更新 elapsedTime += Time.deltaTime; // 计算当前进度,确保不超过1 currentProgress = Mathf.Clamp01(elapsedTime / progressDuration); // 更新UI UpdateProgressBar(currentProgress); UpdateProgressText(currentProgress); // 触发进度更新事件 OnProgressUpdated?.Invoke(currentProgress); // 每帧更新,实现平滑动画 yield return null; } // 确保进度达到100% if (isProgressing) { currentProgress = 1f; UpdateProgressBar(1f); UpdateProgressText(1f); OnProgressUpdated?.Invoke(1f); // 完成进度 CompleteProgress(); } } /// /// 更新进度条显示 /// private void UpdateProgressBar(float progress) { if (progressBar != null) { progressBar.value = progress; } } /// /// 更新进度文本显示 /// private void UpdateProgressText(float progress) { if (completionText != null) { if (showPercentage) { int percentage = Mathf.RoundToInt(progress * 100); completionText.text = $"{progressMessage} {percentage}%"; } else { completionText.text = $"{progressMessage} 进度: {progress:F2}"; } } } /// /// 完成进度处理 /// private void CompleteProgress() { Debug.Log("ProgressBarController: 进度完成"); isProgressing = false; // 显示完成提示 if (completionText != null) { completionText.text = completionMessage; } // 触发完成事件 OnProgressCompleted?.Invoke(); } /// /// 设置进度持续时间 /// public void SetProgressDuration(float duration) { if (duration > 0) { progressDuration = duration; Debug.Log($"ProgressBarController: 设置进度持续时间为 {duration} 秒"); } else { Debug.LogError("ProgressBarController: 进度持续时间必须大于0!"); } } /// /// 设置进度过程中的提示文本 /// public void SetProgressMessage(string message) { progressMessage = message; Debug.Log($"ProgressBarController: 设置进度提示为 '{message}'"); } /// /// 设置完成提示文本 /// public void SetCompletionMessage(string message) { completionMessage = message; Debug.Log($"ProgressBarController: 设置完成提示为 '{message}'"); } /// /// 获取当前进度值 /// public float GetCurrentProgress() { return currentProgress; } /// /// 检查是否正在运行 /// public bool IsProgressing() { return isProgressing; } /// /// 组件销毁时清理 /// private void OnDestroy() { if (progressCoroutine != null) { StopCoroutine(progressCoroutine); } } }