Created
September 26, 2018 21:47
-
-
Save TanaTanoi/d6a9655de74e53c0f0920d61f252fb13 to your computer and use it in GitHub Desktop.
Detecting if the app is running on a Chromebook in Unity
This file contains hidden or 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
private static bool? isChromebook = null; | |
#if UNITY_ANDROID && !UNITY_EDITOR | |
private static AndroidJavaClass androidUnityActivity = null; | |
public static AndroidJavaObject GetUnityActivity() { | |
if (androidUnityActivity == null) { | |
andr oidUnityActivity = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); | |
} | |
return androidUnityActivity.GetStatic<AndroidJavaObject>("currentActivity"); | |
} | |
#endif | |
public static bool IsChromebook { | |
get { | |
if (!isChromebook.HasValue) { | |
#if UNITY_ANDROID && !UNITY_EDITOR | |
try { | |
using (AndroidJavaObject context = GetUnityActivity().Call<AndroidJavaObject>("getApplicationContext")) | |
using (AndroidJavaObject packageManager = context.Call<AndroidJavaObject>("getPackageManager")) { | |
isChromebook = packageManager.Call<bool>("hasSystemFeature", "org.chromium.arc.device_management"); | |
} | |
} catch (System.Exception e) { | |
isChromebook = false; | |
} | |
#else | |
isChromebook = false; | |
#endif | |
} | |
return (bool)isChromebook; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment