1
\$\begingroup\$

So I'm fine with gameplay coding, but menus are something I'm still very new to.

I was watching Unity's own video on how to deal with Buttons and I thought I'd try mock up my own button animation much like they had. It works wonderfully, there's just one minor issue.

So a Button can go from 'any state', so either pressed, highlighted or normal (or disabled but I'm not using that). When I hover it works but it'll go straight back to normal once it's 'time'/'cycle' has completed. I figured that to fix this, I ought to add a transition between Highlighted and Normal.

This does work, providing I say that 'Normal' must be true before transitioning between the 2. Here's my issue however - when I click, the button hits pressed fine, then goes back to highlighted, and won't go back to normal unless I just click the background/anything that isn't the Button.

How can I get it to go straight to normal once the mouse is no longer on the button? I thought maybe a trigger collider with OnExitTrigger type function but that seems messy, isn't there a way to do this IN animator?

A Gif showing off the issue I've been talking about

\$\endgroup\$
0
\$\begingroup\$

This can be done with an Pointer Exit Event Trigger. But there is a little more work if you want to do it without coding the whole thing.

enter image description here

Use Boolean for Pressed and Highlighted instead of Trigger parameter on transition conditions. And add an Event Trigger of Pointer Exit having Button component in it with OnDeselect method selected.

Transitions: State_A >> State_B (Bool)

Normal >> Highlighted (Highlighted true)

Highlighted >> Pressed (Pressed true)

Pressed >> Highlighted (Pressed false)

Highlighted >> normal (Highlighted false)

But still suggest to do it programmatically for more control. You will be using a manually created animator controller for it instead of auto generated one.

\$\endgroup\$
  • \$\begingroup\$ You sir are a saint. That works beautifully, just the transitions out can be a tad clunky but that's probably because I've turned off exit time or hahve too short a animation period, I don't know. But thank you! When you say more control, mind elaborating a little? I mean it fits for now because it's a simple menu and I'm just building a foundation of how to do the UI better and the anim does the best visual feedback, so mind just elaborating a bit? \$\endgroup\$ – Bearzerk Aug 24 '16 at 23:02
  • \$\begingroup\$ If you mean they are transiting too fast then you can tweak transition duration when selection a specific transition. You also can edit them in animation window like other usual clips. About more control is, if you you need more than 4 clip or just 2. You can't use more than 4 states without modifying the source code or overriding them. All triggers are called even if you delete the states for checking as they are auto generated. So for hundreds of buttons may be an issue. For small cases like yours it should be working just fine. \$\endgroup\$ – Sourav Paul Aug 25 '16 at 4:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.