using System; using System.Collections; using UnityEngine; public abstract class BaseFader : IFade { public static bool StartCrossFade(IFade fadeIn, IFade fadeOut, float time, bool skippable = true, Func onUpdateFadeIn = null, Action onCompletedFadeIn = null, Func onUpdateFadeOut = null, Action onCompletedFadeOut = null) { if (fadeIn == null || fadeOut == null || fadeIn.isShow || fadeOut.isHide || fadeIn == fadeOut) { return false; } bool flag = fadeIn.StartFade(BaseFader.FadeType.In, time, skippable, onUpdateFadeIn, onCompletedFadeIn); return flag | fadeOut.StartFade(BaseFader.FadeType.Out, time, skippable, onUpdateFadeOut, onCompletedFadeOut); } public static bool StartCrossFade(IFade fadeIn, IFade fadeOut, bool skippable = true, Func onUpdateFadeIn = null, Action onCompletedFadeIn = null, Func onUpdateFadeOut = null, Action onCompletedFadeOut = null) { if (fadeIn == null || fadeOut == null || fadeIn.isShow || fadeOut.isHide || fadeIn == fadeOut) { return false; } bool flag = fadeIn.StartFade(BaseFader.FadeType.In, skippable, onUpdateFadeIn, onCompletedFadeIn); return flag | fadeOut.StartFade(BaseFader.FadeType.Out, skippable, onUpdateFadeOut, onCompletedFadeOut); } public static float DefaultFadeTime { get { return 0.5f; } } public virtual float customDefaultFadeTime { get { return this.customDefaultFadeTime_; } set { this.customDefaultFadeTime_ = value; } } public BaseFader.FadeState state { get; protected set; } public virtual bool isShow { get { return this.state == BaseFader.FadeState.Show || this.state == BaseFader.FadeState.FadeInNow; } } public virtual bool isHide { get { return this.state == BaseFader.FadeState.Hide || this.state == BaseFader.FadeState.FadeOutNow; } } public virtual bool isFadeNow { get { return this.state == BaseFader.FadeState.FadeInNow || this.state == BaseFader.FadeState.FadeOutNow; } } BaseFader.FadeState IFade.state { get { return this.state; } } float IFade.defaultFadeTime { get { return this.customDefaultFadeTime; } } protected MonoBehaviour coroutineInstant { get { return GameMain.Instance; } } public bool StartFade(BaseFader.FadeType type, bool skippable = true, Func onUpdate = null, Action onCompleted = null) { return this.StartFade(type, this.customDefaultFadeTime, skippable, onUpdate, onCompleted); } public virtual bool StartFade(BaseFader.FadeType type, float time, bool skippable = true, Func onUpdate = null, Action onCompleted = null) { if (type == BaseFader.FadeType.In) { if (this.isShow) { return false; } this.StartFade(true, time, skippable, onUpdate, onCompleted); } else { if (this.isHide) { return false; } this.StartFade(false, time, skippable, onUpdate, onCompleted); } return true; } public virtual void FadeComplete() { if (this.state == BaseFader.FadeState.FadeOutNow) { if (this.fadeCoroutine != null) { this.coroutineInstant.StopCoroutine(this.fadeCoroutine); } this.fadeCoroutine = null; this.ApplyFadeValue(0f); this.state = BaseFader.FadeState.Hide; if (this.onCompleted != null) { this.onCompleted(); } this.onCompleted = null; } else if (this.state == BaseFader.FadeState.FadeInNow) { if (this.fadeCoroutine != null) { this.coroutineInstant.StopCoroutine(this.fadeCoroutine); } this.fadeCoroutine = null; this.ApplyFadeValue(1f); this.state = BaseFader.FadeState.Show; if (this.onCompleted != null) { this.onCompleted(); } this.onCompleted = null; } } protected virtual void StartFade(bool typeIn, float time, bool skippable, Func onUpdate, Action onCompleted) { if (typeIn && this.state == BaseFader.FadeState.FadeOutNow) { if (this.fadeCoroutine != null) { this.coroutineInstant.StopCoroutine(this.fadeCoroutine); } this.fadeCoroutine = null; this.OnAbortFadeOut(); } else if (!typeIn && this.state == BaseFader.FadeState.FadeInNow) { if (this.fadeCoroutine != null) { this.coroutineInstant.StopCoroutine(this.fadeCoroutine); } this.fadeCoroutine = null; this.OnAbortFadeIn(); } this.state = ((!typeIn) ? BaseFader.FadeState.FadeOutNow : BaseFader.FadeState.FadeInNow); this.onCompleted = onCompleted; if (typeIn) { this.OnBeforeFadeIn(); } else { this.OnBeforeFadeOut(); } this.fadeCoroutine = this.coroutineInstant.StartCoroutine(this.Fade(this.GetFadeValue(), (!typeIn) ? 0f : 1f, time, skippable, onUpdate, delegate { this.state = ((!typeIn) ? BaseFader.FadeState.Hide : BaseFader.FadeState.Show); if (this.onCompleted != null) { this.onCompleted(); } this.onCompleted = null; if (typeIn) { this.OnAfterFadeIn(); } else { this.OnAfterFadeOut(); } })); } protected virtual IEnumerator Fade(float from, float to, float fadeTime, bool skippable, Func onUpdate, Action onComplete) { if (fadeTime > 0f) { float totalTime = 0f; while (fadeTime >= totalTime) { totalTime += Time.unscaledDeltaTime; float value = Mathf.Lerp(from, to, totalTime / fadeTime); if ((skippable && this.CheckSkipState()) || (onUpdate != null && !onUpdate(value))) { break; } this.ApplyFadeValue(value); yield return null; } } else if (onUpdate != null) { onUpdate(to); } this.ApplyFadeValue(to); if (onComplete != null) { onComplete(); } this.fadeCoroutine = null; yield break; } protected abstract void ApplyFadeValue(float value); protected abstract float GetFadeValue(); protected abstract bool CheckSkipState(); protected virtual void OnBeforeFadeIn() { } protected virtual void OnAfterFadeIn() { } protected virtual void OnBeforeFadeOut() { } protected virtual void OnAfterFadeOut() { } protected virtual void OnAbortFadeIn() { } protected virtual void OnAbortFadeOut() { } protected Action onCompleted; protected Coroutine fadeCoroutine; protected float customDefaultFadeTime_ = BaseFader.DefaultFadeTime; public enum FadeType { In, Out } public enum FadeState { None, Show, Hide, FadeInNow, FadeOutNow } }