/// <summary> /// Coroutine used to make the character's sprite flicker (when hurt for example). /// </summary> public static IEnumerator Flicker(Renderer renderer, Color initialColor, Color flickerColor, float flickerSpeed, float flickerDuration) { if (renderer == null) { yield break; } if (!renderer.material.HasProperty("_Color")) { yield break; } if (initialColor == flickerColor) { yield break; } float flickerStop = Time.time + flickerDuration; while (Time.time < flickerStop) { renderer.material.color = flickerColor; yield return(MMCoroutine.WaitFor(flickerSpeed)); renderer.material.color = initialColor; yield return(MMCoroutine.WaitFor(flickerSpeed)); } renderer.material.color = initialColor; }
/// <summary> /// Waits for BeforeExitFadeDelay seconds /// </summary> /// <returns></returns> protected virtual IEnumerator ProcessDelayBeforeExitFade() { if (_beforeExitFadeDelay > 0f) { MMLoadingSceneDebug("MMLoadingSceneManagerAdditive : delay before exit fade, duration : " + _beforeExitFadeDelay); MMSceneLoadingManager.LoadingSceneEvent.Trigger(_sceneToLoadName, MMSceneLoadingManager.LoadingStatus.BeforeExitFade); OnBeforeExitFade?.Invoke(); yield return(MMCoroutine.WaitFor(_beforeExitFadeDelay)); } }
/// <summary> /// Waits for the specified AfterEntryFadeDelay /// </summary> /// <returns></returns> protected virtual IEnumerator ProcessDelayAfterEntryFade() { if (_afterEntryFadeDelay > 0f) { MMLoadingSceneDebug("MMLoadingSceneManagerAdditive : delay after entry fade, duration : " + _afterEntryFadeDelay); MMSceneLoadingManager.LoadingSceneEvent.Trigger(_sceneToLoadName, MMSceneLoadingManager.LoadingStatus.AfterEntryFade); OnAfterEntryFade?.Invoke(); yield return(MMCoroutine.WaitFor(_afterEntryFadeDelay)); } }
/// <summary> /// Waits and triggers state change and events /// </summary> protected virtual IEnumerator TimedActivationSequence() { if (DelayMode == DelayModes.Time) { yield return(MMCoroutine.WaitFor(TimeBeforeStateChange)); } else { yield return(StartCoroutine(MMCoroutine.WaitForFrames(FrameCount))); } StateChange(); Activate(); }
/// <summary> /// Destroys the object after TimeBeforeDestruction seconds /// </summary> protected virtual IEnumerator Destruction() { yield return(MMCoroutine.WaitFor(TimeBeforeDestruction)); if (TimeDestructionMode == TimedDestructionModes.Destroy) { Destroy(gameObject); } else { gameObject.SetActive(false); } }
/// <summary> /// Disables an audio source after it's done playing /// </summary> /// <param name="duration"></param> /// <param name="targetObject"></param> /// <returns></returns> public virtual IEnumerator AutoDisableAudioSource(float duration, AudioSource source, AudioClip clip, bool doNotAutoRecycleIfNotDonePlaying) { yield return(MMCoroutine.WaitFor(duration)); if (source.clip != clip) { yield break; } if (doNotAutoRecycleIfNotDonePlaying) { while (source.time < source.clip.length) { yield return(null); } } source.gameObject.SetActive(false); }
/// <summary> /// Requests a fade on exit /// </summary> /// <returns></returns> protected virtual IEnumerator ExitFade() { SetAudioListener(false); if (_exitFadeDuration > 0f) { MMLoadingSceneDebug("MMLoadingSceneManagerAdditive : exit fade, duration : " + _exitFadeDuration); MMSceneLoadingManager.LoadingSceneEvent.Trigger(_sceneToLoadName, MMSceneLoadingManager.LoadingStatus.ExitFade); OnExitFade?.Invoke(); if (_fadeMode == FadeModes.FadeOutThenIn) { MMFadeInEvent.Trigger(_exitFadeDuration, _exitFadeTween, FaderID, true); } else { MMFadeOutEvent.Trigger(_exitFadeDuration, _exitFadeTween, FaderID, true); } yield return(MMCoroutine.WaitFor(_exitFadeDuration)); } }
/// <summary> /// Hides the bar when it reaches zero /// </summary> /// <returns>The hide bar.</returns> protected virtual IEnumerator FinalHideBar() { _finalHideStarted = true; if (InstantiatedOnDeath != null) { Instantiate(InstantiatedOnDeath, this.transform.position + HealthBarOffset, this.transform.rotation); } if (HideBarAtZeroDelay == 0) { _showBar = false; _progressBar.gameObject.SetActive(false); yield return(null); } else { yield return(MMCoroutine.WaitFor(HideBarAtZeroDelay)); _showBar = false; _progressBar.gameObject.SetActive(false); } }
/// <summary> /// A coroutine used to toggle the menu /// </summary> /// <param name="active"></param> /// <returns></returns> protected virtual IEnumerator ToggleCo(bool active) { if (_toggling) { yield break; } if (!active) { _containerRect.gameObject.SetActive(true); } _toggling = true; Active = active; _newPosition = active ? _offPosition : _initialContainerPosition; MMTween.MoveRectTransform(this, _containerRect, _containerRect.localPosition, _newPosition, null, 0f, Data.ToggleDuration, Data.ToggleCurve); yield return(MMCoroutine.WaitFor(Data.ToggleDuration)); if (active) { _containerRect.gameObject.SetActive(false); } Active = !active; _toggling = false; }
/// <summary> /// Starts a fade /// </summary> /// <param name="fadingIn"></param> /// <param name="duration"></param> /// <param name="curve"></param> /// <param name="id"></param> /// <param name="ignoreTimeScale"></param> /// <param name="worldPosition"></param> protected virtual IEnumerator StartFading(bool fadingIn, float duration, MMTweenType curve, int id, bool ignoreTimeScale, Vector3 worldPosition) { if (id != ID) { yield break; } if (InitialDelay > 0f) { yield return(MMCoroutine.WaitFor(InitialDelay)); } if (!_initialized) { Initialization(); } if (curve == null) { curve = DefaultTween; } IgnoreTimescale = ignoreTimeScale; EnableFader(); _fading = true; _fadeStartedAt = IgnoreTimescale ? Time.unscaledTime : Time.time; _currentCurve = curve; _currentDuration = duration; _fromPosition = _rectTransform.anchoredPosition; _toPosition = fadingIn ? _initialPosition : ExitPosition(); _newPosition = MMTween.Tween(0f, 0f, duration, _fromPosition, _toPosition, _currentCurve); _rectTransform.anchoredPosition = _newPosition; }
/// <summary> /// Plays a new song in the playlist, and stops / fades the previous one /// </summary> /// <param name="index"></param> /// <returns></returns> protected virtual IEnumerator PlaySong(int index) { // if we don't have a song, we stop if (Songs.Count == 0) { yield break; } // if we've played all our songs, we stop if (!Endless && (_songsPlayedThisCycle > Songs.Count)) { yield break; } if (_coroutine != null) { StopCoroutine(_coroutine); } // we stop our current song if ((PlaylistState.CurrentState == PlaylistStates.Playing) && (index >= 0 && index < Songs.Count)) { StartCoroutine(Fade(CurrentlyPlayingIndex, Random.Range(Songs[index].CrossFadeDuration.x, Songs[index].CrossFadeDuration.y), Songs[CurrentlyPlayingIndex].Volume.y * VolumeMultiplier, Songs[CurrentlyPlayingIndex].Volume.x * VolumeMultiplier, true)); } // we stop all other coroutines if ((CurrentlyPlayingIndex >= 0) && (Songs.Count > CurrentlyPlayingIndex)) { foreach (MMPlaylistSong song in Songs) { if (song != Songs[CurrentlyPlayingIndex]) { song.Fading = false; } } } if (index < 0 || index >= Songs.Count) { yield break; } // initial delay yield return(MMCoroutine.WaitFor(Random.Range(Songs[index].InitialDelay.x, Songs[index].InitialDelay.y))); if (Songs[index].TargetAudioSource == null) { Debug.LogError(this.name + " : the playlist song you're trying to play is null"); yield break; } Songs[index].TargetAudioSource.pitch = Random.Range(Songs[index].Pitch.x, Songs[index].Pitch.y); Songs[index].TargetAudioSource.panStereo = Songs[index].StereoPan; Songs[index].TargetAudioSource.spatialBlend = Songs[index].SpatialBlend; Songs[index].TargetAudioSource.loop = Songs[index].Loop; // fades the new song's volume StartCoroutine(Fade(index, Random.Range(Songs[index].CrossFadeDuration.x, Songs[index].CrossFadeDuration.y), Songs[index].Volume.x * VolumeMultiplier, Songs[index].Volume.y * VolumeMultiplier, false)); // starts the new song Songs[index].TargetAudioSource.Play(); // updates our state CurrentSongName = Songs[index].TargetAudioSource.clip.name; PlaylistState.ChangeState(PlaylistStates.Playing); Songs[index].Playing = true; CurrentlyPlayingIndex = index; _songsPlayedSoFar++; _songsPlayedThisCycle++; while (Songs[index].TargetAudioSource.isPlaying) { yield return(null); } if (PlaylistState.CurrentState != PlaylistStates.Playing) { yield break; } if (_songsPlayedSoFar < Songs.Count) { _coroutine = StartCoroutine(PlaySong(PickNextIndex())); } else { if (Endless) { _coroutine = StartCoroutine(PlaySong(PickNextIndex())); } else { PlaylistState.ChangeState(PlaylistStates.Idle); } } }
/// <summary> /// Disables an audio source after it's done playing /// </summary> /// <param name="duration"></param> /// <param name="targetObject"></param> /// <returns></returns> public virtual IEnumerator AutoDisableAudioSource(float duration, GameObject targetObject) { yield return(MMCoroutine.WaitFor(duration)); targetObject.SetActive(false); }
/// <summary> /// An internal coroutine used to handle the disabling of the progress bar after a delay /// </summary> /// <param name="delay"></param> /// <returns></returns> protected virtual IEnumerator HideBarCo(float delay) { yield return(MMCoroutine.WaitFor(delay)); this.gameObject.SetActive(false); }
/// <summary> /// Plays a new track in the playlist, and stops / fades the previous one /// </summary> /// <param name="index"></param> /// <returns></returns> protected virtual IEnumerator PlayTrack(int index) { // if we don't have a song, we stop if (Songs.Count == 0) { yield break; } // if we've played all our songs, we stop if (!Endless && (_songsPlayedThisCycle > Songs.Count)) { yield break; } // we stop our current track if (PlaylistState.CurrentState == PlaylistStates.Playing) { StartCoroutine(Fade(CurrentlyPlayingIndex, Random.Range(Songs[index].CrossFadeDuration.x, Songs[index].CrossFadeDuration.y), Songs[CurrentlyPlayingIndex].Volume.y, Songs[CurrentlyPlayingIndex].Volume.x, true)); } // we stop all other coroutines if (CurrentlyPlayingIndex >= 0) { foreach (MMPlaylistSong song in Songs) { if (song != Songs[CurrentlyPlayingIndex]) { song.Fading = false; } } } // initial delay yield return(MMCoroutine.WaitFor(Random.Range(Songs[index].InitialDelay.x, Songs[index].InitialDelay.y))); if (Songs[index].TargetAudioSource == null) { Debug.LogError(this.name + " : the playlist song you're trying to play is null"); yield break; } Songs[index].TargetAudioSource.pitch = Random.Range(Songs[index].Pitch.x, Songs[index].Pitch.y); Songs[index].TargetAudioSource.panStereo = Songs[index].StereoPan; Songs[index].TargetAudioSource.spatialBlend = Songs[index].SpatialBlend; Songs[index].TargetAudioSource.loop = Songs[index].Loop; // fades the new track's volume StartCoroutine(Fade(index, Random.Range(Songs[index].CrossFadeDuration.x, Songs[index].CrossFadeDuration.y), Songs[index].Volume.x, Songs[index].Volume.y, false)); // starts the new track Songs[index].TargetAudioSource.Play(); // updates our state CurrentTrackName = Songs[index].TargetAudioSource.clip.name; PlaylistState.ChangeState(PlaylistStates.Playing); Songs[index].Playing = true; CurrentlyPlayingIndex = index; _songsPlayedSoFar++; _songsPlayedThisCycle++; }