- Home /
How can I tell if the device I am on supports touch?
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?
Answer by moobchunks · Feb 12, 2016 at 10:49 AM
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
Answer by trs9556 · May 04, 2013 at 04:20 AM
http://docs.unity3d.com/Documentation/ScriptReference/SystemInfo.html
//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.
Answer by Adam-Buckner · May 02, 2013 at 11:59 AM
The first place to look is Platform Dependent Compilation: http://docs.unity3d.com/Documentation/Manual/PlatformDependentCompilation.html
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": http://docs.unity3d.com/Documentation/ScriptReference/Input-multiTouchEnabled.html
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: http://docs.unity3d.com/Documentation/ScriptReference/Input.html
Please Note: When dealing with the Platform Dependent Compilation, UNITY_EDITOR is a separate enum! This makes it a different check from the other platforms. As such #if UNITY_EDITOR does not mean !UNITY_IPHON$$anonymous$$ As I said, these are two different checks, so you can have both #if UNITY_EDITOR and #if UNITY_IPHONE return true if you are in the editor and have the build target set for iOS devices.
If you are building specifically to, for argument's sake, Unity iOS, but want keyboard controls for testing, you would need to use something like:
#if UNITY_EDITOR
// Input$$anonymous$$anager Code for $$anonymous$$eyboards and $$anonymous$$ice
#else
// Touches and Acceleration from the iOS device
#endif
If you are coding for mobiles, I believe you can say something like this:
#if UNITY_EDITOR || (!UNITY_IPHONE && !UNITY_ANDROID)
// Input$$anonymous$$anager Code for $$anonymous$$eyboards and $$anonymous$$ice
#else
// Touches and Acceleration from the iOS device
#endif
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.
Answer by dorpeleg · Apr 21, 2013 at 12:27 AM
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.
I feel like even if you asked, you would still be doing if statement checks to see "if touch device" which would count against you at runtime.
Your answer
Follow this Question
Related Questions
What is the command to check whether player is touching the screen or not?? 1 Answer
Touch and release type of controll in Unity (Android). How to make it? 0 Answers
How to get position of touch on touch screen 1 Answer
ITouch multi touch with joystick problem 0 Answers
Processes two touches instead of one 0 Answers