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. Unity 2022.2 is now available as the latest Tech release.
    Dismiss Notice
  3. We are making some changes to the DOTS forums.
    Dismiss Notice
  4. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    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:
    70
    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,266
    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:
    70
    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,266
    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:
    70
    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.