123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using AnimationOrTween;
- using UnityEngine;
- [AddComponentMenu("NGUI/Internal/Active Animation")]
- public class ActiveAnimation : MonoBehaviour
- {
- private float playbackTime
- {
- get
- {
- return Mathf.Clamp01(this.mAnimator.GetCurrentAnimatorStateInfo(0).normalizedTime);
- }
- }
- public bool isPlaying
- {
- get
- {
- if (!(this.mAnim == null))
- {
- IEnumerator enumerator = this.mAnim.GetEnumerator();
- try
- {
- while (enumerator.MoveNext())
- {
- object obj = enumerator.Current;
- AnimationState animationState = (AnimationState)obj;
- if (this.mAnim.IsPlaying(animationState.name))
- {
- if (this.mLastDirection == Direction.Forward)
- {
- if (animationState.time < animationState.length)
- {
- return true;
- }
- }
- else
- {
- if (this.mLastDirection != Direction.Reverse)
- {
- return true;
- }
- if (animationState.time > 0f)
- {
- return true;
- }
- }
- }
- }
- }
- finally
- {
- IDisposable disposable;
- if ((disposable = (enumerator as IDisposable)) != null)
- {
- disposable.Dispose();
- }
- }
- return false;
- }
- if (this.mAnimator != null)
- {
- if (this.mLastDirection == Direction.Reverse)
- {
- if (this.playbackTime == 0f)
- {
- return false;
- }
- }
- else if (this.playbackTime == 1f)
- {
- return false;
- }
- return true;
- }
- return false;
- }
- }
- public void Finish()
- {
- if (this.mAnim != null)
- {
- IEnumerator enumerator = this.mAnim.GetEnumerator();
- try
- {
- while (enumerator.MoveNext())
- {
- object obj = enumerator.Current;
- AnimationState animationState = (AnimationState)obj;
- if (this.mLastDirection == Direction.Forward)
- {
- animationState.time = animationState.length;
- }
- else if (this.mLastDirection == Direction.Reverse)
- {
- animationState.time = 0f;
- }
- }
- }
- finally
- {
- IDisposable disposable;
- if ((disposable = (enumerator as IDisposable)) != null)
- {
- disposable.Dispose();
- }
- }
- this.mAnim.Sample();
- }
- else if (this.mAnimator != null)
- {
- this.mAnimator.Play(this.mClip, 0, (this.mLastDirection != Direction.Forward) ? 0f : 1f);
- }
- }
- public void Reset()
- {
- if (this.mAnim != null)
- {
- IEnumerator enumerator = this.mAnim.GetEnumerator();
- try
- {
- while (enumerator.MoveNext())
- {
- object obj = enumerator.Current;
- AnimationState animationState = (AnimationState)obj;
- if (this.mLastDirection == Direction.Reverse)
- {
- animationState.time = animationState.length;
- }
- else if (this.mLastDirection == Direction.Forward)
- {
- animationState.time = 0f;
- }
- }
- }
- finally
- {
- IDisposable disposable;
- if ((disposable = (enumerator as IDisposable)) != null)
- {
- disposable.Dispose();
- }
- }
- }
- else if (this.mAnimator != null)
- {
- this.mAnimator.Play(this.mClip, 0, (this.mLastDirection != Direction.Reverse) ? 0f : 1f);
- }
- }
- private void Start()
- {
- if (this.eventReceiver != null && EventDelegate.IsValid(this.onFinished))
- {
- this.eventReceiver = null;
- this.callWhenFinished = null;
- }
- }
- private void Update()
- {
- float deltaTime = RealTime.deltaTime;
- if (deltaTime == 0f)
- {
- return;
- }
- if (this.mAnimator != null)
- {
- this.mAnimator.Update((this.mLastDirection != Direction.Reverse) ? deltaTime : (-deltaTime));
- if (this.isPlaying)
- {
- return;
- }
- this.mAnimator.enabled = false;
- base.enabled = false;
- }
- else
- {
- if (!(this.mAnim != null))
- {
- base.enabled = false;
- return;
- }
- bool flag = false;
- IEnumerator enumerator = this.mAnim.GetEnumerator();
- try
- {
- while (enumerator.MoveNext())
- {
- object obj = enumerator.Current;
- AnimationState animationState = (AnimationState)obj;
- if (this.mAnim.IsPlaying(animationState.name))
- {
- float num = animationState.speed * deltaTime;
- animationState.time += num;
- if (num < 0f)
- {
- if (animationState.time > 0f)
- {
- flag = true;
- }
- else
- {
- animationState.time = 0f;
- }
- }
- else if (animationState.time < animationState.length)
- {
- flag = true;
- }
- else
- {
- animationState.time = animationState.length;
- }
- }
- }
- }
- finally
- {
- IDisposable disposable;
- if ((disposable = (enumerator as IDisposable)) != null)
- {
- disposable.Dispose();
- }
- }
- this.mAnim.Sample();
- if (flag)
- {
- return;
- }
- base.enabled = false;
- }
- if (this.mNotify)
- {
- this.mNotify = false;
- if (ActiveAnimation.current == null)
- {
- ActiveAnimation.current = this;
- EventDelegate.Execute(this.onFinished);
- if (this.eventReceiver != null && !string.IsNullOrEmpty(this.callWhenFinished))
- {
- this.eventReceiver.SendMessage(this.callWhenFinished, SendMessageOptions.DontRequireReceiver);
- }
- ActiveAnimation.current = null;
- }
- if (this.mDisableDirection != Direction.Toggle && this.mLastDirection == this.mDisableDirection)
- {
- NGUITools.SetActive(base.gameObject, false);
- }
- }
- }
- private void Play(string clipName, Direction playDirection)
- {
- if (playDirection == Direction.Toggle)
- {
- playDirection = ((this.mLastDirection == Direction.Forward) ? Direction.Reverse : Direction.Forward);
- }
- if (this.mAnim != null)
- {
- base.enabled = true;
- this.mAnim.enabled = false;
- bool flag = string.IsNullOrEmpty(clipName);
- if (flag)
- {
- if (!this.mAnim.isPlaying)
- {
- this.mAnim.Play();
- }
- }
- else if (!this.mAnim.IsPlaying(clipName))
- {
- this.mAnim.Play(clipName);
- }
- IEnumerator enumerator = this.mAnim.GetEnumerator();
- try
- {
- while (enumerator.MoveNext())
- {
- object obj = enumerator.Current;
- AnimationState animationState = (AnimationState)obj;
- if (string.IsNullOrEmpty(clipName) || animationState.name == clipName)
- {
- float num = Mathf.Abs(animationState.speed);
- animationState.speed = num * (float)playDirection;
- if (playDirection == Direction.Reverse && animationState.time == 0f)
- {
- animationState.time = animationState.length;
- }
- else if (playDirection == Direction.Forward && animationState.time == animationState.length)
- {
- animationState.time = 0f;
- }
- }
- }
- }
- finally
- {
- IDisposable disposable;
- if ((disposable = (enumerator as IDisposable)) != null)
- {
- disposable.Dispose();
- }
- }
- this.mLastDirection = playDirection;
- this.mNotify = true;
- this.mAnim.Sample();
- }
- else if (this.mAnimator != null)
- {
- if (base.enabled && this.isPlaying && this.mClip == clipName)
- {
- this.mLastDirection = playDirection;
- return;
- }
- base.enabled = true;
- this.mNotify = true;
- this.mLastDirection = playDirection;
- this.mClip = clipName;
- this.mAnimator.Play(this.mClip, 0, (playDirection != Direction.Forward) ? 1f : 0f);
- }
- }
- public static ActiveAnimation Play(Animation anim, string clipName, Direction playDirection, EnableCondition enableBeforePlay, DisableCondition disableCondition)
- {
- if (!NGUITools.GetActive(anim.gameObject))
- {
- if (enableBeforePlay != EnableCondition.EnableThenPlay)
- {
- return null;
- }
- NGUITools.SetActive(anim.gameObject, true);
- UIPanel[] componentsInChildren = anim.gameObject.GetComponentsInChildren<UIPanel>();
- int i = 0;
- int num = componentsInChildren.Length;
- while (i < num)
- {
- componentsInChildren[i].Refresh();
- i++;
- }
- }
- ActiveAnimation activeAnimation = anim.GetComponent<ActiveAnimation>();
- if (activeAnimation == null)
- {
- activeAnimation = anim.gameObject.AddComponent<ActiveAnimation>();
- }
- activeAnimation.mAnim = anim;
- activeAnimation.mDisableDirection = (Direction)disableCondition;
- activeAnimation.onFinished.Clear();
- activeAnimation.Play(clipName, playDirection);
- if (activeAnimation.mAnim != null)
- {
- activeAnimation.mAnim.Sample();
- }
- else if (activeAnimation.mAnimator != null)
- {
- activeAnimation.mAnimator.Update(0f);
- }
- return activeAnimation;
- }
- public static ActiveAnimation Play(Animation anim, string clipName, Direction playDirection)
- {
- return ActiveAnimation.Play(anim, clipName, playDirection, EnableCondition.DoNothing, DisableCondition.DoNotDisable);
- }
- public static ActiveAnimation Play(Animation anim, Direction playDirection)
- {
- return ActiveAnimation.Play(anim, null, playDirection, EnableCondition.DoNothing, DisableCondition.DoNotDisable);
- }
- public static ActiveAnimation Play(Animator anim, string clipName, Direction playDirection, EnableCondition enableBeforePlay, DisableCondition disableCondition)
- {
- if (enableBeforePlay != EnableCondition.IgnoreDisabledState && !NGUITools.GetActive(anim.gameObject))
- {
- if (enableBeforePlay != EnableCondition.EnableThenPlay)
- {
- return null;
- }
- NGUITools.SetActive(anim.gameObject, true);
- UIPanel[] componentsInChildren = anim.gameObject.GetComponentsInChildren<UIPanel>();
- int i = 0;
- int num = componentsInChildren.Length;
- while (i < num)
- {
- componentsInChildren[i].Refresh();
- i++;
- }
- }
- ActiveAnimation activeAnimation = anim.GetComponent<ActiveAnimation>();
- if (activeAnimation == null)
- {
- activeAnimation = anim.gameObject.AddComponent<ActiveAnimation>();
- }
- activeAnimation.mAnimator = anim;
- activeAnimation.mDisableDirection = (Direction)disableCondition;
- activeAnimation.onFinished.Clear();
- activeAnimation.Play(clipName, playDirection);
- if (activeAnimation.mAnim != null)
- {
- activeAnimation.mAnim.Sample();
- }
- else if (activeAnimation.mAnimator != null)
- {
- activeAnimation.mAnimator.Update(0f);
- }
- return activeAnimation;
- }
- public static ActiveAnimation current;
- public List<EventDelegate> onFinished = new List<EventDelegate>();
- [HideInInspector]
- public GameObject eventReceiver;
- [HideInInspector]
- public string callWhenFinished;
- private Animation mAnim;
- private Direction mLastDirection;
- private Direction mDisableDirection;
- private bool mNotify;
- private Animator mAnimator;
- private string mClip = string.Empty;
- }
|