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

4.6 GUI - Touch Position

Discussion in 'Immediate Mode GUI (IMGUI)' started by PedroGV, Oct 8, 2014.

  1. PedroGV

    PedroGV

    Joined:
    Nov 1, 2010
    Posts:
    415
    Is there any way to get the position of a touch on GUI elements?

    Say an image is touched by the player, and I'm observing the pressDown event, which would be the best way to fetch the screen position of that precise touch? Is there any way to get that info from the event itself?
     
  2. PedroGV

    PedroGV

    Joined:
    Nov 1, 2010
    Posts:
    415
    Anyone?
     
  3. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,307
    eventData.touchPosition
     
  4. PedroGV

    PedroGV

    Joined:
    Nov 1, 2010
    Posts:
    415
    How do I get to eventData from the editor/code when I observe such events from the editor itself?
     
  5. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,307
    not sure I follow, is this inside a custom editor script?

    you can get events for your behaviour if you implement some interfaces:

    UnityEngine.EventSystems.IPointerDownHander
    UnityEngine.EventSystems.IPointerUpHander
    UnityEngine.EventSystems.IPointerClickHander

    there's quite a few, once you implement the interface (only add the ones you need), you can add methods:

    OnPointerDown(PointerEventData eventData)
     
  6. PedroGV

    PedroGV

    Joined:
    Nov 1, 2010
    Posts:
    415
    Ah, ok.

    My idea was to call a custom operation from the EventTrigger component on the editor itself and, instead of passing static parameters, using a dynamic one, but Vector2 is not allowed so I will have to manually code on the script.

    Thanks a lot.

    Edit: Picture it as a new way of declaring the signature of allowed functions, like MyFunction(bool), MyFunction(int), MyFunction(string) and so on, to MyFunction(Vector2, bool), etc., and on the editor, you are allowed to only set the second parameter, since you will be receiving automatically the value for the first Vector 2 paratemer.
     
    Last edited: Oct 14, 2014
  7. PedroGV

    PedroGV

    Joined:
    Nov 1, 2010
    Posts:
    415
    Any chance for adding an event to the IEventSystemHandler interface? Say, like: event Action<UIEventType, BaseEventData> OnUIEvent, where UIEventType is an enum for the actions defined in the EventTrigger class? (Deselect, ..., PointerDown, etc.)
     
  8. PedroGV

    PedroGV

    Joined:
    Nov 1, 2010
    Posts:
    415
  9. PedroGV

    PedroGV

    Joined:
    Nov 1, 2010
    Posts:
    415
    So, my question is: how can I get the touch position from an existing EventTrigger without having to create my own UI control from the scracth with a custom editor script?

    Using uGUI 4.6 I have added an image to a canvas and then attached an event trigger to the image for the PointerDown event, that calls an operation OnFoo() of some script of the game object named Foo.

    The issue that I see with this rationale is that there is no way to fetch the touch position of the event, since it is only notifying the game object that the event occurred and only static data is passed to it, like bool, int, float, string or none.

    So, basically, what I want is a way to know where exaclty the image is being touched by the player without having to query each finger on a specific zone.

    For what I can see on the event trigger class definition, that information is stored but neither exposed nor notified.

    So, how can I fetch that info from the event trigger without having to create a custom image element for the editor?
     
  10. PedroGV

    PedroGV

    Joined:
    Nov 1, 2010
    Posts:
    415
    Could anyone on the Unity Team reply, please?
     
  11. PedroGV

    PedroGV

    Joined:
    Nov 1, 2010
    Posts:
    415
    Is querying Input.Touch[] and comparing it with the UI element's rect the only way to achieve this?