Author Topic: Re-enabling breaks UIButtonKeys Start Selected?  (Read 5215 times)

eucryptic

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 27
    • View Profile
Re-enabling breaks UIButtonKeys Start Selected?
« on: April 23, 2013, 09:06:18 PM »
I'm trying to write a pause menu using NGUI, and I've hit a snag.

My pause menu panel has 3 buttons: Resume, Restart and Quit.
I'm using UIButtonKeys to let the user move between the buttons.
I've ticked "Start Selected" on the Resume button.

Everything works fine, until I disable the panel.
Then when I re-enable it, Resume is never selected by default.

What am I doing wrong? It seems there are two ways of enabling, using SetActiveRecursively and NGUITools.SetActive. But neither of them make sure that Resume is selected when I re-enable the parent panel.

Here's my controller code:
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class InGameMenuController : MonoBehaviour {
  5.  
  6.   protected GameObject pausePanel;
  7.  
  8.   void Start() {
  9.     pausePanel = GameObject.Find("Pause");
  10.  
  11.     // Disable at first
  12.     // pausePanel.SetActiveRecursively(false);
  13.     NGUITools.SetActive(pausePanel, false);
  14.   }
  15.        
  16.   void Update() {
  17.     if (Input.GetButtonDown("Start")) {
  18.       if (Time.timeScale != 0) {
  19.         PauseGame();
  20.       }
  21.       else {
  22.         UnpauseGame();
  23.       }
  24.     }
  25.   }
  26.  
  27.   private void PauseGame() {
  28.     Time.timeScale = 0;
  29.  
  30.     // Show the pause menu
  31.     //pausePanel.SetActiveRecursively(true);
  32.     NGUITools.SetActive(pausePanel, true);
  33.  
  34.     GameObject button = GameObject.Find("Resume Button");
  35.     // None of these manual attempts seem to work
  36.     //UICamera.selectedObject = button;
  37.     //NGUITools.SetActive(button, true);
  38.     //button.active = true;
  39.   }
  40.  
  41.   private void UnpauseGame() {
  42.     Time.timeScale = 1.0f;
  43.  
  44.     //pausePanel.SetActiveRecursively(false);
  45.     NGUITools.SetActive(pausePanel, false);
  46.   }
  47. }
  48.  

Any help would be really appreciated.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Re-enabling breaks UIButtonKeys Start Selected?
« Reply #1 on: April 24, 2013, 12:36:51 AM »
Why not attach a script to your button that will set UICamera.selectedObject = gameObject in its OnEnable() function?

eucryptic

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 27
    • View Profile
Re: Re-enabling breaks UIButtonKeys Start Selected?
« Reply #2 on: April 24, 2013, 01:16:37 AM »
Thanks for the reply!

I tried your suggestion, adding a script to my Resume button, setting UICamera.selectedObject = gameObject in OnEnable().

Unfortunately it didn't work, the button was not selected.

I also tried changing the Pause() function of my controller to to set UICamera.selectedObject = resumeButton; but that didn't work either.

When I use the inspector to tick "Starts Selected" then resume is selected at first. But when I hide and re-show the pause menu, it's disabled the second time.

Is there something else I need to do other than set selectedObject?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Re-enabling breaks UIButtonKeys Start Selected?
« Reply #3 on: April 24, 2013, 12:31:25 PM »
Nope, there is nothing else. If it doesn't become selected, what does? Maybe it does get selected, it's just not visual? If it's that, then broadcast OnSelect(true) to the game object.

sh0v0r

  • Guest
Re: Re-enabling breaks UIButtonKeys Start Selected?
« Reply #4 on: June 28, 2013, 07:09:52 AM »
Nope, there is nothing else. If it doesn't become selected, what does? Maybe it does get selected, it's just not visual? If it's that, then broadcast OnSelect(true) to the game object.

I was having this same issue, except the Button WAS selected it just wasn't highlighted.

I added this to UIButtonKeys which seemed to solve the problem for me.

  1. void OnEnable()
  2.     {
  3.         if (startsSelected)
  4.         {                                              
  5.             UICamera.Notify(gameObject, "OnHover", true);
  6.         }      
  7.     }
  8.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Re-enabling breaks UIButtonKeys Start Selected?
« Reply #5 on: June 28, 2013, 04:07:25 PM »
Try replacing your Start and OnEnable functions with this combo function instead:
  1.         void OnEnable ()
  2.         {
  3.                 if (startsSelected)
  4.                 {
  5.                         if (UICamera.selectedObject == null || !NGUITools.GetActive(UICamera.selectedObject))
  6.                         {
  7.                                 UICamera.selectedObject = gameObject;
  8.                         }
  9.                         else
  10.                         {
  11.                                 UICamera.Notify(gameObject, "OnHover", true);
  12.                         }
  13.                 }
  14.         }

