HykbContext.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using UnityEngine;
  2. /// <summary>
  3. /// For support android context
  4. /// create by chenbaoyang
  5. /// </summary>
  6. namespace com.m3839.sdk
  7. {
  8. public sealed class HykbContext : MonoBehaviour
  9. {
  10. // 横屏
  11. public static int SCREEN_LANDSCAPE = 0;
  12. // 竖屏
  13. public static int SCREEN_PORTRAIT = 1;
  14. private AndroidJavaObject currentActivity;
  15. private static readonly HykbContext _HykbContext = new HykbContext();
  16. /// <summary>
  17. /// 获取当前实例
  18. /// </summary>
  19. /// <returns></returns>
  20. public static HykbContext GetInstance()
  21. {
  22. return _HykbContext;
  23. }
  24. /*
  25. * 获取当前Activity
  26. */
  27. public AndroidJavaObject GetActivity()
  28. {
  29. if (null == currentActivity)
  30. {
  31. currentActivity = new AndroidJavaClass("com.unity3d.player.UnityPlayer")
  32. .GetStatic<AndroidJavaObject>("currentActivity");
  33. }
  34. return currentActivity;
  35. }
  36. /*
  37. * 运行在主UI线程
  38. */
  39. public void RunOnUIThread(AndroidJavaRunnable runnable)
  40. {
  41. GetActivity().Call("runOnUiThread", runnable);
  42. }
  43. /*
  44. * 获取根节点的布局
  45. */
  46. public AndroidJavaObject GetRootLayout()
  47. {
  48. AndroidJavaClass R = new AndroidJavaClass("android.R$id");
  49. return currentActivity.Call<AndroidJavaObject>("findViewById", R.GetStatic<int>("content"));
  50. }
  51. }
  52. }