I ask this as I see there are platform dependent compilation that you can say, if iPhone or if Android, but what about the case with Windows 8 tablets and touchscreen computers?
I have a microsoft Surface pro that supports full Windows 8 Pro and has a touchscreen. If I made a desktop version of a game, the pro theoretically could support touch AND keyboard and mouse. If I restricted the touch code to just iPhone or Android, it would force the game to be keyboard and touch only.
I can’t find anything like if (Input.SupportsTouch) {}.
How can I detect if a device supports touch generally without asking directly if it is of a specific type of device ie. iPhone?
4 Answers
4
Currently (it’s been several years since OP asked), the Input class does have a “.touchSupported” and a “.multiTouchEnabled” (as well as others!). I would recommend using those in conjunction with the CrossPlatformInput standard asset to create and deploy a single code-base for your game across all your target platforms
//check if our current system info equals a desktop
if(SystemInfo.deviceType == DeviceType.Desktop){
//we are on a desktop device, so don't use touch
dontUseTouch = true;
}
//if it isn't a desktop, lets see if our device is a handheld device aka a mobile device
else if(SystemInfo.deviceType == DeviceType.Handheld){
//we are on a mobile device, so lets use touch input
dontUseTouch = false;
}
Should be able to figure it out from there.
Under SystemInfo there are a few other things that can help you out, like operating system/device name. Some googling will be required but all “windows 8 touch available” pcs might have something in common. If that is the case, for the example of windows 8 touch supported computers, you might be able to do a contains(“thatCommonString”) to enable/disable touch.
The first place to look is Platform Dependent Compilation:
This is how you can have one code base and one project and deploy to many devices.
Platform Dependent Compilation also lets you check the version of Unity that’s running, which can help when writing code that could be run on many different versions - say you were selling a package on the asset store and wanted to support both Unity 3.5 and Unity 4.1.
And example of how to use this code is here:
void Update () {
#if UNITY_EDITOR || UNITY_STANDALONE || UNITY_WEBPLAYER
movement += new Vector3 (Input.GetAxis("XAxis"), Input.GetAxis("YAxis"), Input.GetAxis("ZAxis"));
#else
movement = Input.acceleration;
#endif
transform.Translate (movement, Space.World);
}
This code takes Input.GetAxis from the Editor, Standalone builds and the Webplayer, otherwise, it uses acceleration from a mobile device… this could be touches or whatever code you’d like.
Other resources you could use could be “MultiTouchEnabled”:
You may want to find a way that you don’t have to poll that every frame, perhaps by enabling/disabling scripts on start based on this value?
Also you could experiment with polling for “TouchCount”:
file:///Applications/Unity/Documentation/ScriptReference/Input-touchCount.html
Have a quick scan of the Input class for more details on getting input from your devices:
I don’t think it’s currently possible.
either detect specific type of device or ask the user if he is using a touch device.
The problem with all of this is if you are using a desktop, using the UNITY_STANDALONE define would assume keyboard and mouse controls and not tell me if it supports touch. I tried forcing mobile controls and it turns out that Windows Desktop doesn't seem to be picking up touches anyway on my Surface Pro. It seems as if Unity doesn't play nice with windows touch screens so my whole idea of asking this question doesn't really work anymore.
– dberroa