- Home /
[Unity Editor] Emulate Touch Input - Still Asking 12/10
Edit: This question still has no answer as of 12/10 - the below response does not answer the question.
Hi, I need to create touch input(s) while in Editor to test everything before deploying to devices. Is there a way to create/emulate/spawn/* touch events using scripts?
getMouseButtonDown does not do what I want (afaik), I want to literally fake a touch input and have everything else run as normal. Ideally, I get to do this via a script, and then can use hard coded values or base it on the mouse location.
My intended purposes are in-editor testing+debugging, and automated testing.
Answer by Graham-Dunnett · Dec 04, 2012 at 02:45 PM
Here's what I'd do.
a) Write some code that runs on the device and captures all the real world touch inputs, and writes these to a file with a time stamp. Copy this file off the device and onto the host computer.
b) Wrap up the input processing logic in the game, so it knows if it is running in the editor or devices (either using Application.isEditor or platform defines (see http://docs.unity3d.com/Documentation/Manual/PlatformDependentCompilation.html).
c) If running in the editor, read the file containing the touch inputs, and feed those into the game at the correct time.
Now you can play the game in the editor with faked, but real-world touch data.
So, yes, there is a way, but you haven't told me what it is. How do I do step C? What's the code, man?
@Narfanator the solution that Graham describes here works by creating a layer around Input.touches that you would use rather than the real Input.touches. If your input code is in one place, then you can substitute your existing input code with one that is fed from file. If your Input code is scattered across a lot of places then you may want to consider refactoring that code so that you have a uniform, wrappable input class. You could also create a class that overrides the name of class Input that wraps the behaviour that you want.
This behaviour would not plug in that easily with existing Unity code that already uses the internal input system, like GUI.
So the answer is "No, you cannot emulate touch input, but you could try this work-around"...?
Yes, that's the answer. The Input class just wraps native code from the engines core which just wraps the API of the target platform. Since a PC (usually) don't have a touch device and doesn't doesn't use the Adroid / iOS API you can't emulate touch input.
Like Statement said, create another layer of abstraction in between to wrap the touch input with your own class. There you're free to emulate what ever you want.