Search Unity

  1. We've closed the job boards. If you're looking for work, or looking to hire check out Unity Connect. You can see more information here.
    Dismiss Notice
  2. We're running great holiday deals on subscriptions, swag and Asset Store packages! Take a peek at this blog for more information!
    Dismiss Notice
  3. Check out our Unite Austin 2017 YouTube playlist to catch up on what you missed. More videos coming soon.
    Dismiss Notice
  4. Unity 2017.2 is now released.
    Dismiss Notice
  5. The Unity Gear Store is here to help you look great at your next meetup, user group or conference. With all new Unity apparel, stickers and more!
    Dismiss Notice
  6. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice
  8. Unity 2017.3 beta is now available for download.
    Dismiss Notice

Pointer Click fired outside of game object?

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

  1. fwalker

    fwalker

    Joined:
    Feb 5, 2013
    Posts:
    63
    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:

    [​IMG]

    What am I not understanding ?
     

    Attached Files:

  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    3,525
  3. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    681
    @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
    fwalkerCirca likes this.
  4. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    681
    @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:
    3,525
    Nice posts :)
     
  6. fwalkerCirca

    fwalkerCirca

    Joined:
    Apr 10, 2017
    Posts:
    15
    eses,

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