I’ve been playing around with buttons and noticed an interesting behavior. Simply put, if a user clicks down on a button, drags the cursor outside of the button, then releases on the mouse, the OnClick() methods WILL NOT fire (my expected behaivor) and the Button Selected transition WILL occur (not my expected behavior).
To demonstrate I will be using the following 3 button sprites and 1 OnClick() method:
With my setup, I will discuss the following use cases:
- I click and release with my mouse on the button
- I click with my mouse on the button but release with my mouse off the button
Let’s first discuss the onclick behavior. As the documentation states the onclick behavior is how I expect:
So in case 1, my onclick method fires but in case 2 it does now. However I find it interesting that the Selected Sprite does not interact in this way. The button will transition to the selected sprite in BOTH cases.
To solve this, currently I’m writing additional methods to add to onclick() which will manipulate the sprite. In this way I’m having the transition of my button tied to the onclick().
I’m curious if anyone else has other ideas or disagrees with my assessment and can tell me why the selected sprite is expected behavior.
Another additional enhancement I’d love to see is for the Pressed sprite. Current behavior is if I click down then drag my mouse outside of the button, the Pressed sprite will remain there until I release. I think it’d be cool if the Pressed sprite would also take into account the mouse location, so while you held the mouse down it would also detect if the mouse is withing the button. This would be very useful for mobile games since lots of times a user might push down on a button the move their finger away. I think I have an idea of how to do this with a script (add a collider and then have on trigger enters and exits) but I probably won’t get around to it.
NOTE: I noticed in the documentation there is no longer a selected sprite, my unity version is 2020.1.1f1 Personal