Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Button Selected should be aligned with it's OnClick(), currently it behaves differently

Discussion in 'UGUI & TextMesh Pro' started by kevinlwu168, Oct 29, 2020.

  1. kevinlwu168

    kevinlwu168

    Joined:
    Sep 3, 2020
    Posts:
    2
    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:

    upload_2020-10-29_9-35-13.png


    With my setup, I will discuss the following use cases:

    1. I click and release with my mouse on the button
    2. 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:

    upload_2020-10-29_9-32-29.png

    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().

    upload_2020-10-29_9-40-32.png

    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
     
    Last edited: Oct 29, 2020
    tmendez likes this.
  2. tmendez

    tmendez

    Joined:
    Oct 12, 2015
    Posts:
    39
    This behavior has been bothering me too. I think if you click and hold, and move off of the button, once you leave the button it should go back to the default state.

    It is default behavior, so the button states should reflect that. :rolleyes:
     
    kevinlwu168 likes this.