Reputation: 4222
I'm a total beginner in Unity and created a function that simply sets the text of a text element when a touch is detected like this:
// Update is called once per frame
void Update () {
if (didTap ()) {
print ("did tap!");
tapText.text = "TAP!";
}
}
private bool didTap() {
print ("checking didtap");
if (Input.touchCount > 0) {
Touch initialTouch = Input.GetTouch (0);
return initialTouch.phase == TouchPhase.Began;
}
return false;
}
When I run this on an actual device (Android phone) it works perfectly, but when I 'click' in the unity editor when running the game nothing happens. I'm running OSX 10.12.2. Anything I need to configure to have the mouse mimic touch events?
Upvotes: 0
Views: 859
Reputation: 9821
There's an odd difference between "touch" input and "mouse" input in Unity. Input.touchCount
will always return 0 while you're running in the Editor but Input.GetMouseButtonDown(0)
works for both the Editor and the first touch of one or more touches while on a mobile build.
You have two options to solve this issue via code (your other option is the remote ryemoss mentioned):
Input.GetMouseButtonDown(0)
and Input.GetMouseButtonUp(0)
.#if UNITY_IOS
, #if UNITY_ANDROID
and #if UNITY_EDITOR
tags to write separate input readers for your platforms and then have them all call into the same functionality.Upvotes: 0
Reputation: 4343
You can duplicate Unity's play mode window onto your mobile device by using the UnityRemote app. This will allow you to interact with your phone's features, while still running Unity on your computer.
UnityRemote requires some setup in Unity along with downloading the app. Consider watching this video: Unity Remote Setup Tutorial
Alternatively, see the answer from Hellium in this question, in conjunction with getting the mouse clicks, to only compile code based on the device used.
Upvotes: 1