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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Unity UI Button: OnClick: Pass Click Position[SOLVED]

Discussion in 'Editor & General Support' started by renman3000, May 23, 2017.

  1. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,683
    Hi, I am building for mobile, so when my player clicks a button, if I need to know the finger position (which is no longer on screen, as it was a released tap), is there a Unity UI feature that allows for specific information, like tap pos to be gathered via the UI button?

    I can just monitor in update, but I was hoping an embedded solution.


    Thank you.
     
  2. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
  3. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,683

    thanks.
    At the moment, I am using this solution...

    Code (csharp):
    1.  
    2.     void Start () {
    3.          ui_btn_move.onClick.AddListener(() => OnClickAction(Input.mousePosition));
    4.  
    5.  
    6.     }
    7.     void OnClickAction (Vector2 mousePos){
    8.         print ("ON CLICK: mouse pos: " + mousePos);
    9.         moveTo (mousePos);
    10.     }
     
  4. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Ah, You caught that!

    I had removed that post because it doesn't handle edge cases very well. To support multiple fingers at the same time, you would need to track the fingers. If you're tracking the fingers, you might as well just check which ones were near the button and get the data that way...

    With OnPointerUp you get much more reliable (and detailed) data. But if it works, it works!
     
    renman3000 likes this.