AndroidPluginBase.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace SC.XR.Unity
  5. {
  6. public abstract class AndroidPluginBase : SCModule
  7. {
  8. private static AndroidJavaObject mCurrentActivity = null;
  9. private static AndroidJavaObject mCurrentRtcActivity = null;
  10. private static AndroidJavaClass mUnityPlayerClass = null;
  11. private static AndroidJavaObject mContext = null;
  12. protected static AndroidJavaObject CurrentActivity
  13. {
  14. get
  15. {
  16. if (Application.platform == RuntimePlatform.Android)
  17. {
  18. if (mCurrentActivity == null && UnityPlayerClass != null)
  19. {
  20. mCurrentActivity = UnityPlayerClass.GetStatic<AndroidJavaObject>("currentActivity");
  21. }
  22. }
  23. return mCurrentActivity;
  24. }
  25. }
  26. protected static AndroidJavaObject CurrentRtcActivity
  27. {
  28. get
  29. {
  30. if (Application.platform == RuntimePlatform.Android)
  31. {
  32. if (mCurrentRtcActivity == null && UnityPlayerClass != null)
  33. {
  34. var javaClass = new AndroidJavaClass("org.mediasoup.droid.demo.rtc.unitytojava");
  35. var javaObject = javaClass.CallStatic<AndroidJavaObject>("instance", UnityPlayerClass.GetStatic<AndroidJavaObject>("currentActivity"));
  36. mCurrentRtcActivity = javaObject;
  37. }
  38. }
  39. return mCurrentRtcActivity;
  40. }
  41. }
  42. protected static AndroidJavaClass UnityPlayerClass
  43. {
  44. get
  45. {
  46. if (Application.platform == RuntimePlatform.Android)
  47. {
  48. if (mUnityPlayerClass == null)
  49. {
  50. mUnityPlayerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
  51. }
  52. }
  53. return mUnityPlayerClass;
  54. }
  55. }
  56. protected static AndroidJavaObject Context
  57. {
  58. get
  59. {
  60. if (Application.platform == RuntimePlatform.Android)
  61. {
  62. if (mContext == null)
  63. {
  64. mContext = CurrentActivityFunctionCall<AndroidJavaObject>("getApplicationContext");
  65. }
  66. }
  67. return mContext;
  68. }
  69. }
  70. protected static List<AndroidJavaProxy> androidListernerList = new List<AndroidJavaProxy>();
  71. public static void AddListener(string setCallBackFunctionName, AndroidJavaProxy callBack)
  72. {
  73. if (Application.platform == RuntimePlatform.Android)
  74. {
  75. CurrentActivity.Call(setCallBackFunctionName, callBack);
  76. androidListernerList.Add(callBack);
  77. }
  78. }
  79. public static void ObjectAddListener(AndroidJavaObject androidObject, string setCallBackFunctionName, AndroidJavaProxy callBack)
  80. {
  81. if (Application.platform == RuntimePlatform.Android)
  82. {
  83. if (androidObject != null)
  84. {
  85. androidObject.Call(setCallBackFunctionName, callBack);
  86. androidListernerList.Add(callBack);
  87. }
  88. }
  89. }
  90. public static AndroidJavaClass GetAndroidJavaClass(string classPatch)
  91. {
  92. if (Application.platform == RuntimePlatform.Android)
  93. {
  94. try
  95. {
  96. return new AndroidJavaClass(classPatch);
  97. }
  98. catch (Exception e)
  99. {
  100. Debug.LogError(e);
  101. }
  102. }
  103. return null;
  104. }
  105. /// <summary>
  106. /// 实例方法
  107. /// </summary>
  108. /// <typeparam name="T"></typeparam>
  109. /// <param name="androidJavaObject"></param>
  110. /// <param name="callName"></param>
  111. /// <param name="args"></param>
  112. /// <returns></returns>
  113. public static T ObjectFunctionCall<T>(AndroidJavaObject androidJavaObject, string callName, params object[] args)
  114. {
  115. if (androidJavaObject != null)
  116. {
  117. try
  118. {
  119. return androidJavaObject.Call<T>(callName, args);
  120. }
  121. catch (Exception e)
  122. {
  123. Debug.LogError(e);
  124. }
  125. }
  126. return default(T);
  127. }
  128. public static void ObjectFunctionCall(AndroidJavaObject androidJavaObject, string callName, params object[] args)
  129. {
  130. if (androidJavaObject != null)
  131. {
  132. try
  133. {
  134. androidJavaObject.Call(callName, args);
  135. }
  136. catch (Exception e)
  137. {
  138. Debug.LogError(e);
  139. }
  140. }
  141. }
  142. public static T ObjectFunctionCall2<T>(AndroidJavaObject androidJavaObject, string callName, object args)
  143. {
  144. if (androidJavaObject != null)
  145. {
  146. try
  147. {
  148. return androidJavaObject.Call<T>(callName, args);
  149. }
  150. catch (Exception e)
  151. {
  152. Debug.LogError(e);
  153. }
  154. }
  155. return default(T);
  156. }
  157. public static void ObjectFunctionCall2(AndroidJavaObject androidJavaObject, string callName, object args)
  158. {
  159. if (androidJavaObject != null)
  160. {
  161. try
  162. {
  163. androidJavaObject.Call(callName, args);
  164. }
  165. catch (Exception e)
  166. {
  167. Debug.LogError(e);
  168. }
  169. }
  170. }
  171. public static T CurrentActivityFunctionCall<T>(string callName, params object[] args)
  172. {
  173. if (CurrentActivity != null)
  174. {
  175. try
  176. {
  177. return CurrentActivity.Call<T>(callName, args);
  178. }
  179. catch (Exception e)
  180. {
  181. Debug.LogError(e);
  182. }
  183. }
  184. return default(T);
  185. }
  186. public static void CurrentActivityFunctionCall(string callName, params object[] args)
  187. {
  188. if (CurrentActivity != null)
  189. {
  190. try
  191. {
  192. CurrentActivity.Call(callName, args);
  193. }
  194. catch (Exception e)
  195. {
  196. Debug.LogError(e);
  197. }
  198. }
  199. }
  200. /// <summary>
  201. /// 类方法
  202. /// </summary>
  203. /// <param name="androidJavaClass"></param>
  204. /// <param name="callName"></param>
  205. /// <param name="args"></param>
  206. public static void ClassFunctionCallStatic(AndroidJavaClass androidJavaClass, string callName, params object[] args)
  207. {
  208. if (androidJavaClass != null)
  209. {
  210. try
  211. {
  212. androidJavaClass.CallStatic(callName, args);
  213. }
  214. catch (Exception e)
  215. {
  216. Debug.LogError(e);
  217. }
  218. }
  219. }
  220. public static T ClassFunctionCallStatic<T>(AndroidJavaClass androidJavaClass, string callName, params object[] args)
  221. {
  222. if (androidJavaClass != null)
  223. {
  224. try
  225. {
  226. return androidJavaClass.CallStatic<T>(callName, args);
  227. }
  228. catch (Exception e)
  229. {
  230. Debug.LogError(e);
  231. }
  232. }
  233. return default(T);
  234. }
  235. }
  236. }