async UniTask ApplyVolumeTransition(BgmReference bgm, float maxVolume, AnimationCurveSO curve) { var cancellationToken = bgm.CreateTransitionCt(); await Curve.EvaluateAsync(curve.Value, OnVolumeChanged, ct : cancellationToken); void OnVolumeChanged(float volume) { if (bgm != null && bgm.AudioSource != null) { bgm.AudioSource.volume = volume * maxVolume; } } }
async UniTask StopTrackAsync(BgmReference bgm, AnimationCurveSO outgoingTransition = null) { // Cancel any transitions if not complete bgm.CancelVolumeTransition(); // Remove from referencable list audioReferences.Remove(bgm); if (outgoingTransition != null) { //BUG: If a track is fading in at this time, it will instantly play at "MaxVolume" before fading out. await ApplyVolumeTransition(bgm, bgm.AudioSO.Details.MaxVolume, outgoingTransition); } bgm.AudioSO.Unload(); // Destroy when outgoing transition is complete Destroy(bgm.GameObject); }