BaseAndroidDevice.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright 2016 Nibiru. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. using UnityEngine;
  15. /// @cond
  16. namespace Nxr.Internal {
  17. public abstract class BaseAndroidDevice : BaseARDevice {
  18. #if UNITY_ANDROID
  19. protected AndroidJavaObject androidActivity;
  20. public override void Destroy() {
  21. if (androidActivity != null) {
  22. androidActivity.Dispose();
  23. androidActivity = null;
  24. }
  25. base.Destroy();
  26. }
  27. protected virtual void ConnectToActivity() {
  28. try {
  29. using (AndroidJavaClass player = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) {
  30. androidActivity = player.GetStatic<AndroidJavaObject>("currentActivity");
  31. }
  32. } catch (AndroidJavaException e) {
  33. androidActivity = null;
  34. Debug.LogError("Exception while connecting to the Activity: " + e);
  35. }
  36. }
  37. public static AndroidJavaClass GetClass(string className) {
  38. try {
  39. return new AndroidJavaClass(className);
  40. } catch (AndroidJavaException e) {
  41. Debug.LogError("Exception getting class " + className + ": " + e);
  42. return null;
  43. }
  44. }
  45. public static AndroidJavaObject Create(string className, params object[] args) {
  46. try {
  47. return new AndroidJavaObject(className, args);
  48. } catch (AndroidJavaException e) {
  49. Debug.LogError("Exception creating object " + className + ": " + e);
  50. return null;
  51. }
  52. }
  53. public static bool CallStaticMethod(AndroidJavaObject jo, string name, params object[] args) {
  54. if (jo == null) {
  55. Debug.LogError("Object is null when calling static method " + name);
  56. return false;
  57. }
  58. try {
  59. jo.CallStatic(name, args);
  60. return true;
  61. } catch (AndroidJavaException e) {
  62. Debug.LogError("Exception calling static method " + name + ": " + e);
  63. return false;
  64. }
  65. }
  66. public static bool CallObjectMethod(AndroidJavaObject jo, string name, params object[] args) {
  67. if (jo == null) {
  68. Debug.LogError("Object is null when calling method " + name);
  69. return false;
  70. }
  71. try {
  72. jo.Call(name, args);
  73. return true;
  74. } catch (AndroidJavaException e) {
  75. Debug.LogError("Exception calling method " + name + ": " + e);
  76. return false;
  77. }
  78. }
  79. public static bool CallStaticMethod<T>(ref T result, AndroidJavaObject jo, string name,
  80. params object[] args) {
  81. if (jo == null) {
  82. Debug.LogError("Object is null when calling static method " + name);
  83. return false;
  84. }
  85. try {
  86. result = jo.CallStatic<T>(name, args);
  87. return true;
  88. } catch (AndroidJavaException e) {
  89. Debug.LogError("Exception calling static method " + name + ": " + e);
  90. return false;
  91. }
  92. }
  93. // UI线程中运行
  94. public static void RunOnUIThread(AndroidJavaObject activityObj , AndroidJavaRunnable r)
  95. {
  96. activityObj.Call("runOnUiThread", r);
  97. }
  98. static AndroidJavaClass EnvironmentCls;
  99. // 获取内置SD卡路径
  100. public static string GetAndroidStoragePath()
  101. {
  102. if (Application.platform == RuntimePlatform.Android)
  103. {
  104. if(EnvironmentCls == null) EnvironmentCls = new AndroidJavaClass("android.os.Environment");
  105. return EnvironmentCls.CallStatic<AndroidJavaObject>("getExternalStorageDirectory").Call<string>("getPath");
  106. }
  107. else
  108. return null;
  109. }
  110. public static bool CallObjectMethod<T>(ref T result, AndroidJavaObject jo, string name,
  111. params object[] args) {
  112. if (jo == null) {
  113. Debug.LogError("Object is null when calling method " + name);
  114. return false;
  115. }
  116. try {
  117. result = jo.Call<T>(name, args);
  118. return true;
  119. } catch (AndroidJavaException e) {
  120. Debug.LogError("Exception calling method " + name + ": " + e);
  121. return false;
  122. }
  123. }
  124. #elif UNITY_STANDALONE_WIN || ANDROID_REMOTE_NRR
  125. public static AndroidJavaClass GetClass(string className)
  126. { return null; }
  127. #endif
  128. }
  129. }
  130. /// @endcond