AndroidPluginBase.cs 7.0 KB

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