Author Topic: UIKeyNavigation's Start Selected don't do onSelect  (Read 4765 times)

Sundaerae

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 9
    • View Profile
UIKeyNavigation's Start Selected don't do onSelect
« on: February 23, 2015, 05:50:15 AM »
I'm using Unity 4.6.0 and NGUI 3.8.0 (documentation said 3.7.7)
When I was watching the tutorial, Start Selected supposed to play the animations, usually hover and such right after the scene started. (Or in Controller Input example, plays the button animation through UIPlayTween).
It doesn't happen, both in my newly made scene, or in the Controller Input Example. The object is selected, yes, but the animation (or any onselect event) didn't trigger.
EDIT: after checking, only the OnHover color / set sprite of the button that didn't trigger, it's fine if I use PlayTween or PlayAnimation.

any way to fix this?

The UIKeyNavigation itself works fine, if I navigate to other buttons, the hover animation plays as intended. It just the initial onSelect that is not triggered.
« Last Edit: February 23, 2015, 06:11:23 AM by Sundaerae »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIKeyNavigation's Start Selected don't do onSelect
« Reply #1 on: February 24, 2015, 11:17:36 AM »
Yup known issue, already fixed in the Pro version. Replace UIPlayAnimation with this:
  1. //----------------------------------------------
  2. //            NGUI: Next-Gen UI kit
  3. // Copyright © 2011-2015 Tasharen Entertainment
  4. //----------------------------------------------
  5.  
  6. using UnityEngine;
  7. using System.Collections.Generic;
  8. using AnimationOrTween;
  9.  
  10. /// <summary>
  11. /// Play the specified animation on click.
  12. /// </summary>
  13.  
  14. [ExecuteInEditMode]
  15. [AddComponentMenu("NGUI/Interaction/Play Animation")]
  16. public class UIPlayAnimation : MonoBehaviour
  17. {
  18.         static public UIPlayAnimation current = null;
  19.  
  20.         /// <summary>
  21.         /// Target animation to activate.
  22.         /// </summary>
  23.  
  24.         public Animation target;
  25.  
  26.         /// <summary>
  27.         /// Target animator system.
  28.         /// </summary>
  29.  
  30.         public Animator animator;
  31.  
  32.         /// <summary>
  33.         /// Optional clip name, if the animation has more than one clip.
  34.         /// </summary>
  35.  
  36.         public string clipName;
  37.  
  38.         /// <summary>
  39.         /// Which event will trigger the animation.
  40.         /// </summary>
  41.  
  42.         public Trigger trigger = Trigger.OnClick;
  43.  
  44.         /// <summary>
  45.         /// Which direction to animate in.
  46.         /// </summary>
  47.  
  48.         public Direction playDirection = Direction.Forward;
  49.  
  50.         /// <summary>
  51.         /// Whether the animation's position will be reset on play or will continue from where it left off.
  52.         /// </summary>
  53.  
  54.         public bool resetOnPlay = false;
  55.  
  56.         /// <summary>
  57.         /// Whether the selected object (this button) will be cleared when the animation gets activated.
  58.         /// </summary>
  59.  
  60.         public bool clearSelection = false;
  61.  
  62.         /// <summary>
  63.         /// What to do if the target game object is currently disabled.
  64.         /// </summary>
  65.  
  66.         public EnableCondition ifDisabledOnPlay = EnableCondition.DoNothing;
  67.  
  68.         /// <summary>
  69.         /// What to do with the target when the animation finishes.
  70.         /// </summary>
  71.  
  72.         public DisableCondition disableWhenFinished = DisableCondition.DoNotDisable;
  73.  
  74.         /// <summary>
  75.         /// Event delegates called when the animation finishes.
  76.         /// </summary>
  77.  
  78.         public List<EventDelegate> onFinished = new List<EventDelegate>();
  79.  
  80.         // Deprecated functionality, kept for backwards compatibility
  81.         [HideInInspector][SerializeField] GameObject eventReceiver;
  82.         [HideInInspector][SerializeField] string callWhenFinished;
  83.  
  84.         bool mStarted = false;
  85.         bool mActivated = false;
  86.         bool dragHighlight = false;
  87.  
  88.         bool dualState { get { return trigger == Trigger.OnPress || trigger == Trigger.OnHover; } }
  89.  
  90.         void Awake ()
  91.         {
  92.                 UIButton btn = GetComponent<UIButton>();
  93.                 if (btn != null) dragHighlight = btn.dragHighlight;
  94.  
  95.                 // Remove deprecated functionality if new one is used
  96.                 if (eventReceiver != null && EventDelegate.IsValid(onFinished))
  97.                 {
  98.                         eventReceiver = null;
  99.                         callWhenFinished = null;
  100. #if UNITY_EDITOR
  101.                         NGUITools.SetDirty(this);
  102. #endif
  103.                 }
  104.         }
  105.  
  106.         /// <summary>
  107.         /// Automatically find the necessary components.
  108.         /// </summary>
  109.  
  110.         void Start ()
  111.         {
  112.                 mStarted = true;
  113.  
  114.                 // Automatically try to find the animator
  115.                 if (target == null && animator == null)
  116.                 {
  117.                         animator = GetComponentInChildren<Animator>();
  118. #if UNITY_EDITOR
  119.                         if (animator != null) NGUITools.SetDirty(this);
  120. #endif
  121.                 }
  122.  
  123.                 if (animator != null)
  124.                 {
  125.                         // Ensure that the animator is disabled as we will be sampling it manually
  126.                         if (animator.enabled) animator.enabled = false;
  127.  
  128.                         // Don't continue since we already have an animator to work with
  129.                         return;
  130.                 }
  131.  
  132.                 if (target == null)
  133.                 {
  134.                         target = GetComponentInChildren<Animation>();
  135. #if UNITY_EDITOR
  136.                         if (target != null) NGUITools.SetDirty(this);
  137. #endif
  138.                 }
  139.  
  140.                 if (target != null && target.enabled)
  141.                         target.enabled = false;
  142.         }
  143.  
  144.         void OnEnable ()
  145.         {
  146. #if UNITY_EDITOR
  147.                 if (!Application.isPlaying) return;
  148. #endif
  149.                 if (mStarted) OnHover(UICamera.IsHighlighted(gameObject));
  150.  
  151.                 if (UICamera.currentTouch != null)
  152.                 {
  153.                         if (trigger == Trigger.OnPress || trigger == Trigger.OnPressTrue)
  154.                                 mActivated = (UICamera.currentTouch.pressed == gameObject);
  155.  
  156.                         if (trigger == Trigger.OnHover || trigger == Trigger.OnHoverTrue)
  157.                                 mActivated = (UICamera.currentTouch.current == gameObject);
  158.                 }
  159.  
  160.                 UIToggle toggle = GetComponent<UIToggle>();
  161.                 if (toggle != null) EventDelegate.Add(toggle.onChange, OnToggle);
  162.         }
  163.  
  164.         void OnDisable ()
  165.         {
  166. #if UNITY_EDITOR
  167.                 if (!Application.isPlaying) return;
  168. #endif
  169.                 UIToggle toggle = GetComponent<UIToggle>();
  170.                 if (toggle != null) EventDelegate.Remove(toggle.onChange, OnToggle);
  171.         }
  172.  
  173.         void OnHover (bool isOver)
  174.         {
  175.                 if (!enabled) return;
  176.                 if ( trigger == Trigger.OnHover ||
  177.                         (trigger == Trigger.OnHoverTrue && isOver) ||
  178.                         (trigger == Trigger.OnHoverFalse && !isOver))
  179.                         Play(isOver, dualState);
  180.         }
  181.  
  182.         void OnPress (bool isPressed)
  183.         {
  184.                 if (!enabled) return;
  185.                 if (UICamera.currentTouchID < -1 && UICamera.currentScheme != UICamera.ControlScheme.Controller) return;
  186.                 if ( trigger == Trigger.OnPress ||
  187.                         (trigger == Trigger.OnPressTrue && isPressed) ||
  188.                         (trigger == Trigger.OnPressFalse && !isPressed))
  189.                         Play(isPressed, dualState);
  190.         }
  191.  
  192.         void OnClick ()
  193.         {
  194.                 if (UICamera.currentTouchID < -1 && UICamera.currentScheme != UICamera.ControlScheme.Controller) return;
  195.                 if (enabled && trigger == Trigger.OnClick) Play(true, false);
  196.         }
  197.  
  198.         void OnDoubleClick ()
  199.         {
  200.                 if (UICamera.currentTouchID < -1 && UICamera.currentScheme != UICamera.ControlScheme.Controller) return;
  201.                 if (enabled && trigger == Trigger.OnDoubleClick) Play(true, false);
  202.         }
  203.  
  204.         void OnSelect (bool isSelected)
  205.         {
  206.                 if (!enabled) return;
  207.                 if (trigger == Trigger.OnSelect ||
  208.                         (trigger == Trigger.OnSelectTrue && isSelected) ||
  209.                         (trigger == Trigger.OnSelectFalse && !isSelected))
  210.                         Play(isSelected, dualState);
  211.         }
  212.  
  213.         void OnToggle ()
  214.         {
  215.                 if (!enabled || UIToggle.current == null) return;
  216.                 if (trigger == Trigger.OnActivate ||
  217.                         (trigger == Trigger.OnActivateTrue && UIToggle.current.value) ||
  218.                         (trigger == Trigger.OnActivateFalse && !UIToggle.current.value))
  219.                         Play(UIToggle.current.value, dualState);
  220.         }
  221.  
  222.         void OnDragOver ()
  223.         {
  224.                 if (enabled && dualState)
  225.                 {
  226.                         if (UICamera.currentTouch.dragged == gameObject) Play(true, true);
  227.                         else if (dragHighlight && trigger == Trigger.OnPress) Play(true, true);
  228.                 }
  229.         }
  230.  
  231.         void OnDragOut ()
  232.         {
  233.                 if (enabled && dualState && UICamera.hoveredObject != gameObject)
  234.                         Play(false, true);
  235.         }
  236.  
  237.         void OnDrop (GameObject go)
  238.         {
  239.                 if (enabled && trigger == Trigger.OnPress && UICamera.currentTouch.dragged != gameObject)
  240.                         Play(false, true);
  241.         }
  242.        
  243.         /// <summary>
  244.         /// Start playing the animation.
  245.         /// </summary>
  246.  
  247.         public void Play (bool forward) { Play(forward, true); }
  248.  
  249.         /// <summary>
  250.         /// Start playing the animation.
  251.         /// </summary>
  252.  
  253.         public void Play (bool forward, bool onlyIfDifferent)
  254.         {
  255.                 if (target || animator)
  256.                 {
  257.                         if (onlyIfDifferent)
  258.                         {
  259.                                 if (mActivated == forward) return;
  260.                                 mActivated = forward;
  261.                         }
  262.  
  263.                         if (clearSelection && UICamera.selectedObject == gameObject)
  264.                                 UICamera.selectedObject = null;
  265.  
  266.                         int pd = -(int)playDirection;
  267.                         Direction dir = forward ? playDirection : ((Direction)pd);
  268.                         ActiveAnimation anim = target ?
  269.                                 ActiveAnimation.Play(target, clipName, dir, ifDisabledOnPlay, disableWhenFinished) :
  270.                                 ActiveAnimation.Play(animator, clipName, dir, ifDisabledOnPlay, disableWhenFinished);
  271.  
  272.                         if (anim != null)
  273.                         {
  274.                                 if (resetOnPlay) anim.Reset();
  275.                                 for (int i = 0; i < onFinished.Count; ++i)
  276.                                         EventDelegate.Add(anim.onFinished, OnFinished, true);
  277.                         }
  278.                 }
  279.         }
  280.  
  281.         /// <summary>
  282.         /// Callback triggered when each tween executed by this script finishes.
  283.         /// </summary>
  284.  
  285.         void OnFinished ()
  286.         {
  287.                 if (current == null)
  288.                 {
  289.                         current = this;
  290.                         EventDelegate.Execute(onFinished);
  291.  
  292.                         // Legacy functionality
  293.                         if (eventReceiver != null && !string.IsNullOrEmpty(callWhenFinished))
  294.                                 eventReceiver.SendMessage(callWhenFinished, SendMessageOptions.DontRequireReceiver);
  295.  
  296.                         eventReceiver = null;
  297.                         current = null;
  298.                 }
  299.         }
  300. }
  301.  

Sundaerae

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: UIKeyNavigation's Start Selected don't do onSelect
« Reply #2 on: February 25, 2015, 07:44:27 AM »
Sorry Aren, it still don't work even after replacing. And yeah, I have the Pro version since I download it directly from asset store.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIKeyNavigation's Start Selected don't do onSelect
« Reply #3 on: February 27, 2015, 04:36:08 AM »
Asset Store is a Standard version. Pro version is Github access.

Related? http://www.tasharen.com/forum/index.php?topic=11925.15

Sundaerae

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: UIKeyNavigation's Start Selected don't do onSelect
« Reply #4 on: March 04, 2015, 03:03:22 AM »
I see, I'll just wait for the next update then.

*about the link: in 3.8.0, all of those (UICamera.currentTouchID < -1) already changed to (UICamera.currentTouchID < -1 && UICamera.currentScheme != UICamera.ControlScheme.Controller), so the problem is not there.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIKeyNavigation's Start Selected don't do onSelect
« Reply #5 on: March 05, 2015, 09:52:11 PM »
Please remind me after the 9th, I will have a look at it again when I return from PAX East.