Created
February 17, 2021 16:29
-
-
Save 0xdw/8b8ebe2569384342736781e00e5c5d40 to your computer and use it in GitHub Desktop.
Unity - Detecting between a tablet/iPad and mobile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum DeviceType { | |
Tablet, | |
Phone | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public static class DeviceTypeChecker { | |
private static float DeviceDiagonalSizeInInches() { | |
float screenWidth = Screen.width / Screen.dpi; | |
float screenHeight = Screen.height / Screen.dpi; | |
float diagonalInches = Mathf.Sqrt(Mathf.Pow(screenWidth, 2) + Mathf.Pow(screenHeight, 2)); | |
return diagonalInches; | |
} | |
public static DeviceType GetDeviceType() { | |
#if UNITY_IOS | |
bool deviceIsIpad = UnityEngine.iOS.Device.generation.ToString().Contains("iPad"); | |
if (deviceIsIpad) { | |
return DeviceType.Tablet; | |
} | |
bool deviceIsIphone = UnityEngine.iOS.Device.generation.ToString().Contains("iPhone"); | |
if (deviceIsIphone) { | |
return DeviceType.Phone; | |
} | |
#elif UNITY_ANDROID | |
float aspectRatio = Mathf.Max(Screen.width, Screen.height) / Mathf.Min(Screen.width, Screen.height); | |
bool tablet = (DeviceDiagonalSizeInInches() > 6.5f && aspectRatio < 2f); | |
return tablet ? DeviceType.Tablet : DeviceType.Phone; | |
#endif | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment