123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using UnityEngine;
- using UnityEngine.Android;
- using XRTool.Util;
- namespace XRTool.Util
- {
- /// <summary>
- /// 安卓与Unity接口类交互
- /// </summary>
- public class AndroidLib : UnitySingleton<AndroidLib>
- {
- private static AndroidJavaObject currentActivity;
- /// <summary>
- /// 安卓的Activity
- /// </summary>
- public static AndroidJavaObject MainActivity
- {
- get
- {
- #if UNITY_ANDROID
- if (currentActivity == null)
- {
- AndroidJavaClass up = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
- currentActivity = up.GetStatic<AndroidJavaObject>("currentActivity");
- }
- #endif
- return currentActivity;
- }
- }
- protected override void Awake()
- {
- name = typeof(AndroidLib).Name;
- base.Awake();
- DontDestroyOnLoad(gameObject);
- }
- /// <summary>
- /// 安卓回调接口,安卓调用Unity
- /// 统一接口?
- ///
- /// </summary>
- public void AndroidCallBack(string funName)
- {
- }
- /// <summary>
- /// 调用安卓代码
- /// </summary>
- public static T CallAndLibMethod<T>(AndroidJavaObject andObj, bool isStatic, string meshod, params object[] args)
- {
- if (andObj != null)
- {
- if (isStatic)
- {
- return andObj.CallStatic<T>(meshod, args);
- }
- return andObj.Call<T>(meshod, args);
- }
- return default(T);
- }
- /// <summary>
- /// 调用安卓代码
- /// </summary>
- public static T CallAndLibMethod<T>(AndroidJavaObject andObj, bool isStatic, string meshod)
- {
- if (andObj != null)
- {
- if (isStatic)
- {
- return andObj.CallStatic<T>(meshod);
- }
- return andObj.Call<T>(meshod);
- }
- return default(T);
- }
- /// <summary>
- /// 调用安卓代码
- /// </summary>
- public static void CallAndLibMethod(AndroidJavaObject andObj, bool isStatic, string meshod, params object[] args)
- {
- if (andObj != null)
- {
- if (isStatic)
- {
- andObj.CallStatic(meshod, args);
- }
- else
- {
- andObj.Call(meshod, args);
- }
- }
- }
- public static bool CheckCamera()
- {
- if (!Permission.HasUserAuthorizedPermission(Permission.Camera))
- {
- Permission.RequestUserPermission(Permission.Camera);
- return false;
- }
- return true;
- }
- /// <summary>
- /// 调用安卓代码
- /// </summary>
- public static void CallAndLibMethod(AndroidJavaObject andObj, bool isStatic, string meshod)
- {
- if (andObj != null)
- {
- if (isStatic)
- {
- andObj.CallStatic(meshod);
- }
- else
- {
- andObj.Call(meshod);
- }
- }
- }
- /// <summary>
- /// 调用安卓代码
- /// </summary>
- public static T CallAndLibMethod<T>(bool isStatic, string meshod, params object[] args)
- {
- return CallAndLibMethod<T>(MainActivity,isStatic, meshod, args);
- }
- /// <summary>
- /// 调用安卓代码
- /// </summary>
- public static T CallAndLibMethod<T>( bool isStatic, string meshod)
- {
- return CallAndLibMethod<T>(MainActivity, isStatic, meshod);
- }
- /// <summary>
- /// 调用安卓代码
- /// </summary>
- public static void CallAndLibMethod(bool isStatic, string meshod, params object[] args)
- {
- CallAndLibMethod(MainActivity, isStatic, meshod, args);
- }
-
- /// <summary>
- /// 调用安卓代码
- /// </summary>
- public static void CallAndLibMethod(bool isStatic, string meshod)
- {
- CallAndLibMethod(MainActivity, isStatic, meshod);
- }
- }
- }
|