Author Topic: How to Deactivate Button, Turn Button into switch  (Read 4340 times)

Zenneth

  • Guest
How to Deactivate Button, Turn Button into switch
« on: August 09, 2012, 06:41:25 AM »
Hi,

Let say that I have 2 GameStates, and depending on which state the game is in,
The Buttons input will either be disable or enable.

While Disable, no input can be made, and the color tint will be gray out.
While Enable, buttons will return to default color and be in OFF State, then become On State if pressed.

-----------------------------------------------------------------------------

For Disable
The fastest way that I could gather at the moment is simply to change color tint to grey and disable BoxCollider.
But I feel that this is not an appropriate method of utilizing NGUI.

For Enable
I don't know how to change NGUI Button into a switch.

Can anyone give me any suggestion?

THX in advance.

PhilipC

  • Guest
Re: How to Deactivate Button, Turn Button into switch
« Reply #1 on: August 09, 2012, 09:12:01 AM »
If i am reading this correctly you want something like a "sticky" button where the button stay's pressed until pressed again. You would have to write your own script for that (very easy).

If you override UIButton.OnPress(bool) and keep track of presses and based on the current state change the color or not (you would be ignoring the isPressed = false)

UIButton also has a .isEnabled which disables the collider on the button and changes its color to the disabled color.

Zenneth

  • Guest
Re: How to Deactivate Button, Turn Button into switch
« Reply #2 on: August 09, 2012, 09:17:45 AM »

........If you override UIButton.OnPress(bool) ............


Thank you PhilipC, something like this?

Class UISwitchButton : UIButton
{
 //Override
    public OnPress(bool)
   { *Main Code here* }
}

PhilipC

  • Guest
Re: How to Deactivate Button, Turn Button into switch
« Reply #3 on: August 09, 2012, 09:37:07 AM »
UIButton already overrides UIButtonColor.OnPress so you'd want to do something similar.

  1. class UISwitchButton : UIButton
  2. {
  3.     bool mCurrentlyPressed = false;
  4.  
  5.     protected override void OnPress (bool isPressed)
  6.     {
  7.         if (isEnabled && isPressed)
  8.         {
  9.             if (mCurrentlyPressed) // Trigger it to unpressed state
  10.             else // trigger it to the press state
  11.         }
  12.     }
  13. }
  14.  

To see how to trigger between the press and unpressed state look at UIButtonColor.

Note you might also have to override OnHover just so it doesnt change the color when hover off.