Author Topic: How-To: Currently selected button?  (Read 7306 times)

SuperTalesNat

  • Guest
How-To: Currently selected button?
« on: March 15, 2013, 01:45:03 PM »
I have a list of UIGrid based buttons that change the background music track inside of a game. Currently they are just buttons that when pressed change the current track. What i'd like to do is have the currently selected button display differently, either scaled, or colored, potentially even animated in a loop, to show that its the active button. As a player selects another button, it would then turn the last selected one off, and the current one on, and so forth.

Before i hop into it later today, i figured i would put out the question to the forum on the best way to go about this with NGUI.

My thoughts were to create a list of GameObjects in a script on a parent object, then assign the buttons to those list items. The buttons would then have a UIButtonMessage that calls a 'for' loop in the script that sets a tween, or multiple, off/on based on the current selected button. This would run each time a button would be pressed.

Is this the only/best way to go about this in NGUI, or is there an easier/better way?

Nathaniel Hunter
SuperTales Inc.

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: How-To: Currently selected button?
« Reply #1 on: March 16, 2013, 10:07:58 AM »
I would do it something like this

  1. MyButtonScript[] myButtons;
  2. int activeButtonIndex = 0;
  3.  

when a button is clicked or some other event changes the active button, i would channel it through this buttonhandler script and disable all except the one that's supposed to be active.

In MyButtonScript you can just define a Activate/Deactivate method, which handles any animation that needs to be done.

SuperTalesNat

  • Guest
Re: How-To: Currently selected button?
« Reply #2 on: March 21, 2013, 12:19:14 AM »
Looks like only 2 lines of code came through Nicki, or was that just an example of creating a list and then the INT for the currently selected?

Nathaniel

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: How-To: Currently selected button?
« Reply #3 on: March 21, 2013, 05:26:33 AM »
That was intended. ;)

If your "MyButtonScript" has Activate and Deactivate methods, you should just run through the array and deactivate everything and activate the one you want active; then the individual buttons hold the state at any given moment, while you just deactivate/activate on clicks.

Instead of using UIButtonMessage, have the lines I wrote above in a script that holds references to all the buttons, so that you can attach a UIEventListener to each button from there:
  1. UIEventlisterner.Get(myButtons[0]).onClick += ActivateClicked;
  2.  
  3.  
  4. void ActivateClicked(GameObject sender)
  5. {
  6.   for (int i ...)
  7.   {
  8.      if (myButtons[i].gameObject == sender)
  9.      {
  10.        //this button was clicked!!
  11.        activeButtonIndex = i;
  12.        myButtons[i].Activate();
  13.      }
  14.      else
  15.     {
  16.       myButtons[i].Deactivate();
  17.     }
  18.  }
  19. }
  20.  

Something like that would work.

MaceikaDev

  • Guest
Re: How-To: Currently selected button?
« Reply #4 on: April 23, 2013, 12:35:51 PM »
Was looking at this and i am wondering does the UIEventListner call go in my update? I am thinking it does. also this could work for a scroll panel as a way to make the little images act as buttons..

MaceikaDev

  • Guest
Re: How-To: Currently selected button?
« Reply #5 on: April 23, 2013, 01:27:49 PM »
Also how do you go about using onPress? i am using this for a touch application..

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: How-To: Currently selected button?
« Reply #6 on: April 24, 2013, 02:32:09 AM »
UIEventListener does not go in the update. You have to setup the listener once, so do it when you setup your button - for instance on Start().

Take a look inside UIEventListener, it's relatively simple to understand once you see it.

You can do something like this:
  1. UIEventlisterner.Get(myButton.gameObject]).onPress += MyButtonPressed;
  2. void MyButtonPressed(GameObject sender, bool pressed)
  3. {
  4. if (pressed)
  5. Debug.Log("Touch down on " + myButton.name);
  6. else
  7. Debug.Log("Touch up");
  8. }

MaceikaDev

  • Guest
Re: How-To: Currently selected button?
« Reply #7 on: April 24, 2013, 09:31:41 AM »
Thanks Nicki.. Yeah i figured it out :)..