2

I have an issue with the input system that I can't figure out.

I have a VisualElement (using the UI Toolkit), and I need to get the "localPosition" of the pointer on click. (It's going to be a game for touch and mouse so I use pointer.)

I can get the local position using a callback:

myVisualElement.RegisterCallback<PointerDownEvent>(OnPointerDown, TrickleDown.TrickleDown);

Then in the function:

private void OnPointerDown(PointerDownEvent evt)
    {
        pointerLocalPos = evt.localPosition;
    }

But I'm also using the new input system for getting the pressed states (started, canceled, etc.):

InputManager.inputControls.Draw.Click.started += ctx => Click_started(ctx);

Here's the problem. I need to get the localPosition within the VisualElement before or just at the beginning when "Click.started" is called. But the Click_started() fucntion will always be called before "OnPointerDown" sets the value for the pointer position. Is there a way to get the localPosition in the "Click_started" function without a callback maybe? Or is there a another of doing this?

KeepCool
  • 497
  • 1
  • 6
  • 24

1 Answers1

1

If delaying the processing is an option, you could try something like this (untested):


InputManager.inputControls.Draw.Click.started += Click_started;


using System.Threading.Tasks;

async void Click_started()
{
    await Task.Yield();
    // now OnPointerDown should have been called, if it was going to be..

}
Milan Egon Votrubec
  • 3,696
  • 2
  • 10
  • 24
  • That's a solution, but I'd rather avoid this workaround, as I feel like it could cause problems. – KeepCool Oct 18 '22 at 00:04
  • @KeepCool it’s a delay of one “frame”, just enough time for the other processes to run before hand. I did think that you’re delaying the process by one frame,and might that cause an issue, but about a week ago, I was looking through the Unity code for a lot of the InputSystemUIInputModule (off the top of my head) and there were comments from the Unity team about delaying processing by a frame, if I recall correctly, for a very similar reason. I’m not in front of the computer at the moment, so I can’t check to see what mechanism they used. – Milan Egon Votrubec Oct 18 '22 at 00:09
  • Oh ok. Unfortunately, it didn't work. Error: "The body of... cannot be an iterator block because 'void' is not an iterator interface type – KeepCool Oct 18 '22 at 00:36
  • @KeepCool sorry, got that mixed up with coroutines. Luckily I pointed out it was untested! Fixed the code. – Milan Egon Votrubec Oct 18 '22 at 00:53
  • Wow! It worked. I feel like it's not an elegant way to do it, but seems to be the only way. Thanks so much! – KeepCool Oct 18 '22 at 19:41