I was wondering if you create a button in unity if your able to both use a mouse to click it and use it on a touch screen to select. I’m unsure if I create a button if when I release it on ipad or ipod if it will react to touches instead of clicks ? or do I need to code it a certain way ?
In general GUI buttons work the same on touchscreen.
If you draw a simple button with GUI.Button, it will work when somebody taps it on an iPad or a phone.
However… Practically speaking, you will likely have to do some things different with you code to release on multiple platforms.
The Input manager does a decent job of guessing how to relate touch input to mouse input. For example, it will treat the movement of the first touch like it would treat the user holding the left mouse button and moving their mouse.
Once you start doing more complicated things with the input, however, you’ll have to have some touchscreen specific code.
The answer is
#YES, EXACTLY THE SAME … BUT …
there’s one big problem, the unityGUI system
#ONLY HANDLES ONE CLICK AT A TIME !!!
That’s the secret. Once the iPad/Phone user touches in two positions – it no longer works. (i.e., not only does it not read two touches, but it does not even get one touch when there are two touches on the screen.) Bummer.
In practice, you can simply use code like this …
if ( Input.GetMouseButtonDown(0) )
{
// or GetMouseButton for continuous...
Input.mousePosition.y .. blah blah
}
Most of the time, because very conveniently that will work both in your iPad, and in Unity on your Mac when you click play in the editor. It’s great.
But in practice, for production code to ship to customers, you need to use “real” iPhone/Pad code…
if ((Input.touchCount == 1) &&
(Input.GetTouch(0).phase == TouchPhase.Began) )
{
Input.GetTouch(0).position .. blah blah
}
In short, realistically, you can’t ship code using the “awesome quick” method. (Because of the “one touch only” problem.) But, the “awesome quick method” is awesome because it also works when you run the app on your Mac during development and you’re using a mouse rather than touches. Hope it helps!