123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- namespace SC.XR.Unity
- {
- public abstract class AndroidPluginBase : SCModule
- {
- private static AndroidJavaObject mCurrentActivity = null;
- private static AndroidJavaObject mCurrentRtcActivity = null;
- private static AndroidJavaClass mUnityPlayerClass = null;
- private static AndroidJavaObject mContext = null;
- protected static AndroidJavaObject CurrentActivity
- {
- get
- {
- if (Application.platform == RuntimePlatform.Android)
- {
- if (mCurrentActivity == null && UnityPlayerClass != null)
- {
- mCurrentActivity = UnityPlayerClass.GetStatic<AndroidJavaObject>("currentActivity");
- }
- }
- return mCurrentActivity;
- }
- }
- protected static AndroidJavaObject CurrentRtcActivity
- {
- get
- {
- if (Application.platform == RuntimePlatform.Android)
- {
- if (mCurrentRtcActivity == null && UnityPlayerClass != null)
- {
- var javaClass = new AndroidJavaClass("org.mediasoup.droid.demo.rtc.unitytojava");
- var javaObject = javaClass.CallStatic<AndroidJavaObject>("instance", UnityPlayerClass.GetStatic<AndroidJavaObject>("currentActivity"));
- mCurrentRtcActivity = javaObject;
- }
- }
- return mCurrentRtcActivity;
- }
- }
- protected static AndroidJavaClass UnityPlayerClass
- {
- get
- {
- if (Application.platform == RuntimePlatform.Android)
- {
- if (mUnityPlayerClass == null)
- {
- mUnityPlayerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
- }
- }
- return mUnityPlayerClass;
- }
- }
- protected static AndroidJavaObject Context
- {
- get
- {
- if (Application.platform == RuntimePlatform.Android)
- {
- if (mContext == null)
- {
- mContext = CurrentActivityFunctionCall<AndroidJavaObject>("getApplicationContext");
- }
- }
- return mContext;
- }
- }
- protected static List<AndroidJavaProxy> androidListernerList = new List<AndroidJavaProxy>();
- public static void AddListener(string setCallBackFunctionName, AndroidJavaProxy callBack)
- {
- if (Application.platform == RuntimePlatform.Android)
- {
- CurrentActivity.Call(setCallBackFunctionName, callBack);
- androidListernerList.Add(callBack);
- }
- }
- public static void ObjectAddListener(AndroidJavaObject androidObject, string setCallBackFunctionName, AndroidJavaProxy callBack)
- {
- if (Application.platform == RuntimePlatform.Android)
- {
- if (androidObject != null)
- {
- androidObject.Call(setCallBackFunctionName, callBack);
- androidListernerList.Add(callBack);
- }
- }
- }
- public static AndroidJavaClass GetAndroidJavaClass(string classPatch)
- {
- if (Application.platform == RuntimePlatform.Android)
- {
- try
- {
- return new AndroidJavaClass(classPatch);
- }
- catch (Exception e)
- {
- Debug.LogError(e);
- }
- }
- return null;
- }
- /// <summary>
- /// 实例方法
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="androidJavaObject"></param>
- /// <param name="callName"></param>
- /// <param name="args"></param>
- /// <returns></returns>
- public static T ObjectFunctionCall<T>(AndroidJavaObject androidJavaObject, string callName, params object[] args)
- {
- if (androidJavaObject != null)
- {
- try
- {
- return androidJavaObject.Call<T>(callName, args);
- }
- catch (Exception e)
- {
- Debug.LogError(e);
- }
- }
- return default(T);
- }
- public static void ObjectFunctionCall(AndroidJavaObject androidJavaObject, string callName, params object[] args)
- {
- if (androidJavaObject != null)
- {
- try
- {
- androidJavaObject.Call(callName, args);
- }
- catch (Exception e)
- {
- Debug.LogError(e);
- }
- }
- }
- public static T ObjectFunctionCall2<T>(AndroidJavaObject androidJavaObject, string callName, object args)
- {
- if (androidJavaObject != null)
- {
- try
- {
- return androidJavaObject.Call<T>(callName, args);
- }
- catch (Exception e)
- {
- Debug.LogError(e);
- }
- }
- return default(T);
- }
- public static void ObjectFunctionCall2(AndroidJavaObject androidJavaObject, string callName, object args)
- {
- if (androidJavaObject != null)
- {
- try
- {
- androidJavaObject.Call(callName, args);
- }
- catch (Exception e)
- {
- Debug.LogError(e);
- }
- }
- }
- public static T CurrentActivityFunctionCall<T>(string callName, params object[] args)
- {
- if (CurrentActivity != null)
- {
- try
- {
- return CurrentActivity.Call<T>(callName, args);
- }
- catch (Exception e)
- {
- Debug.LogError(e);
- }
- }
- return default(T);
- }
- public static void CurrentActivityFunctionCall(string callName, params object[] args)
- {
- if (CurrentActivity != null)
- {
- try
- {
- CurrentActivity.Call(callName, args);
- }
- catch (Exception e)
- {
- Debug.LogError(e);
- }
- }
- }
- /// <summary>
- /// 类方法
- /// </summary>
- /// <param name="androidJavaClass"></param>
- /// <param name="callName"></param>
- /// <param name="args"></param>
- public static void ClassFunctionCallStatic(AndroidJavaClass androidJavaClass, string callName, params object[] args)
- {
- if (androidJavaClass != null)
- {
- try
- {
- androidJavaClass.CallStatic(callName, args);
- }
- catch (Exception e)
- {
- Debug.LogError(e);
- }
- }
- }
- public static T ClassFunctionCallStatic<T>(AndroidJavaClass androidJavaClass, string callName, params object[] args)
- {
- if (androidJavaClass != null)
- {
- try
- {
- return androidJavaClass.CallStatic<T>(callName, args);
- }
- catch (Exception e)
- {
- Debug.LogError(e);
- }
- }
- return default(T);
- }
- }
- }
|