123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- using Fort23.Core;
- using UnityEngine;
- namespace Core.UI.UTool
- {
- public class AccumulatorUtility
- {
- public class AccumulationData
- {
- public float currentTotal;
- public float targetTotal;
- public float duration;
- public System.Action<float> onIncrement;
- public System.Action onComplete;
- }
- /// <summary>
- /// 开始精确累加
- /// </summary>
- public static async void StartAccumulation(int targetTotal, float duration, int steps,
- System.Action<int> onStep, System.Action onComplete = null)
- {
- int stepValue = targetTotal / (steps);
- float timePerStep = duration / steps;
- int currentTotal = 0;
- for (int i = 0; i < steps-1; i++)
- {
- await TimerComponent.Instance.WaitAsync((int)(timePerStep * 1000));
- currentTotal += stepValue;
- onStep?.Invoke(stepValue);
- }
- // 确保最终精度
- // currentTotal = targetTotal;
- onStep?.Invoke(targetTotal - currentTotal);
- onComplete?.Invoke();
- }
- /// <summary>
- /// 开始实时累加(基于Update)
- /// </summary>
- public static async void StartRealtimeAccumulation(AccumulationData data)
- {
- float timer = 0f;
- data.currentTotal = 0f;
- while (timer < data.duration)
- {
- timer += Time.deltaTime;
- float progress = timer / data.duration;
- float newTotal = progress * data.targetTotal;
- float increment = newTotal - data.currentTotal;
- if (increment > 0)
- {
- data.currentTotal = newTotal;
- data.onIncrement?.Invoke(data.currentTotal);
- }
- // yield return null;
- }
- data.currentTotal = data.targetTotal;
- data.onIncrement?.Invoke(data.currentTotal);
- data.onComplete?.Invoke();
- }
- }
- }
|