123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405 |
- using System;
- using System.Collections.Generic;
- using AnimationOrTween;
- using UnityEngine;
- public abstract class UITweener : MonoBehaviour
- {
- public float amountPerDelta
- {
- get
- {
- if (this.mDuration != this.duration)
- {
- this.mDuration = this.duration;
- this.mAmountPerDelta = Mathf.Abs((this.duration <= 0f) ? 1000f : (1f / this.duration)) * Mathf.Sign(this.mAmountPerDelta);
- }
- return this.mAmountPerDelta;
- }
- }
- public float tweenFactor
- {
- get
- {
- return this.mFactor;
- }
- set
- {
- this.mFactor = Mathf.Clamp01(value);
- }
- }
- public Direction direction
- {
- get
- {
- return (this.amountPerDelta >= 0f) ? Direction.Forward : Direction.Reverse;
- }
- }
- private void Reset()
- {
- if (!this.mStarted)
- {
- this.SetStartToCurrentValue();
- this.SetEndToCurrentValue();
- }
- }
- protected virtual void Start()
- {
- this.Update();
- }
- private void Update()
- {
- float num = (!this.ignoreTimeScale) ? Time.deltaTime : RealTime.deltaTime;
- float num2 = (!this.ignoreTimeScale) ? Time.time : RealTime.time;
- if (!this.mStarted)
- {
- this.mStarted = true;
- this.mStartTime = num2 + this.delay;
- }
- if (num2 < this.mStartTime)
- {
- return;
- }
- this.mFactor += this.amountPerDelta * num;
- if (this.style == UITweener.Style.Loop)
- {
- if (this.mFactor > 1f)
- {
- this.mFactor -= Mathf.Floor(this.mFactor);
- }
- }
- else if (this.style == UITweener.Style.PingPong)
- {
- if (this.mFactor > 1f)
- {
- this.mFactor = 1f - (this.mFactor - Mathf.Floor(this.mFactor));
- this.mAmountPerDelta = -this.mAmountPerDelta;
- }
- else if (this.mFactor < 0f)
- {
- this.mFactor = -this.mFactor;
- this.mFactor -= Mathf.Floor(this.mFactor);
- this.mAmountPerDelta = -this.mAmountPerDelta;
- }
- }
- if (this.style == UITweener.Style.Once && (this.duration == 0f || this.mFactor > 1f || this.mFactor < 0f))
- {
- this.mFactor = Mathf.Clamp01(this.mFactor);
- this.Sample(this.mFactor, true);
- if (this.duration == 0f || (this.mFactor == 1f && this.mAmountPerDelta > 0f) || (this.mFactor == 0f && this.mAmountPerDelta < 0f))
- {
- base.enabled = false;
- }
- if (UITweener.current == null)
- {
- UITweener.current = this;
- if (this.onFinished != null)
- {
- this.mTemp = this.onFinished;
- this.onFinished = new List<EventDelegate>();
- EventDelegate.Execute(this.mTemp);
- for (int i = 0; i < this.mTemp.Count; i++)
- {
- EventDelegate eventDelegate = this.mTemp[i];
- if (eventDelegate != null && !eventDelegate.oneShot)
- {
- EventDelegate.Add(this.onFinished, eventDelegate, eventDelegate.oneShot);
- }
- }
- this.mTemp = null;
- }
- if (this.eventReceiver != null && !string.IsNullOrEmpty(this.callWhenFinished))
- {
- this.eventReceiver.SendMessage(this.callWhenFinished, this, SendMessageOptions.DontRequireReceiver);
- }
- UITweener.current = null;
- }
- }
- else
- {
- this.Sample(this.mFactor, false);
- }
- }
- public void SetOnFinished(EventDelegate.Callback del)
- {
- EventDelegate.Set(this.onFinished, del);
- }
- public void SetOnFinished(EventDelegate del)
- {
- EventDelegate.Set(this.onFinished, del);
- }
- public void AddOnFinished(EventDelegate.Callback del)
- {
- EventDelegate.Add(this.onFinished, del);
- }
- public void AddOnFinished(EventDelegate del)
- {
- EventDelegate.Add(this.onFinished, del);
- }
- public void RemoveOnFinished(EventDelegate del)
- {
- if (this.onFinished != null)
- {
- this.onFinished.Remove(del);
- }
- if (this.mTemp != null)
- {
- this.mTemp.Remove(del);
- }
- }
- private void OnDisable()
- {
- this.mStarted = false;
- }
- public void Sample(float factor, bool isFinished)
- {
- float num = Mathf.Clamp01(factor);
- if (this.method == UITweener.Method.EaseIn)
- {
- num = 1f - Mathf.Sin(1.57079637f * (1f - num));
- if (this.steeperCurves)
- {
- num *= num;
- }
- }
- else if (this.method == UITweener.Method.EaseOut)
- {
- num = Mathf.Sin(1.57079637f * num);
- if (this.steeperCurves)
- {
- num = 1f - num;
- num = 1f - num * num;
- }
- }
- else if (this.method == UITweener.Method.EaseInOut)
- {
- num -= Mathf.Sin(num * 6.28318548f) / 6.28318548f;
- if (this.steeperCurves)
- {
- num = num * 2f - 1f;
- float num2 = Mathf.Sign(num);
- num = 1f - Mathf.Abs(num);
- num = 1f - num * num;
- num = num2 * num * 0.5f + 0.5f;
- }
- }
- else if (this.method == UITweener.Method.BounceIn)
- {
- num = this.BounceLogic(num);
- }
- else if (this.method == UITweener.Method.BounceOut)
- {
- num = 1f - this.BounceLogic(1f - num);
- }
- this.OnUpdate((this.animationCurve == null) ? num : this.animationCurve.Evaluate(num), isFinished);
- }
- private float BounceLogic(float val)
- {
- if (val < 0.363636f)
- {
- val = 7.5685f * val * val;
- }
- else if (val < 0.727272f)
- {
- val = 7.5625f * (val -= 0.545454f) * val + 0.75f;
- }
- else if (val < 0.90909f)
- {
- val = 7.5625f * (val -= 0.818181f) * val + 0.9375f;
- }
- else
- {
- val = 7.5625f * (val -= 0.9545454f) * val + 0.984375f;
- }
- return val;
- }
- [Obsolete("Use PlayForward() instead")]
- public void Play()
- {
- this.Play(true);
- }
- public void PlayForward()
- {
- this.Play(true);
- }
- public void PlayReverse()
- {
- this.Play(false);
- }
- public void Play(bool forward)
- {
- this.mAmountPerDelta = Mathf.Abs(this.amountPerDelta);
- if (!forward)
- {
- this.mAmountPerDelta = -this.mAmountPerDelta;
- }
- base.enabled = true;
- this.Update();
- }
- public void ResetToBeginning()
- {
- this.mStarted = false;
- this.mFactor = ((this.amountPerDelta >= 0f) ? 0f : 1f);
- this.Sample(this.mFactor, false);
- }
- public void Toggle()
- {
- if (this.mFactor > 0f)
- {
- this.mAmountPerDelta = -this.amountPerDelta;
- }
- else
- {
- this.mAmountPerDelta = Mathf.Abs(this.amountPerDelta);
- }
- base.enabled = true;
- }
- protected abstract void OnUpdate(float factor, bool isFinished);
- public static T Begin<T>(GameObject go, float duration) where T : UITweener
- {
- T t = go.GetComponent<T>();
- if (t != null && t.tweenGroup != 0)
- {
- t = (T)((object)null);
- T[] components = go.GetComponents<T>();
- int i = 0;
- int num = components.Length;
- while (i < num)
- {
- t = components[i];
- if (t != null && t.tweenGroup == 0)
- {
- break;
- }
- t = (T)((object)null);
- i++;
- }
- }
- if (t == null)
- {
- t = go.AddComponent<T>();
- if (t == null)
- {
- Debug.LogError(string.Concat(new object[]
- {
- "Unable to add ",
- typeof(T),
- " to ",
- NGUITools.GetHierarchy(go)
- }), go);
- return (T)((object)null);
- }
- }
- t.mStarted = false;
- t.duration = duration;
- t.mFactor = 0f;
- t.mAmountPerDelta = Mathf.Abs(t.amountPerDelta);
- t.style = UITweener.Style.Once;
- t.animationCurve = new AnimationCurve(new Keyframe[]
- {
- new Keyframe(0f, 0f, 0f, 1f),
- new Keyframe(1f, 1f, 1f, 0f)
- });
- t.eventReceiver = null;
- t.callWhenFinished = null;
- t.enabled = true;
- return t;
- }
- public virtual void SetStartToCurrentValue()
- {
- }
- public virtual void SetEndToCurrentValue()
- {
- }
- public static UITweener current;
- [HideInInspector]
- public UITweener.Method method;
- [HideInInspector]
- public UITweener.Style style;
- [HideInInspector]
- public AnimationCurve animationCurve = new AnimationCurve(new Keyframe[]
- {
- new Keyframe(0f, 0f, 0f, 1f),
- new Keyframe(1f, 1f, 1f, 0f)
- });
- [HideInInspector]
- public bool ignoreTimeScale = true;
- [HideInInspector]
- public float delay;
- [HideInInspector]
- public float duration = 1f;
- [HideInInspector]
- public bool steeperCurves;
- [HideInInspector]
- public int tweenGroup;
- [HideInInspector]
- public List<EventDelegate> onFinished = new List<EventDelegate>();
- [HideInInspector]
- public GameObject eventReceiver;
- [HideInInspector]
- public string callWhenFinished;
- private bool mStarted;
- private float mStartTime;
- private float mDuration;
- private float mAmountPerDelta = 1000f;
- private float mFactor;
- private List<EventDelegate> mTemp;
- public enum Method
- {
- Linear,
- EaseIn,
- EaseOut,
- EaseInOut,
- BounceIn,
- BounceOut
- }
- public enum Style
- {
- Once,
- Loop,
- PingPong
- }
- }
|