This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
/
DeviceInfo.cs
57 lines (50 loc) · 2.28 KB
/
DeviceInfo.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using System;
using UnityEngine;
namespace Improbable.Gdk.Mobile
{
public static class DeviceInfo
{
/// <summary>
/// The default IP address to connect your Android Studio emulator to localhost.
/// </summary>
public const string AndroidEmulatorDefaultCallbackIp = "10.0.2.2";
private static MobileDeviceType activeDeviceType = MobileDeviceType.Unknown;
public static MobileDeviceType ActiveDeviceType
{
get
{
if (activeDeviceType != MobileDeviceType.Unknown)
{
return activeDeviceType;
}
#if UNITY_ANDROID
using (var build = new AndroidJavaObject("android.os.Build"))
{
var fingerprint = build.GetStatic<string>("FINGERPRINT");
var model = build.GetStatic<string>("MODEL");
var brand = build.GetStatic<string>("BRAND");
var device = build.GetStatic<string>("DEVICE");
var product = build.GetStatic<string>("PRODUCT");
// How to detect an Android emulator by Stackoverflow:
// https://stackoverflow.com/questions/2799097/how-can-i-detect-when-an-android-application-is-running-in-the-emulator/21505193#21505193
activeDeviceType = (fingerprint.StartsWith("generic") || fingerprint.StartsWith("unknown") ||
model.Contains("google_sdk") || model.Contains("Emulator") ||
model.Contains("Android SDK built for x86") ||
brand.StartsWith("generic") && device.StartsWith("generic") ||
product.Equals("google_sdk"))
? MobileDeviceType.Virtual
: MobileDeviceType.Physical;
}
return activeDeviceType;
#elif UNITY_IOS
activeDeviceType = SystemInfo.deviceModel.Equals("x86_64")
? MobileDeviceType.Virtual
: MobileDeviceType.Physical;
return activeDeviceType;
#else
throw new PlatformNotSupportedException($"{nameof(DeviceInfo)} is only supported while using the iOS or Android platform.");
#endif
}
}
}
}