SpookyFishGames

  • Guest
Re: Re-enabling breaks UIButtonKeys Start Selected?
« Reply #6 on: August 20, 2013, 09:59:57 AM »
I was having a similar problem to this in my code. I have added the code in the last post to my OnEnable method of UIButtonKeys and now my default button is highlighted whenever I show this panel.

However when I move away from this default button it remains highlighted until I move back to it, then move away again. Is there another bit of code I can add somewhere to fix that?


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Re-enabling breaks UIButtonKeys Start Selected?
« Reply #7 on: August 20, 2013, 01:54:08 PM »
Are you trying to mix mouse and keyboard input here? The "starts selected" option is meant to be used for controller / keyboard navigation, which highlights on selection. Mouse highlights on hover instead. You pretty much have to use one or the other.

SpookyFishGames

  • Guest
Re: Re-enabling breaks UIButtonKeys Start Selected?
« Reply #8 on: August 20, 2013, 04:16:54 PM »
No, or at least I don't think so. I've got all the mouse/touch etc. disabled in in my UICamera, the only 2 properties I've got checked in there (that are related to input) are 'Use Keyboard' and 'Use Controller'. Am I right in assuming that this is the only place it needs setting?

My setup is that I've got a 9x5 grid of buttons representing a keyboard, and this needs to be navigated via joypad. I've created each button and applied the UIButtonKeys script to each one, and set up the 4 directions, and it all works fine (by the way, for future reference, is there a quicker way to set up the directions on the ButtonKeys script? took me ages to do 45 buttons) I've also got 2 UIButtonMessage scripts attached to each, one for hover, and one for click. These are needed as I need to update a label with what the user is entering as they navigate the keyboard.

When I first set this up I had it all working, but my default button (the one set as starts selected) wouldn't highlight. It was the active button because if I pressed fire, the click message fired, and if I moved away from it you could tell it was the active button because of where it moved to. However it would not highlight, which is how I ended up in this thread.

I applied the code above to my UIButtonKeys script and now the button is highlighted at start, but if I move away from it, it stays highlighted until I move to it and back again. It's like sending it the hover message using UICamera.Notify is working, but something doesn't know it's been fired so isn't sending the 'UnHover' (assuming that's how it works that is :) )

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Re-enabling breaks UIButtonKeys Start Selected?
« Reply #9 on: August 21, 2013, 10:45:52 AM »
I assume you've looked at the controller input menu example? It does the selection / highlighting of buttons when switching between different windows and it works as expected when you navigate away from the selected button.

SpookyFishGames

  • Guest
Re: Re-enabling breaks UIButtonKeys Start Selected?
« Reply #10 on: August 21, 2013, 11:07:46 AM »
yes, that's were I learned how to set up the the ButtonKeys scripts etc. As I say the navigating away from the selected button works fine, except when it's the first button after the panel has been loaded.

When I first set it up the 'start selected button' (of which I've made sure there is only one) wasn't being highlighted at all, now (after added the code above to ButtonKeys OnEnable) the 'start selected' button is highlighted when it's first loaded, but will not de-highlight until after it's been made selected/de-selected again.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Re-enabling breaks UIButtonKeys Start Selected?
« Reply #11 on: August 22, 2013, 11:42:06 AM »
I'm not sure why that would be the case. I'm also not quite understanding what would be different between yours and the example. In the example the button also gets selected when the panel has been loaded, and it works fine. Are you perhaps causing it to get selected twice somehow?

SpookyFishGames

  • Guest
Re: Re-enabling breaks UIButtonKeys Start Selected?
« Reply #12 on: August 22, 2013, 03:34:52 PM »
OK. To try and get to the bottom of whats going on I have just started a brand new blank project in Unity, and added the NGUI package to the project. Opened the scene for "Example 4 - Controller Input", changed absolutely nothing and ran it in the editor, and it doesn't work either. It displays the dialog fine, but the default/selected button is not highlighted. It's obviously the selected button because I can move up and down the menu and the buttons highlight fine, it's just not highlighted when first started exactly the same as is the case with my project.

I'm using the ngui.unitypackage I downloaded from my purchase email (bought directly from you, not through asset store) and the version numbers of everything I'm using are: Unity Basic 4.1.5f1 - NGUI version 2.6.3

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Re-enabling breaks UIButtonKeys Start Selected?
« Reply #13 on: August 23, 2013, 03:05:12 PM »
Sigh. Please update your NGUI.

SpookyFishGames

  • Guest
Re: Re-enabling breaks UIButtonKeys Start Selected?
« Reply #14 on: August 23, 2013, 03:27:05 PM »
Ok, I'll update and see if it fixes it. Not sure exactly why the 'sigh' was necessary though.