123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine.Events;
- using UnityEngine.EventSystems;
- namespace Rokid.MRC
- {
- //基类
- public abstract class Tweener : MonoBehaviour
- {
- static public Tweener current;
- public enum Direction
- {
- eReverse = -1,
- eToggle = 0,
- eForward = 1
- }
- public enum Trigger
- {
- eOnPointerEnter,
- eOnPointerDown,
- eOnPointerClick,
- eOnPointerUp,
- eOnPointerExit,
- None
- }
- public enum ShakeType
- {
- ePosition,
- eScale,
- eRotation
- }
- public iTween.EaseType method = iTween.EaseType.linear;
- public iTween.LoopType style = iTween.LoopType.none;
- public AnimationCurve animationCurve = new AnimationCurve(new Keyframe(0f, 0f, 0f, 1f), new Keyframe(1f, 1f, 1f, 0f));
- public bool ignoreTimeScale = true;
- public float delay = 0f;
- public float duration = 1f;
- public float fixedDelta = 0f;
- public int tweenGroup = 0;
- private UnityAction onFinished = null;
- private UnityAction onValueChange = null;
- //[HideInInspector]
- //protected GameObject eventReceiver;
- //[HideInInspector]
- //protected string callWhenFinished;
- private bool mStarted = false;
- private float mStartTime = 0f;
- private float mDuration = 0f;
- private float mAmountPerDelta = 1000f;
- private float mFactor = 0f;
- public bool currentDirection
- {
- get
- {
- return mFactor > 0;
- }
- }
- /// <summary>
- /// Amount advanced per delta time.
- /// </summary>
- protected float amountPerDelta
- {
- get
- {
- if(mDuration != duration)
- {
- mDuration = duration;
- mAmountPerDelta = Mathf.Abs((duration > 0f) ? 1f / duration : 1000f) * Mathf.Sign(mAmountPerDelta);
- }
- return mAmountPerDelta;
- }
- }
- protected void Reset()
- {
- if(!mStarted)
- {
- SetStartToCurrentValue();
- SetEndToCurrentValue();
- }
- }
- protected virtual void Start()
- {
- Update();
- }
- //实时进行缓动
- void Update()
- {
- float delta = ignoreTimeScale ? Time.unscaledDeltaTime : Time.deltaTime;
- float time = ignoreTimeScale ? Time.unscaledTime : Time.time;
- if(fixedDelta > float.Epsilon)
- {
- delta = fixedDelta;
- }
- if(!mStarted)
- {
- mStarted = true;
- mStartTime = time + delay;
- }
- if(time < mStartTime)
- return;
- // Advance the sampling factor
- mFactor += amountPerDelta * delta;
- // Loop style simply resets the play factor after it exceeds 1.
- if(style == iTween.LoopType.loop)
- {
- if(mFactor > 1f)
- {
- mFactor -= Mathf.Floor(mFactor);
- }
- }
- else if(style == iTween.LoopType.pingPong)
- {
- // Ping-pong style reverses the direction
- if(mFactor > 1f)
- {
- mFactor = 1f - (mFactor - Mathf.Floor(mFactor));
- mAmountPerDelta = -mAmountPerDelta;
- }
- else if(mFactor < 0f)
- {
- mFactor = -mFactor;
- mFactor -= Mathf.Floor(mFactor);
- mAmountPerDelta = -mAmountPerDelta;
- }
- }
- // If the factor goes out of range and this is a one-time tweening operation, disable the script
- if((style == iTween.LoopType.none) && (duration == 0f || mFactor > 1f || mFactor < 0f))
- {
- mFactor = Mathf.Clamp01(mFactor);
- Sample(mFactor, true);
- enabled = false;
- if(current != this)
- {
- Tweener before = current;
- current = this;
- if(onFinished != null)
- onFinished();
- // Deprecated legacy functionality support
- //if (eventReceiver != null && !string.IsNullOrEmpty(callWhenFinished))
- // eventReceiver.SendMessage(callWhenFinished, this, SendMessageOptions.DontRequireReceiver);
- current = before;
- }
- }
- else
- Sample(mFactor, false);
- }
- public void SetOnFinished(UnityAction finishedCallBack)
- {
- onFinished = finishedCallBack;
- }
- public void AddOnFinished(UnityAction finishedCallBack)
- {
- onFinished += finishedCallBack;
- }
- public void RemoveOnFinished(UnityAction finishedCallBack)
- {
- if(onFinished != null)
- onFinished -= finishedCallBack;
- }
- void OnDisable()
- {
- mStarted = false;
- }
- /// <summary>
- /// Sample the tween at the specified factor.
- /// </summary>
- protected void Sample(float factor, bool isFinished)
- {
- // Calculate the sampling value
- float val = Mathf.Clamp01(factor);
- val = (method == iTween.EaseType.None) ? animationCurve.Evaluate(val) : iTween.easeValue(method, 0, 1, val);
- // Call the virtual update
- OnUpdate((method == iTween.EaseType.None) ? animationCurve.Evaluate(val) : val, isFinished);
- if(onValueChange != null)
- {
- onValueChange.Invoke();
- }
- }
- /// <summary>
- /// Manually activate the tweening process, reversing it if necessary.
- /// true Play the tween forward.
- /// false Play the tween in reverse.
- /// </summary>
- public void Play(bool forward)
- {
- mAmountPerDelta = Mathf.Abs(amountPerDelta);
- if(!forward)
- mAmountPerDelta = -mAmountPerDelta;
- enabled = true;
- Update();
- }
- /// <summary>
- /// Manually reset the tweener's state to the beginning.
- /// If the tween is playing forward, this means the tween's start.
- /// If the tween is playing in reverse, this means the tween's end.
- /// </summary>
- public void ResetToBeginning()
- {
- mStarted = false;
- mFactor = (amountPerDelta < 0f) ? 1f : 0f;
- Sample(mFactor, false);
- }
- /// <summary>
- /// Manually start the tweening process, reversing its direction.
- /// </summary>
- public void Toggle()
- {
- if(mFactor > 0f)
- {
- mAmountPerDelta = -amountPerDelta;
- }
- else
- {
- mAmountPerDelta = Mathf.Abs(amountPerDelta);
- }
- enabled = true;
- }
- /// <summary>
- /// Actual tweening logic should go here.
- /// </summary>
- abstract protected void OnUpdate(float factor, bool isFinished);
- /// <summary>
- /// Starts the tweening operation.
- /// </summary>
- static protected T Begin<T>(GameObject go, float duration) where T : Tweener
- {
- T comp = go.GetComponent<T>();
- #if UNITY_FLASH
- if ((object)comp == null) comp = (T)go.AddComponent<T>();
- #else
- // Find the tween with an unset group ID (group ID of 0).
- if(comp != null && comp.tweenGroup != 0)
- {
- comp = null;
- T[] comps = go.GetComponents<T>();
- for(int i = 0, imax = comps.Length;i < imax;++i)
- {
- comp = comps[i];
- if(comp != null && comp.tweenGroup == 0)
- break;
- comp = null;
- }
- }
- if(comp == null)
- {
- comp = go.AddComponent<T>();
- if(comp == null)
- {
- Debug.LogError("Unable to add " + typeof(T) + " to " + go);
- return null;
- }
- }
- #endif
- comp.mStarted = false;
- comp.duration = duration;
- comp.mFactor = 0f;
- comp.mAmountPerDelta = Mathf.Abs(comp.amountPerDelta);
- comp.style = iTween.LoopType.none;
- comp.animationCurve = new AnimationCurve(new Keyframe(0f, 0f, 0f, 1f), new Keyframe(1f, 1f, 1f, 0f));
- //comp.eventReceiver = null;
- //comp.callWhenFinished = null;
- comp.onFinished = null;
- comp.enabled = true;
- return comp;
- }
- protected virtual void SetStartToCurrentValue()
- {
- }
- protected virtual void SetEndToCurrentValue()
- {
- }
- protected virtual void SetCurrentValueToStart()
- {
- }
- protected virtual void SetCurrentValueToEnd()
- {
- }
- }
- }
-
|