/// <summary> Triggers this behavior by executing its actions </summary> /// <param name="toggle"> UIToggle that triggered this behavior </param> /// <param name="playAnimation"> Play the set animation </param> /// <param name="playSound"> Play the set sound </param> /// <param name="executeEffect"> Run the effect </param> /// <param name="executeAnimatorEvents"> Trigger all the animator events </param> /// <param name="sendGameEvents"> Send all the game events </param> /// <param name="executeUnityEvent"> Execute the Action and the UnityEvent </param> public void Invoke(UIToggle toggle, bool playAnimation = true, bool playSound = true, bool executeEffect = true, bool executeAnimatorEvents = true, bool sendGameEvents = true, bool executeUnityEvent = true) { if (toggle == null) { return; } UIAction uiAction = toggle.IsOn ? OnToggleOn : OnToggleOff; if (playAnimation) { PlayAnimation(toggle, false); //Animation } if (playSound) { uiAction.PlaySound(); //Sound } if (executeEffect) { uiAction.ExecuteEffect(uiAction.GetCanvas(toggle.gameObject)); //Effect } if (executeAnimatorEvents) { uiAction.InvokeAnimatorEvents(); //Animator Events } if (!sendGameEvents && !executeUnityEvent) { return; } if (!TriggerEventsAfterAnimation) { if (sendGameEvents) { uiAction.SendGameEvents(toggle.gameObject); //Game Events } if (!executeUnityEvent) { return; } uiAction.InvokeAction(toggle.gameObject); //Action uiAction.InvokeUnityEvent(); //UnityEvent } else { Coroutiner.Start(InvokeCallbackAfterDelay(() => { if (toggle == null) { return; } if (sendGameEvents) { uiAction.SendGameEvents(toggle.gameObject); //Game Events } if (!executeUnityEvent) { return; } uiAction.InvokeAction(toggle.gameObject); //Action uiAction.InvokeUnityEvent(); //UnityEvent }, GetAnimationTotalDuration())); } }
/// <summary> Plays the currently active animation </summary> /// <param name="toggle"> The toggle reference </param> /// <param name="withSound"> If set to <c>true</c> [with sound] </param> /// <param name="onStartCallback"> Callback fired when the animation starts playing </param> /// <param name="onCompleteCallback"> Callback fired when the animation completed playing </param> public void PlayAnimation(UIToggle toggle, bool withSound = true, UnityAction onStartCallback = null, UnityAction onCompleteCallback = null) { // ReSharper disable once SwitchStatementMissingSomeCases switch (ButtonAnimationType) { case ButtonAnimationType.Punch: if (PunchAnimation == null) { return; } UIAnimator.StopAnimations(toggle.RectTransform, AnimationType.Punch); if (PunchAnimation.Move.Enabled) { toggle.ResetPosition(); } if (PunchAnimation.Rotate.Enabled) { toggle.ResetRotation(); } if (PunchAnimation.Scale.Enabled) { toggle.ResetScale(); } UIAnimator.MovePunch(toggle.RectTransform, PunchAnimation, toggle.StartPosition); //play the move punch animation UIAnimator.RotatePunch(toggle.RectTransform, PunchAnimation, toggle.StartRotation); //play the rotate punch animation UIAnimator.ScalePunch(toggle.RectTransform, PunchAnimation, toggle.StartScale); //play the scale punch animation Coroutiner.Start(InvokeCallbacks(PunchAnimation, onStartCallback, onCompleteCallback)); break; case ButtonAnimationType.State: if (StateAnimation == null) { return; } UIAnimator.StopAnimations(toggle.RectTransform, AnimationType.State); UIAnimator.MoveState(toggle.RectTransform, StateAnimation, toggle.StartPosition); UIAnimator.RotateState(toggle.RectTransform, StateAnimation, toggle.StartRotation); UIAnimator.ScaleState(toggle.RectTransform, StateAnimation, toggle.StartScale); UIAnimator.FadeState(toggle.RectTransform, StateAnimation, toggle.StartAlpha); Coroutiner.Start(InvokeCallbacks(StateAnimation, onStartCallback, onCompleteCallback)); break; case ButtonAnimationType.Animator: if (Animators == null || Animators.Count == 0) { return; } foreach (AnimatorEvent animatorEvent in Animators) { animatorEvent.Invoke(); } break; } if (!withSound) { return; } if (toggle.IsOn) { OnToggleOn.PlaySound(); } else { OnToggleOff.PlaySound(); } }
/// <summary> Initializes a new instance of the class with reference to the UIToggle, its toggle state and UIButtonBehaviorType, of the UIButtonBehavior, that triggered this message </summary> /// <param name="toggle"> Reference to the UIToggle that sent this message </param> /// <param name="toggleState"> The toggle state the UIToggle was in when the message was sent </param> /// <param name="type"> UIToggleBehaviorType of the UIToggleBehavior that triggered the UIToggle to send this message </param> public UIToggleMessage(UIToggle toggle, UIToggleState toggleState, UIToggleBehaviorType type) { Toggle = toggle; ToggleState = toggleState; Type = type; }