AndroidLib.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEngine;
  6. using UnityEngine.Android;
  7. using XRTool.Util;
  8. namespace XRTool.Util
  9. {
  10. /// <summary>
  11. /// 安卓与Unity接口类交互
  12. /// </summary>
  13. public class AndroidLib : UnitySingleton<AndroidLib>
  14. {
  15. private static AndroidJavaObject currentActivity;
  16. /// <summary>
  17. /// 安卓的Activity
  18. /// </summary>
  19. public static AndroidJavaObject MainActivity
  20. {
  21. get
  22. {
  23. #if UNITY_ANDROID
  24. if (currentActivity == null)
  25. {
  26. AndroidJavaClass up = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
  27. currentActivity = up.GetStatic<AndroidJavaObject>("currentActivity");
  28. }
  29. #endif
  30. return currentActivity;
  31. }
  32. }
  33. protected override void Awake()
  34. {
  35. name = typeof(AndroidLib).Name;
  36. base.Awake();
  37. DontDestroyOnLoad(gameObject);
  38. }
  39. /// <summary>
  40. /// 安卓回调接口,安卓调用Unity
  41. /// 统一接口?
  42. ///
  43. /// </summary>
  44. public void AndroidCallBack(string funName)
  45. {
  46. }
  47. /// <summary>
  48. /// 调用安卓代码
  49. /// </summary>
  50. public static T CallAndLibMethod<T>(AndroidJavaObject andObj, bool isStatic, string meshod, params object[] args)
  51. {
  52. if (andObj != null)
  53. {
  54. if (isStatic)
  55. {
  56. return andObj.CallStatic<T>(meshod, args);
  57. }
  58. return andObj.Call<T>(meshod, args);
  59. }
  60. return default(T);
  61. }
  62. /// <summary>
  63. /// 调用安卓代码
  64. /// </summary>
  65. public static T CallAndLibMethod<T>(AndroidJavaObject andObj, bool isStatic, string meshod)
  66. {
  67. if (andObj != null)
  68. {
  69. if (isStatic)
  70. {
  71. return andObj.CallStatic<T>(meshod);
  72. }
  73. return andObj.Call<T>(meshod);
  74. }
  75. return default(T);
  76. }
  77. /// <summary>
  78. /// 调用安卓代码
  79. /// </summary>
  80. public static void CallAndLibMethod(AndroidJavaObject andObj, bool isStatic, string meshod, params object[] args)
  81. {
  82. if (andObj != null)
  83. {
  84. if (isStatic)
  85. {
  86. andObj.CallStatic(meshod, args);
  87. }
  88. else
  89. {
  90. andObj.Call(meshod, args);
  91. }
  92. }
  93. }
  94. public static bool CheckCamera()
  95. {
  96. if (!Permission.HasUserAuthorizedPermission(Permission.Camera))
  97. {
  98. Permission.RequestUserPermission(Permission.Camera);
  99. return false;
  100. }
  101. return true;
  102. }
  103. /// <summary>
  104. /// 调用安卓代码
  105. /// </summary>
  106. public static void CallAndLibMethod(AndroidJavaObject andObj, bool isStatic, string meshod)
  107. {
  108. if (andObj != null)
  109. {
  110. if (isStatic)
  111. {
  112. andObj.CallStatic(meshod);
  113. }
  114. else
  115. {
  116. andObj.Call(meshod);
  117. }
  118. }
  119. }
  120. /// <summary>
  121. /// 调用安卓代码
  122. /// </summary>
  123. public static T CallAndLibMethod<T>(bool isStatic, string meshod, params object[] args)
  124. {
  125. return CallAndLibMethod<T>(MainActivity,isStatic, meshod, args);
  126. }
  127. /// <summary>
  128. /// 调用安卓代码
  129. /// </summary>
  130. public static T CallAndLibMethod<T>( bool isStatic, string meshod)
  131. {
  132. return CallAndLibMethod<T>(MainActivity, isStatic, meshod);
  133. }
  134. /// <summary>
  135. /// 调用安卓代码
  136. /// </summary>
  137. public static void CallAndLibMethod(bool isStatic, string meshod, params object[] args)
  138. {
  139. CallAndLibMethod(MainActivity, isStatic, meshod, args);
  140. }
  141. /// <summary>
  142. /// 调用安卓代码
  143. /// </summary>
  144. public static void CallAndLibMethod(bool isStatic, string meshod)
  145. {
  146. CallAndLibMethod(MainActivity, isStatic, meshod);
  147. }
  148. }
  149. }