123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- // Copyright 2016 Nibiru. All rights reserved.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- using UnityEngine;
- /// @cond
- namespace Nxr.Internal {
- public abstract class BaseAndroidDevice : BaseARDevice {
- #if UNITY_ANDROID
- protected AndroidJavaObject androidActivity;
- public override void Destroy() {
- if (androidActivity != null) {
- androidActivity.Dispose();
- androidActivity = null;
- }
- base.Destroy();
- }
- protected virtual void ConnectToActivity() {
- try {
- using (AndroidJavaClass player = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) {
- androidActivity = player.GetStatic<AndroidJavaObject>("currentActivity");
- }
- } catch (AndroidJavaException e) {
- androidActivity = null;
- Debug.LogError("Exception while connecting to the Activity: " + e);
- }
- }
- public static AndroidJavaClass GetClass(string className) {
- try {
- return new AndroidJavaClass(className);
- } catch (AndroidJavaException e) {
- Debug.LogError("Exception getting class " + className + ": " + e);
- return null;
- }
- }
- public static AndroidJavaObject Create(string className, params object[] args) {
- try {
- return new AndroidJavaObject(className, args);
- } catch (AndroidJavaException e) {
- Debug.LogError("Exception creating object " + className + ": " + e);
- return null;
- }
- }
- public static bool CallStaticMethod(AndroidJavaObject jo, string name, params object[] args) {
- if (jo == null) {
- Debug.LogError("Object is null when calling static method " + name);
- return false;
- }
- try {
- jo.CallStatic(name, args);
- return true;
- } catch (AndroidJavaException e) {
- Debug.LogError("Exception calling static method " + name + ": " + e);
- return false;
- }
- }
- public static bool CallObjectMethod(AndroidJavaObject jo, string name, params object[] args) {
- if (jo == null) {
- Debug.LogError("Object is null when calling method " + name);
- return false;
- }
- try {
- jo.Call(name, args);
- return true;
- } catch (AndroidJavaException e) {
- Debug.LogError("Exception calling method " + name + ": " + e);
- return false;
- }
- }
- public static bool CallStaticMethod<T>(ref T result, AndroidJavaObject jo, string name,
- params object[] args) {
- if (jo == null) {
- Debug.LogError("Object is null when calling static method " + name);
- return false;
- }
- try {
- result = jo.CallStatic<T>(name, args);
- return true;
- } catch (AndroidJavaException e) {
- Debug.LogError("Exception calling static method " + name + ": " + e);
- return false;
- }
- }
- // UI线程中运行
- public static void RunOnUIThread(AndroidJavaObject activityObj , AndroidJavaRunnable r)
- {
- activityObj.Call("runOnUiThread", r);
- }
- static AndroidJavaClass EnvironmentCls;
- // 获取内置SD卡路径
- public static string GetAndroidStoragePath()
- {
- if (Application.platform == RuntimePlatform.Android)
- {
- if(EnvironmentCls == null) EnvironmentCls = new AndroidJavaClass("android.os.Environment");
- return EnvironmentCls.CallStatic<AndroidJavaObject>("getExternalStorageDirectory").Call<string>("getPath");
- }
- else
- return null;
- }
- public static bool CallObjectMethod<T>(ref T result, AndroidJavaObject jo, string name,
- params object[] args) {
- if (jo == null) {
- Debug.LogError("Object is null when calling method " + name);
- return false;
- }
- try {
- result = jo.Call<T>(name, args);
- return true;
- } catch (AndroidJavaException e) {
- Debug.LogError("Exception calling method " + name + ": " + e);
- return false;
- }
- }
- #elif UNITY_STANDALONE_WIN || ANDROID_REMOTE_NRR
- public static AndroidJavaClass GetClass(string className)
- { return null; }
- #endif
- }
- }
- /// @endcond
|