Skip to content

Instantly share code, notes, and snippets.

@0xdw
Created February 17, 2021 16:29
Show Gist options
  • Save 0xdw/8b8ebe2569384342736781e00e5c5d40 to your computer and use it in GitHub Desktop.
Save 0xdw/8b8ebe2569384342736781e00e5c5d40 to your computer and use it in GitHub Desktop.
Unity - Detecting between a tablet/iPad and mobile
public enum DeviceType {
Tablet,
Phone
}
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