Example #1
0
        void Update()
        {
            if (playOnAwake && defaultSong)
            {
                playOnAwake = false;
                PlaySong(defaultSong);
            }

            if (!songStartEventInvoked && songHasStarted && songPosition >= 0)
            {
                songStartEventInvoked = true;
                onSongStart.Invoke();
            }

            // If we need to automatically handle play/pause according to the timescale;
            if (songHasStarted && songStartEventInvoked && autoTimeScalePause)
            {
                if (!songPaused && Time.timeScale == 0)
                {
                    PauseSong();
                }
                else if (songPaused && Time.timeScale == 1)
                {
                    ResumeSong();
                }
            }

            //Sync the tracks position with the audio
            if (!songPaused && songHasStarted)
            {
                songPosition = (float)(AudioSettings.dspTime - dspStartTime - delay - accumulatedPauseTime);

                trackManager.UpdateTrack(songPosition, secPerBeat);

                onSongProgress.Invoke(songPosition);

                if (inverseProgressFill)
                {
                    onSongProgressFill.Invoke(1 - (songPosition / currentSongItem.clip.length));
                }
                else
                {
                    onSongProgressFill.Invoke(songPosition / currentSongItem.clip.length);
                }

                if (songPosition >= 0)
                {
                    if (progressAsPercentage)
                    {
                        onSongProgressDisplay.Invoke(Math.Truncate(songPosition / currentSongItem.clip.length * 100) + "%");
                    }
                    else
                    {
                        var now = new DateTime((long)songPosition * TimeSpan.TicksPerSecond);
                        onSongProgressDisplay.Invoke(now.ToString("mm:ss"));
                    }
                }
            }

            if (songHasStarted && currentSongItem.clip && songPosition >= currentSongItem.clip.length)
            {
                songHasStarted        = false;
                songStartEventInvoked = false;
                resultsScreenObject.SetActive(true);
                resultsScreenObject.GetComponent <StarHandler>().starsAchieved();
                ResultsVinheta.GetComponent <AudioSource>().Play();
                onSongFinished.Invoke();

                trackManager.ClearAllTracks();

                //If its looping, we replay the current song
                if (looping)
                {
                    PlaySong(currentSongItem);
                }
            }
        }