Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question When button is released... call a function depending on where it's released.

Discussion in 'Editor & General Support' started by daveinpublic, Jan 6, 2021.

  1. daveinpublic

    daveinpublic

    Joined:
    May 24, 2013
    Posts:
    167
    Hi,

    I want to make a button that calls a function when the mouse is released, but do various actions depending on where it's released.

    I have a large button in the middle of the screen, and various smaller buttons around it. You press it, and then drag your finger on top of one of the smaller buttons, and then release.

    I added an Event Trigger component to the large button that starts a function the moment you have the 'Pointer Down', because I need a process to start. That works fine.

    Then I added a 'Pointer Up' event to the smaller buttons, which didn't work, because the pointer has to originate there to work, and I tried 'Pointer Exit'... which actually calls but unfortunately not when the pointer is up, but when the pointer has left the area of the button.

    Any help is appreciated!
    Thanks - David
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,890
    You may not be able to rely on Unity's built in events for this. Do your own tracking of mouse position and pressed state each frame and figure it out.
     
  3. daveinpublic

    daveinpublic

    Joined:
    May 24, 2013
    Posts:
    167
    What's a good way to track screen position so that it works with both touch input and mouse input?
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,890
    Depends if you care about multitouch or not. If you only care about single touch, Input.mousePosition should work for both mouse and touch.
     
  5. daveinpublic

    daveinpublic

    Joined:
    May 24, 2013
    Posts:
    167
    Thanks for your help.

    I have something that's working a little different way. I still have my main button event triggered by a 'Pointer Down' unity event, and also 'Pointer Up'.

    On the smaller buttons around the outside, I added unity events that trigger when the mouse pointer enters and exits. Those functions change a boolean in the script. When the mouse pointer is released, it checks the boolean.