Skip to content

Instantly share code, notes, and snippets.

@TanaTanoi
Created September 26, 2018 21:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TanaTanoi/d6a9655de74e53c0f0920d61f252fb13 to your computer and use it in GitHub Desktop.
Save TanaTanoi/d6a9655de74e53c0f0920d61f252fb13 to your computer and use it in GitHub Desktop.
Detecting if the app is running on a Chromebook in Unity
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