Search Unity

Pointer Click fired outside of game object?

Discussion in 'UGUI & TextMesh Pro' started by fwalker, Jun 14, 2017.

  1. fwalker

    fwalker

    Joined:
    Feb 5, 2013
    Posts:
    255
    I have a game object to which I have attached a Event Trigger script. What would cause the Pointer Click event to fire when clicking outside of the component/game object?

    This is what I am doing, in code, to get the location of the click. this is the method called from the Event Trigger on Pointer Click:

    Code (CSharp):
    1.  
    2.  
    3. public void OnTimelineBarClicked(BaseEventData data)
    4.  
    5. {
    6.             PointerEventData pointerData = data as PointerEventData;
    7.  
    8.             Vector2 localCursor;
    9.             if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(pointerData.pointerPress.GetComponent<RectTransform>(), pointerData.position, null, out localCursor))
    10.                 return;
    11.  
    12.             if (IsDebug)
    13.                 Debug.Log("TimeLineclicked " + localCursor);
    14. }[/I]
    15.  
    This code returns negative numbers. Which I would not expect. It also returns numbers that are larger than the actual size (width) of the component. I am only focusing on the x component.

    The component inspector looks like this:



    What am I not understanding ?
     

    Attached Files:

  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
  3. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @fwalker

    hi,

    I don't think you should get that. You actually have a UI GameObject with RectTransform... so it should get the clicks only when you hit it's rect.

    One case that comes to my mind:
    Your UI RectTransform could have a child. Those too will get the clicks.

    But it is hard to say what is going on based on your explanation, I don't see your hierarchy etc.

    Also, remember that there is nothing wrong getting negative coordinates for clicks, clicks in RectTransform are in RectTransform coordinates, which are relative to pivot of RectTransform... so if you have a center pivot, any clicks below pivot and to left of it will be negative values.
     
    Last edited: Jun 19, 2017
    EmilHuzell and fwalkerCirca like this.
  4. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @fwalker

    I just did a quick test in clean scene, it worked just fine with the RectTransform values you used, and using your code.
     
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Nice posts :)
     
  6. fwalkerCirca

    fwalkerCirca

    Joined:
    Apr 10, 2017
    Posts:
    57
    eses,

    I think you are on to something. It did not occur to me to check the pivot. I will investigate and report back :)