using System; using System.Collections.Generic; using UnityEngine; namespace SC.XR.Unity { public abstract class AndroidPluginBase: SCModule { private static AndroidJavaObject mCurrentActivity = 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("currentActivity"); } } return mCurrentActivity; } } 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("getApplicationContext"); } } return mContext; } } protected static List androidListernerList = new List(); 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; } /// /// 实例方法 /// /// /// /// /// /// public static T ObjectFunctionCall(AndroidJavaObject androidJavaObject, string callName, params object[] args) { if(androidJavaObject != null) { try { return androidJavaObject.Call(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(AndroidJavaObject androidJavaObject, string callName, object args) { if(androidJavaObject != null) { try { return androidJavaObject.Call(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(string callName, params object[] args) { if(CurrentActivity != null) { try { return CurrentActivity.Call(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); } } } /// /// 类方法 /// /// /// /// 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(AndroidJavaClass androidJavaClass, string callName, params object[] args) { if(androidJavaClass != null) { try { return androidJavaClass.CallStatic(callName, args); } catch(Exception e) { Debug.LogError(e); } } return default(T); } } }