using UnityEngine; using System; using System.Collections; public class TweenAudio : Tween { #region 变量 protected float Delta; protected float Origin; protected float Destination; protected AudioSource Target; protected CurveFunctionF Func; #endregion public TweenAudio(AudioSource target, float origin, float destination, float duration, bool originActive, bool destActive, Curve curve) { if (Math.Abs(target.volume - origin) < 0.0005f) { InForward = true; } else if (Math.Abs(target.volume - destination) < 0.0005f) { InBackward = true; } Func = ManaAnim.FunctionDicF[curve]; Target = target; IsForward = false; IsBackward = false; Delta = destination - origin; Origin = origin; Duration = duration; Destination = destination; OnForwardStart += () => { }; OnForwardFinish += () => { }; OnBackwardStart += () => { }; OnBackwardFinish += () => { }; } public override void StartForward() { base.StartForward(); if (IsBackward) { Timer = Duration - Timer; } else { Target.volume = Origin; } } public override void StartBackward() { base.StartBackward(); if (IsForward) { Timer = Duration - Timer; } else { Target.volume = Destination; } } public override bool DoForward() { Timer += Time.fixedDeltaTime; if (Timer > Duration) { Timer = 0; Target.volume = Destination; IsForward = false; InBackward = true; OnForwardFinish.Invoke(); ManaAnim.TweenForList.Remove(this); return true; } else { Target.volume = Func(Timer, Duration, Origin, Delta); return false; } } public override bool DoBackward() { Timer += Time.fixedDeltaTime; if (Timer > Duration) { Timer = 0; Target.volume = Origin; IsBackward = false; InForward = true; OnBackwardFinish.Invoke(); ManaAnim.TweenBacList.Remove(this); return true; } else { Target.volume = Func(Timer, Duration, Destination, -Delta); return false; } } }