AndroidUtils.cs 960 B

123456789101112131415161718192021222324252627282930313233
  1. #if UNITY_ANDROID && !UNITY_EDITOR
  2. #define USE_ANDROID
  3. #endif
  4. /**
  5. * @author zeh fernando
  6. */
  7. using System;
  8. using UnityEngine;
  9. class AndroidUtils {
  10. // Use:
  11. // runOnAndroidUiThread(someMethod)
  12. public static void runOnAndroidUiThread(Action target) {
  13. // Calls an Android activity on the UI Thread (needed for some interactions with Views)
  14. #if USE_ANDROID
  15. using (var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) {
  16. using (var activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity")) {
  17. activity.Call("runOnUiThread", new AndroidJavaRunnable(target));
  18. // Other examples:
  19. //string a = activity.Call<string>("someNomw", param);
  20. //window.Call("setFlags", flagsValue, -1); // (int)0x7FFFFFFF
  21. //window.Call("setStatusBarColor", unchecked((int)_statusBarColor)); // for an uint
  22. }
  23. }
  24. #else
  25. Debug.Log("[Android] Would call [" + target + "] later on the UI thread");
  26. #endif
  27. }
  28. }