IEnumerator IE_FadeOurProcess(float duration) { var ticker = new TickHelper(); ticker.Reset(duration); yield return(null); while (ticker.Tick()) { _source.volume = ticker.GetRemainTimeInPercent(); yield return(null); } // finish fadeOut duration, call stop Stop(); }
IEnumerator IE_PlayingProcess(AudioCommand cmd) { var ticker = new TickHelper(); // check delay first if (cmd.startCfg.delay > 0) { ticker.Reset(cmd.startCfg.delay); yield return(null); while (ticker.Tick()) { yield return(null); } } // play here _source.Play(); // the fadeIn effect if (cmd.startCfg.fadeInDuration > 0) { // first volumne _source.volume = 0f; // fadein ticker.Reset(cmd.startCfg.fadeInDuration); yield return(null); //NOTES : must have this, if not, some short audio maybe stop before it play while (ticker.Tick()) { _source.volume = ticker.GetWaitedTimeInPercent(); yield return(null); } // final volumn _source.volume = 1f; } // check loop if (cmd.loopCfg.active) { _source.loop = cmd.loopCfg.active; if (cmd.loopCfg.limitLoopCount > 0) { var playTime = _source.clip.length * cmd.loopCfg.limitLoopCount; ticker.Reset(playTime); yield return(null); //NOTES : must have this, if not, some short audio maybe stop before it play while (ticker.Tick()) { yield return(null); } // stop after finish waiting Stop(); } } // not loop, then wait for finised sound, then call Stop, also let is "ready" else { // if clip shorter than this, the player maybe stop before the sound start. const float TOO_SHORT_THRESHOLD = 0.08f; ticker.Reset((_source.clip.length < TOO_SHORT_THRESHOLD) ? TOO_SHORT_THRESHOLD : _source.clip.length); yield return(null); //NOTES : must have this, if not, some short audio maybe stop before it play while (ticker.Tick()) { yield return(null); } Stop(); } }