123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- using System;
- using System.Collections.Generic;
- using AnimationOrTween;
- using UnityEngine;
- [ExecuteInEditMode]
- [AddComponentMenu("NGUI/Interaction/Play Tween")]
- public class UIPlayTween : MonoBehaviour
- {
- private void Awake()
- {
- if (this.eventReceiver != null && EventDelegate.IsValid(this.onFinished))
- {
- this.eventReceiver = null;
- this.callWhenFinished = null;
- }
- }
- private void Start()
- {
- this.mStarted = true;
- if (this.tweenTarget == null)
- {
- this.tweenTarget = base.gameObject;
- }
- }
- private void OnEnable()
- {
- if (this.mStarted)
- {
- this.OnHover(UICamera.IsHighlighted(base.gameObject));
- }
- if (UICamera.currentTouch != null)
- {
- if (this.trigger == Trigger.OnPress || this.trigger == Trigger.OnPressTrue)
- {
- this.mActivated = (UICamera.currentTouch.pressed == base.gameObject);
- }
- if (this.trigger == Trigger.OnHover || this.trigger == Trigger.OnHoverTrue)
- {
- this.mActivated = (UICamera.currentTouch.current == base.gameObject);
- }
- }
- UIToggle component = base.GetComponent<UIToggle>();
- if (component != null)
- {
- EventDelegate.Add(component.onChange, new EventDelegate.Callback(this.OnToggle));
- }
- }
- private void OnDisable()
- {
- UIToggle component = base.GetComponent<UIToggle>();
- if (component != null)
- {
- EventDelegate.Remove(component.onChange, new EventDelegate.Callback(this.OnToggle));
- }
- }
- private void OnDragOver()
- {
- if (this.trigger == Trigger.OnHover)
- {
- this.OnHover(true);
- }
- }
- private void OnHover(bool isOver)
- {
- if (base.enabled && (this.trigger == Trigger.OnHover || (this.trigger == Trigger.OnHoverTrue && isOver) || (this.trigger == Trigger.OnHoverFalse && !isOver)))
- {
- this.mActivated = (isOver && this.trigger == Trigger.OnHover);
- this.Play(isOver);
- }
- }
- private void OnDragOut()
- {
- if (base.enabled && this.mActivated)
- {
- this.mActivated = false;
- this.Play(false);
- }
- }
- private void OnPress(bool isPressed)
- {
- if (base.enabled && (this.trigger == Trigger.OnPress || (this.trigger == Trigger.OnPressTrue && isPressed) || (this.trigger == Trigger.OnPressFalse && !isPressed)))
- {
- this.mActivated = (isPressed && this.trigger == Trigger.OnPress);
- this.Play(isPressed);
- }
- }
- private void OnClick()
- {
- if (base.enabled && this.trigger == Trigger.OnClick)
- {
- this.Play(true);
- }
- }
- private void OnDoubleClick()
- {
- if (base.enabled && this.trigger == Trigger.OnDoubleClick)
- {
- this.Play(true);
- }
- }
- private void OnSelect(bool isSelected)
- {
- if (base.enabled && (this.trigger == Trigger.OnSelect || (this.trigger == Trigger.OnSelectTrue && isSelected) || (this.trigger == Trigger.OnSelectFalse && !isSelected)))
- {
- this.mActivated = (isSelected && this.trigger == Trigger.OnSelect);
- this.Play(isSelected);
- }
- }
- private void OnToggle()
- {
- if (!base.enabled || UIToggle.current == null)
- {
- return;
- }
- if (this.trigger == Trigger.OnActivate || (this.trigger == Trigger.OnActivateTrue && UIToggle.current.value) || (this.trigger == Trigger.OnActivateFalse && !UIToggle.current.value))
- {
- this.Play(UIToggle.current.value);
- }
- }
- private void Update()
- {
- if (this.disableWhenFinished != DisableCondition.DoNotDisable && this.mTweens != null)
- {
- bool flag = true;
- bool flag2 = true;
- int i = 0;
- int num = this.mTweens.Length;
- while (i < num)
- {
- UITweener uitweener = this.mTweens[i];
- if (uitweener.tweenGroup == this.tweenGroup)
- {
- if (uitweener.enabled)
- {
- flag = false;
- break;
- }
- if (uitweener.direction != (Direction)this.disableWhenFinished)
- {
- flag2 = false;
- }
- }
- i++;
- }
- if (flag)
- {
- if (flag2)
- {
- NGUITools.SetActive(this.tweenTarget, false);
- }
- this.mTweens = null;
- }
- }
- }
- public void Play(bool forward)
- {
- this.mActive = 0;
- GameObject gameObject = (!(this.tweenTarget == null)) ? this.tweenTarget : base.gameObject;
- if (!NGUITools.GetActive(gameObject))
- {
- if (this.ifDisabledOnPlay != EnableCondition.EnableThenPlay)
- {
- return;
- }
- NGUITools.SetActive(gameObject, true);
- }
- this.mTweens = ((!this.includeChildren) ? gameObject.GetComponents<UITweener>() : gameObject.GetComponentsInChildren<UITweener>());
- if (this.mTweens.Length == 0)
- {
- if (this.disableWhenFinished != DisableCondition.DoNotDisable)
- {
- NGUITools.SetActive(this.tweenTarget, false);
- }
- }
- else
- {
- bool flag = false;
- if (this.playDirection == Direction.Reverse)
- {
- forward = !forward;
- }
- int i = 0;
- int num = this.mTweens.Length;
- while (i < num)
- {
- UITweener uitweener = this.mTweens[i];
- if (uitweener.tweenGroup == this.tweenGroup)
- {
- if (!flag && !NGUITools.GetActive(gameObject))
- {
- flag = true;
- NGUITools.SetActive(gameObject, true);
- }
- this.mActive++;
- if (this.playDirection == Direction.Toggle)
- {
- EventDelegate.Add(uitweener.onFinished, new EventDelegate.Callback(this.OnFinished), true);
- uitweener.Toggle();
- }
- else
- {
- if (this.resetOnPlay || (this.resetIfDisabled && !uitweener.enabled))
- {
- uitweener.Play(forward);
- uitweener.ResetToBeginning();
- }
- EventDelegate.Add(uitweener.onFinished, new EventDelegate.Callback(this.OnFinished), true);
- uitweener.Play(forward);
- }
- }
- i++;
- }
- }
- }
- private void OnFinished()
- {
- if (--this.mActive == 0 && UIPlayTween.current == null)
- {
- UIPlayTween.current = this;
- EventDelegate.Execute(this.onFinished);
- if (this.eventReceiver != null && !string.IsNullOrEmpty(this.callWhenFinished))
- {
- this.eventReceiver.SendMessage(this.callWhenFinished, SendMessageOptions.DontRequireReceiver);
- }
- this.eventReceiver = null;
- UIPlayTween.current = null;
- }
- }
- public static UIPlayTween current;
- public GameObject tweenTarget;
- public int tweenGroup;
- public Trigger trigger;
- public Direction playDirection = Direction.Forward;
- public bool resetOnPlay;
- public bool resetIfDisabled;
- public EnableCondition ifDisabledOnPlay;
- public DisableCondition disableWhenFinished;
- public bool includeChildren;
- public List<EventDelegate> onFinished = new List<EventDelegate>();
- [HideInInspector]
- [SerializeField]
- private GameObject eventReceiver;
- [HideInInspector]
- [SerializeField]
- private string callWhenFinished;
- private UITweener[] mTweens;
- private bool mStarted;
- private int mActive;
- private bool mActivated;
- }
|