private void TransitBy(AudioTransition transition) { if (transition.Destination != null) { _ActiveTransition = transition; double deltaTime = 0; bool isPlaying = _CurrentState.IsPlaying; if (isPlaying) { float time = _CurrentState.AudioTime; if (_ActiveTransition.WaitForBreakPoint) { float breakPoint = FindNextBreakPoint(time + _ActiveTransition.FadeOut, _CurrentState.BreakPoints, _CurrentState.End); deltaTime = breakPoint - time; } else { deltaTime = Mathf.Min(_ActiveTransition.FadeOut, _CurrentState.End - time); } } if (_NextState != null) { _NextState.Stop(); } _InTransition = true; _NextState = FindState(transition.Destination); float fdeltaTime = System.Convert.ToSingle(deltaTime); _NextStateTW.Begin(fdeltaTime); if (isPlaying) { _CurrentState.EndTime(deltaTime, _ActiveTransition.FadeOut); } if (_NextState.Clip != null) { _NextState.Initialize(_ActiveTransition.FadeIn > AudioState.MINFADETIME); if (_ActiveTransition.CrossFade) { _UpdateNextStateTW.Begin(Mathf.Max(0, fdeltaTime - _ActiveTransition.FadeIn)); _NextState.StartTime(_UpdateNextStateTW.Length, _ActiveTransition.FadeIn); } else { _NextState.StartTime(deltaTime, _ActiveTransition.FadeIn); } } } }
public void FadeTo(string stateName, float fadeIn, float fadeOut, bool crossFade = true) { AudioState state = FindState(stateName); if (state != null && state != _CurrentState) { AudioTransition at = new AudioTransition(); at.Destination = stateName; at.FadeIn = Mathf.Max(fadeIn, 0); at.FadeOut = Mathf.Max(fadeOut, 0); at.WaitForBreakPoint = false; at.CrossFade = crossFade; TransitBy(at); } }
public void Stop() { if (_NextState != null) { _NextState.Stop(); } if (_CurrentState != null) { _CurrentState.Stop(); } foreach (var t in _TriggersMap.Values) { t.Reset(); } _NextStateTW.End(); _ActiveTransition = null; _NextState = null; _CurrentState = FindState(DefaultState); }
protected override void Update() { base.Update(); if (_NextStateTW.IsEnabled) { if (_NextState == null) { _NextStateTW.End(); if (_ActiveTransition != null) { if (_ActiveTransition.TriggerRef != null) { _ActiveTransition.TriggerRef.Reset(); } _ActiveTransition = null; } } if (_NextStateTW.IsEnabledAndOver) { _UpdateNextStateTW.End(); _ScheduleNextStateTW.End(); _NextStateTW.End(); if (_ActiveTransition != null) { if (_ActiveTransition.TriggerRef != null) { _ActiveTransition.TriggerRef.Reset(); } _ActiveTransition = null; } _CurrentState = _NextState; _InTransition = false; _NextState = null; if (_CurrentState.NextState != null) { float deltaTime = _CurrentState.End - _CurrentState.Begin; if (deltaTime > BufferTime) { _ScheduleNextStateTW.Begin(deltaTime); } else { ScheduleNextState(deltaTime); } } } } else if (_ScheduleNextStateTW.IsEnabled) { if (_ScheduleNextStateTW.TimeLeft < BufferTime) { ScheduleNextState(_CurrentState.EndDspTime - AudioSettings.dspTime); _ScheduleNextStateTW.End(); } } if (_CurrentState != null) { _CurrentState.Update(this.VolumeFactor); if (!_InTransition) { if (_CurrentState.Transitions != null) { foreach (var t in _CurrentState.Transitions) { if (t.TriggerRef != null) { if (t.TriggerRef.IsActive) { TransitBy(t); break; } } } } } } if (_UpdateNextStateTW.IsEnabledAndOver && _NextState != null) { _NextState.Update(this.VolumeFactor); } }