123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- using System;
- using System.Collections.Generic;
- using AnimationOrTween;
- using UnityEngine;
- [ExecuteInEditMode]
- [AddComponentMenu("NGUI/Interaction/Play Animation")]
- public class UIPlayAnimation : MonoBehaviour
- {
- private bool dualState
- {
- get
- {
- return this.trigger == Trigger.OnPress || this.trigger == Trigger.OnHover;
- }
- }
- private void Awake()
- {
- UIButton component = base.GetComponent<UIButton>();
- if (component != null)
- {
- this.dragHighlight = component.dragHighlight;
- }
- if (this.eventReceiver != null && EventDelegate.IsValid(this.onFinished))
- {
- this.eventReceiver = null;
- this.callWhenFinished = null;
- }
- }
- private void Start()
- {
- this.mStarted = true;
- if (this.target == null && this.animator == null)
- {
- this.animator = base.GetComponentInChildren<Animator>();
- }
- if (this.animator != null)
- {
- if (this.animator.enabled)
- {
- this.animator.enabled = false;
- }
- return;
- }
- if (this.target == null)
- {
- this.target = base.GetComponentInChildren<Animation>();
- }
- if (this.target != null && this.target.enabled)
- {
- this.target.enabled = false;
- }
- }
- 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 OnHover(bool isOver)
- {
- if (!base.enabled)
- {
- return;
- }
- if (this.trigger == Trigger.OnHover || (this.trigger == Trigger.OnHoverTrue && isOver) || (this.trigger == Trigger.OnHoverFalse && !isOver))
- {
- this.Play(isOver, this.dualState);
- }
- }
- private void OnPress(bool isPressed)
- {
- if (!base.enabled)
- {
- return;
- }
- if (UICamera.currentTouchID < -1)
- {
- return;
- }
- if (this.trigger == Trigger.OnPress || (this.trigger == Trigger.OnPressTrue && isPressed) || (this.trigger == Trigger.OnPressFalse && !isPressed))
- {
- this.Play(isPressed, this.dualState);
- }
- }
- private void OnClick()
- {
- if (UICamera.currentTouchID < -1)
- {
- return;
- }
- if (base.enabled && this.trigger == Trigger.OnClick)
- {
- this.Play(true, false);
- }
- }
- private void OnDoubleClick()
- {
- if (UICamera.currentTouchID < -1)
- {
- return;
- }
- if (base.enabled && this.trigger == Trigger.OnDoubleClick)
- {
- this.Play(true, false);
- }
- }
- private void OnSelect(bool isSelected)
- {
- if (!base.enabled)
- {
- return;
- }
- if (this.trigger == Trigger.OnSelect || (this.trigger == Trigger.OnSelectTrue && isSelected) || (this.trigger == Trigger.OnSelectFalse && !isSelected))
- {
- this.Play(isSelected, this.dualState);
- }
- }
- 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, this.dualState);
- }
- }
- private void OnDragOver()
- {
- if (base.enabled && this.dualState)
- {
- if (UICamera.currentTouch.dragged == base.gameObject)
- {
- this.Play(true, true);
- }
- else if (this.dragHighlight && this.trigger == Trigger.OnPress)
- {
- this.Play(true, true);
- }
- }
- }
- private void OnDragOut()
- {
- if (base.enabled && this.dualState && UICamera.hoveredObject != base.gameObject)
- {
- this.Play(false, true);
- }
- }
- private void OnDrop(GameObject go)
- {
- if (base.enabled && this.trigger == Trigger.OnPress && UICamera.currentTouch.dragged != base.gameObject)
- {
- this.Play(false, true);
- }
- }
- public void Play(bool forward)
- {
- this.Play(forward, true);
- }
- public void Play(bool forward, bool onlyIfDifferent)
- {
- if (this.target || this.animator)
- {
- if (onlyIfDifferent)
- {
- if (this.mActivated == forward)
- {
- return;
- }
- this.mActivated = forward;
- }
- if (this.clearSelection && UICamera.selectedObject == base.gameObject)
- {
- UICamera.selectedObject = null;
- }
- int num = (int)(-(int)this.playDirection);
- Direction direction = (Direction)((!forward) ? num : ((int)this.playDirection));
- ActiveAnimation activeAnimation = (!this.target) ? ActiveAnimation.Play(this.animator, this.clipName, direction, this.ifDisabledOnPlay, this.disableWhenFinished) : ActiveAnimation.Play(this.target, this.clipName, direction, this.ifDisabledOnPlay, this.disableWhenFinished);
- if (activeAnimation != null)
- {
- if (this.resetOnPlay)
- {
- activeAnimation.Reset();
- }
- for (int i = 0; i < this.onFinished.Count; i++)
- {
- EventDelegate.Add(activeAnimation.onFinished, new EventDelegate.Callback(this.OnFinished), true);
- }
- }
- }
- }
- private void OnFinished()
- {
- if (UIPlayAnimation.current == null)
- {
- UIPlayAnimation.current = this;
- EventDelegate.Execute(this.onFinished);
- if (this.eventReceiver != null && !string.IsNullOrEmpty(this.callWhenFinished))
- {
- this.eventReceiver.SendMessage(this.callWhenFinished, SendMessageOptions.DontRequireReceiver);
- }
- this.eventReceiver = null;
- UIPlayAnimation.current = null;
- }
- }
- public static UIPlayAnimation current;
- public Animation target;
- public Animator animator;
- public string clipName;
- public Trigger trigger;
- public Direction playDirection = Direction.Forward;
- public bool resetOnPlay;
- public bool clearSelection;
- public EnableCondition ifDisabledOnPlay;
- public DisableCondition disableWhenFinished;
- public List<EventDelegate> onFinished = new List<EventDelegate>();
- [HideInInspector]
- [SerializeField]
- private GameObject eventReceiver;
- [HideInInspector]
- [SerializeField]
- private string callWhenFinished;
- private bool mStarted;
- private bool mActivated;
- private bool dragHighlight;
- }
|