337 lines
8.5 KiB
C#
337 lines
8.5 KiB
C#
using System.Collections;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
using TMPro;
|
||
|
||
/// <summary>
|
||
/// 进度条控制器
|
||
/// 提供3秒进度条动画和完成提示功能
|
||
/// 支持自定义进度时间、提示文本和控制操作
|
||
/// </summary>
|
||
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<float> OnProgressUpdated;
|
||
|
||
/// <summary>
|
||
/// 初始化组件
|
||
/// </summary>
|
||
private void Awake()
|
||
{
|
||
InitializeComponents();
|
||
SetupUI();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 组件启动时调用
|
||
/// </summary>
|
||
private void Start()
|
||
{
|
||
if (autoStart)
|
||
{
|
||
StartProgress();
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 初始化UI组件引用
|
||
/// </summary>
|
||
private void InitializeComponents()
|
||
{
|
||
// 自动查找进度条组件
|
||
if (progressBar == null)
|
||
{
|
||
progressBar = GetComponentInChildren<Slider>();
|
||
}
|
||
|
||
// 自动查找文本组件
|
||
if (completionText == null)
|
||
{
|
||
completionText = GetComponentInChildren<TMP_Text>();
|
||
}
|
||
|
||
// 验证必要组件
|
||
if (progressBar == null)
|
||
{
|
||
Debug.LogError($"ProgressBarController: 在 {gameObject.name} 中未找到Slider组件!");
|
||
}
|
||
|
||
if (completionText == null)
|
||
{
|
||
Debug.LogWarning($"ProgressBarController: 在 {gameObject.name} 中未找到TMP_Text组件,文本将不会显示!");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 设置UI初始状态
|
||
/// </summary>
|
||
private void SetupUI()
|
||
{
|
||
// 设置进度条初始值
|
||
if (progressBar != null)
|
||
{
|
||
progressBar.value = 0f;
|
||
progressBar.minValue = 0f;
|
||
progressBar.maxValue = 1f;
|
||
}
|
||
|
||
// 设置初始文本
|
||
UpdateProgressText(0f);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 开始进度动画
|
||
/// </summary>
|
||
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());
|
||
}
|
||
|
||
/// <summary>
|
||
/// 暂停进度动画
|
||
/// </summary>
|
||
public void PauseProgress()
|
||
{
|
||
if (!isProgressing)
|
||
{
|
||
Debug.LogWarning("ProgressBarController: 进度条未在运行中!");
|
||
return;
|
||
}
|
||
|
||
Debug.Log("ProgressBarController: 暂停进度动画");
|
||
isProgressing = false;
|
||
|
||
if (progressCoroutine != null)
|
||
{
|
||
StopCoroutine(progressCoroutine);
|
||
progressCoroutine = null;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 重置进度条
|
||
/// </summary>
|
||
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);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 进度动画协程
|
||
/// </summary>
|
||
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();
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 更新进度条显示
|
||
/// </summary>
|
||
private void UpdateProgressBar(float progress)
|
||
{
|
||
if (progressBar != null)
|
||
{
|
||
progressBar.value = progress;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 更新进度文本显示
|
||
/// </summary>
|
||
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}";
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 完成进度处理
|
||
/// </summary>
|
||
private void CompleteProgress()
|
||
{
|
||
Debug.Log("ProgressBarController: 进度完成");
|
||
isProgressing = false;
|
||
|
||
// 显示完成提示
|
||
if (completionText != null)
|
||
{
|
||
completionText.text = completionMessage;
|
||
}
|
||
|
||
// 触发完成事件
|
||
OnProgressCompleted?.Invoke();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 设置进度持续时间
|
||
/// </summary>
|
||
public void SetProgressDuration(float duration)
|
||
{
|
||
if (duration > 0)
|
||
{
|
||
progressDuration = duration;
|
||
Debug.Log($"ProgressBarController: 设置进度持续时间为 {duration} 秒");
|
||
}
|
||
else
|
||
{
|
||
Debug.LogError("ProgressBarController: 进度持续时间必须大于0!");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 设置进度过程中的提示文本
|
||
/// </summary>
|
||
public void SetProgressMessage(string message)
|
||
{
|
||
progressMessage = message;
|
||
Debug.Log($"ProgressBarController: 设置进度提示为 '{message}'");
|
||
}
|
||
|
||
/// <summary>
|
||
/// 设置完成提示文本
|
||
/// </summary>
|
||
public void SetCompletionMessage(string message)
|
||
{
|
||
completionMessage = message;
|
||
Debug.Log($"ProgressBarController: 设置完成提示为 '{message}'");
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取当前进度值
|
||
/// </summary>
|
||
public float GetCurrentProgress()
|
||
{
|
||
return currentProgress;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 检查是否正在运行
|
||
/// </summary>
|
||
public bool IsProgressing()
|
||
{
|
||
return isProgressing;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 组件销毁时清理
|
||
/// </summary>
|
||
private void OnDestroy()
|
||
{
|
||
if (progressCoroutine != null)
|
||
{
|
||
StopCoroutine(progressCoroutine);
|
||
}
|
||
}
|
||
}
|