I have an android game on Unity that isn't responding fast enough to touches (from what I've searched Unity has indeed an input lag problem on Android), so I am trying to create an Android Library with a method that gets touch data and sends it back to the caller (my own game on Unity).
I thought it would be a breeze to do this, but turns out I don't seem to be able to "poll" for touch data, I must get it through events. So I had to do some hacking/guessing to get Unity's view that processes touch, but when I call "setOnTouchListener" it overwrites the original listener, so my Unity game doesn't get any touches anymore.
So my question is: any suggestion on how to I solve this problem?
From what I can tell I could solve it by one of these two options:
1) somehow polling for touch information (instead of attaching a listener), but I couldn't find any way of doing this.
2) or getting the previous listener so I can pass the touch to it after I get what I need.
or of course 3) give up and leave the input lagged and live with the hundreds of bad reviews I'm getting due to this (I have just migrated my game from an old engine that didn't have this lag, so my players are furious).
Thanks
The issue you described is well known to the Unity community. You don't need to give up or make your own plugin. Unity has been working on an new Input System for years. It is still in experimental mode but looks promising.
With the old API that's too slow, you get touch on the screen with:
void Update()
{
foreach (Touch touch in Input.touches)
{
if (touch.phase == TouchPhase.Began)
{
Vector2 touchLocation = touch.position;
}
}
}
With the new low level InputSystem API, this is how you get the touch information:
public int touchIndex = 0;
void Update()
{
Touchscreen touchscreen = UnityEngine.Experimental.Input.Touchscreen.current;
if (touchscreen.allTouchControls[touchIndex].ReadValue().phase != PointerPhase.None &&
touchscreen.allTouchControls[touchIndex].ReadValue().phase != PointerPhase.Ended)
{
Vector2 touchLocation = touchscreen.allTouchControls[touchIndex].ReadValue().position;
}
}
You can get the new InputSystem API here. You will need and above Unity 2018.2 for this. Finally, if you run into any problem with this API, you can let them know in this forum and it will be fixed ASAP.