Unity Button Pressed State | Create Two State Button with hover highlight in Unity3D

You often need to preserve the pressed state of a Unity button control to show the user that the button is active. For example, when clicking a button to open a window, the button state should indicate that the window is active. To achieve this, you can use sprite swapping with C# scripting.

Unity Two State Button Toggle:

Unity doesn't provide two-state buttons by default or preserve the button state after clicking. While Unity does provide Toggle UI that offers similar behavior, it lacks the mouse hover highlight feature when the toggle state is active. Here's a workaround to preserve the button's pressed state on click. On the second click, the button will revert to its normal state. Attach the script available here to your button and see the magic.

 

All you need to do is properly provide all the sprites in the button component.

Post a Comment

0 Comments