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

Issue with UI Elements and registering Pointer events for touch input

Discussion in 'UI Toolkit' started by midejonai, Feb 10, 2022.

  1. midejonai

    midejonai

    Joined:
    Oct 11, 2017
    Posts:
    5
    Hello.
    I'm making a custom on-screen joystick to be used with a touchscreen.
    I am using RegisterCallback on a VisualElement to register a set of pointer events, specifically:
    • PointerDownEvent
    • PointerMoveEvent
    • PointerUpEvent
    • PointerLeaveEvent
    The events are being fired as expected and my callbacks are working properly when using a mouse in the editor, however when I use a Simulated Touchscreen through the Input Debug, or if I build on Android, they don't fire at all. As far as I understand, pointer events should be covering touch input as well, right?

    I am using the built in UI Elements and Input System 1.2 on Unity 2021.2.8f1.
    My input actions are the ones being created by default when automatically creating the asset through the Player Input component (which I am not using at the moment).

    Is there some extra step that needs to be taken to make them work? Perhaps I have misunderstood their functionality?
    So far, the only alternative I have come up with is to manually track my touches, or to create a fake mouse, but these would feel too "messy" compared to the clean workflow I have come to expect from the input system.

    Any assistance would be highly appreciated!
     
  2. JuliaP_Unity

    JuliaP_Unity

    Unity Technologies

    Joined:
    Mar 26, 2020
    Posts:
    696
    If I understand correctly you're using the new Input System, correct?

    Can you check that you have an Event System component somewhere in your scene?
     
  3. midejonai

    midejonai

    Joined:
    Oct 11, 2017
    Posts:
    5
    Hi there and thanks for replying!

    New Input System indeed. Yup, I do have an Event System component and it's on the same game object as my Input System UI Input Module.
     
  4. uBenoitA

    uBenoitA

    Unity Technologies

    Joined:
    Apr 15, 2020
    Posts:
    214
    Could you confirm that your setup is working properly with touch by testing it separately with some UGUI objects, say a UI.Canvas and a UI.Button? I'm not suggesting to keep them around in your project, you can get rid of them as soon as you know for sure that they do work, I'm just trying to see if the problem comes from the EventSystem/InputModule side of things.

    Also, what's your Active Input Handling set to in your PlayerSettings/Player/Other Settings? It should be "Input System Package (New)".
     
  5. midejonai

    midejonai

    Joined:
    Oct 11, 2017
    Posts:
    5
    Button on a Canvas works properly with touch.
    That said, UIElements button also works on touch input.

    Note that UI>Point and UI>Click actions through the Input System are working as expected.

    Active input handling is set to "Input System Package (New)" indeed.

    To give you a better idea, here's what I'm doing:

    Code (CSharp):
    1.  
    2. // These are in my Start method
    3. leftJoystick = ui.rootVisualElement.Query<VisualElement>("LeftJoystick").AtIndex(0);
    4. leftJoystick.RegisterCallback<PointerDownEvent>(OnPointerDown);
    5.  
    6.  
    7. //OnPointerDown is defined like so
    8. private void OnPointerDown(PointerDownEvent ev) {
    9. ...
    10. }
     
  6. uBenoitA

    uBenoitA

    Unity Technologies

    Joined:
    Apr 15, 2020
    Posts:
    214
    Erm, that's very strange indeed. If the touch events reach UIToolkit buttons, it means they do reach as far as any other mouse-generated events would. No reason clicking on your LeftJoystick with the mouse should trigger PointerDownEvent while touching it with a finger shouldn't, at the same time as it working properly for a button.

    I can only suggest you submit a bug, because I have no idea what could be happening :-(
     
  7. midejonai

    midejonai

    Joined:
    Oct 11, 2017
    Posts:
    5
    Taking your response into account I spent some more time looking into this.

    I found out that although I was using a pointer event, I was in fact trying to retrieve mouse position values instead of the pointer's. Therefore the issue was in my own code and the events had been firing as expected all along!

    Thank you so much for your assistance, it helped me realize my mistake!
     
  8. CodeWithKyrian

    CodeWithKyrian

    Joined:
    Aug 17, 2017
    Posts:
    9
    Hello, I've been trying to implement an on-screen joystick of my own using UIToolkit too. Do you mind sharing your implementation, or how you structured it?
     
  9. midejonai

    midejonai

    Joined:
    Oct 11, 2017
    Posts:
    5
    @CodeWithKyrian Unfortunately it's been a while since I worked on that standalone implementation and I don't remember the specifics, nor do I have the time to dive into it at the moment.

    That said, the last thing I remember thinking is that I had encountered a separate issue and felt that the time I spent trying to implement this with the UIToolkit and the new input system was a bit of a waste, as I felt everything was overcomplicated for my needs. At least that particular implementation.

    Surely, both packages will have been updated since, especially along with newer Unity Editor versions, and anything I did back then might as well be considered deprecated at this point to be honest.

    Best of luck with your attempt though!
     
  10. CodeWithKyrian

    CodeWithKyrian

    Joined:
    Aug 17, 2017
    Posts:
    9
    Alright. Thanks a lot for the feedback