123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- 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<float, bool> onUpdateFadeIn = null, Action onCompletedFadeIn = null, Func<float, bool> 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<float, bool> onUpdateFadeIn = null, Action onCompletedFadeIn = null, Func<float, bool> 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<float, bool> 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<float, bool> 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<float, bool> 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<float, bool> 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
- }
- }
|