AndroidPluginBase.cs 6.2 KB

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