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 onIncrement; public System.Action onComplete; } /// /// 开始精确累加 /// public static async void StartAccumulation(int targetTotal, float duration, int steps, System.Action 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(); } /// /// 开始实时累加(基于Update) /// 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(); } } }