/// <summary> /// 检查函数是否存在 /// </summary> /// <param name="methodName">函数名</param> /// <param name="returnType">返回类型的Type</param> /// <param name="isStatic">是否是静态函数</param> /// <param name="args">所有参数</param> /// <returns></returns> private bool CheckHaveFunInAndroid(string methodName, string returnType, bool isStatic, params object[] args) { string sigStr = GetSigStr(returnType, args); if (string.IsNullOrEmpty(sigStr)) { Debug.Log("the Android Method args is wrong"); return(false); } IntPtr fid; //通过方法名称和参数,获取方法的id,如果方法不存在,fid 为 new IntPtr(0) if (isStatic) { fid = AndroidJNI.GetStaticMethodID(baseJavaObject.GetRawClass(), methodName, sigStr); } else { fid = AndroidJNI.GetMethodID(baseJavaObject.GetRawClass(), methodName, sigStr); } if (fid != new IntPtr(0)) { return(true); } else { AndroidJNI.ExceptionClear(); return(false); } }
public static void CheckException() { IntPtr localref = AndroidJNI.ExceptionOccurred(); if (!(localref != IntPtr.Zero)) { return; } AndroidJNI.ExceptionClear(); IntPtr class1 = AndroidJNI.FindClass("java/lang/Throwable"); IntPtr class2 = AndroidJNI.FindClass("android/util/Log"); try { IntPtr methodId = AndroidJNI.GetMethodID(class1, "toString", "()Ljava/lang/String;"); IntPtr staticmethodId = AndroidJNI.GetStaticMethodID(class2, "getStackTraceString", "(Ljava/lang/Throwable;)Ljava/lang/String;"); string message = AndroidJNI.CallStringMethod(localref, methodId, new jvalue[0]); jvalue[] args = new jvalue[1]; args[0].l = localref; string javaStackTrace = AndroidJNI.CallStaticStringMethod(class2, staticmethodId, args); throw new AndroidJavaException(message, javaStackTrace); } finally { DeleteLocalRef(localref); DeleteLocalRef(class1); DeleteLocalRef(class2); } }
private void AndroidJNISafeCheckException() { IntPtr intPtr = AndroidJNI.ExceptionOccurred(); if (intPtr != IntPtr.Zero) { AndroidJNI.ExceptionClear(); IntPtr intPtr2 = AndroidJNI.FindClass("java/lang/Throwable"); IntPtr intPtr3 = AndroidJNI.FindClass("android/util/Log"); try { IntPtr methodID = AndroidJNI.GetMethodID(intPtr2, "toString", "()Ljava/lang/String;"); IntPtr staticMethodID = AndroidJNI.GetStaticMethodID(intPtr3, "getStackTraceString", "(Ljava/lang/Throwable;)Ljava/lang/String;"); string message = AndroidJNI.CallStringMethod(intPtr, methodID, new jvalue[0]); jvalue[] array = new jvalue[1]; array [0].l = intPtr; string javaStackTrace = AndroidJNI.CallStaticStringMethod(intPtr3, staticMethodID, array); throw new AndroidJavaException(message, javaStackTrace); } finally { AndroidJNISafeDeleteLocalRef(intPtr); AndroidJNISafeDeleteLocalRef(intPtr2); AndroidJNISafeDeleteLocalRef(intPtr3); } } }
/// <summary> /// Throws one of the exceptions (<c>UserProfileNotFoundException</c> or <c>ProviderNotFoundException</c> if needed. /// </summary> public static void checkExceptions() { IntPtr jException = AndroidJNI.ExceptionOccurred(); if (jException != IntPtr.Zero) { AndroidJNI.ExceptionClear(); AndroidJavaClass jniExceptionClass = new AndroidJavaClass("com.soomla.profile.exceptions.UserProfileNotFoundException"); if (AndroidJNI.IsInstanceOf(jException, jniExceptionClass.GetRawClass())) { Debug.Log("SOOMLA/UNITY Caught UserProfileNotFoundException!"); throw new UserProfileNotFoundException(); } jniExceptionClass.Dispose(); jniExceptionClass = new AndroidJavaClass("com.soomla.profile.exceptions.ProviderNotFoundException"); if (AndroidJNI.IsInstanceOf(jException, jniExceptionClass.GetRawClass())) { Debug.Log("SOOMLA/UNITY Caught ProviderNotFoundException!"); throw new ProviderNotFoundException(); } jniExceptionClass.Dispose(); Debug.Log("SOOMLA/UNITY Got an exception but can't identify it!"); } }
/** * Pushes a local JNI frame with 128 capacity. This avoids the reference table * being exceeded, which can happen on some lower-end Android devices in extreme conditions * (e.g. Nexus 7 running Android 6). This is likely due to AndroidJavaObject * not deleting local references immediately. * * If this call is unsuccessful it indicates the device is low on memory so the caller should no-op. * https://docs.unity3d.com/ScriptReference/AndroidJNI.PopLocalFrame.html */ private bool PushLocalFrame() { if (AndroidJNI.PushLocalFrame(128) != 0) { AndroidJNI.ExceptionClear(); // clear pending OutOfMemoryError. return(false); } return(true); }
// 检查用户是否已登录九城游戏中心 public bool isUserLoggedIn() { bool isUserLoggedIn = cls_OpenFeint.CallStatic <bool>("isUserLoggedIn"); if (AndroidJNI.ExceptionOccurred() != IntPtr.Zero) // will return NullPointerException if not logged in { AndroidJNI.ExceptionClear(); return(false); } return(isUserLoggedIn); }
/// <summary> /// 初始化环境,switches用于控制 /// InitContext("12345678", false); /// </summary> /// <param name="appId">appid</param> /// <param name="debug">debug模式</param> /// <returns>状态码</returns> public static int InitContext(string appId, bool debug) { TGPA.TGPAHelper.EnableLog(debug); TGPA.TGPAHelper.Init(); #if UNITY_IOS && !UNITY_EDITOR int initRetValue = gpm_initContext(appId, "unity", debug); GameObject tapmGameObject = new GameObject("TApmGameObject"); GameObject.DontDestroyOnLoad(tapmGameObject); tapmGameObject.AddComponent <FrameProcessor>(); MySetGPMObserver(); return(initRetValue); #elif UNITY_ANDROID && !UNITY_EDITOR AndroidJNI.ExceptionClear(); if (sGPMObj != null) { return(-1); } sGPMObj = new AndroidJavaObject("com.tencent.gcloud.gpm.portal.GPMAgent"); if (sGPMObj == null) { return(-2); } AndroidJavaObject unityContext = null; using (AndroidJavaClass c = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) { unityContext = c.GetStatic <AndroidJavaObject>("currentActivity"); } if (unityContext == null) { return(-3); } sGPMObj.Call <int>("initContext", new object[] { unityContext, appId, "unity", debug }); GameObject tapmGameObject = new GameObject("TApmGameObject"); GameObject.DontDestroyOnLoad(tapmGameObject); tapmGameObject.AddComponent <FrameProcessor>(); #endif return(0); }
public override bool Show(int uniqueId, double x, double y, double width, double height) { AndroidJavaObject adView = AdViewForAdViewId(uniqueId); if (adView == null) { return(false); } AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); AndroidJavaObject activity = unityPlayer.GetStatic <AndroidJavaObject>("currentActivity"); activity.Call("runOnUiThread", new AndroidJavaRunnable(() => { AndroidJavaObject context = activity.Call <AndroidJavaObject>("getApplicationContext"); AndroidJavaObject resources = context.Call <AndroidJavaObject> ("getResources"); AndroidJavaObject displayMetrics = resources.Call <AndroidJavaObject> ("getDisplayMetrics"); float density = displayMetrics.Get <float> ("density"); AndroidJavaObject layoutParams = new AndroidJavaObject("android.widget.LinearLayout$LayoutParams", (int)(width * density), (int)(height * density)); AndroidJavaObject linearLayout = new AndroidJavaObject("android.widget.LinearLayout", activity); AndroidJavaClass R = new AndroidJavaClass("android.R$id"); AndroidJavaObject view = activity.Call <AndroidJavaObject> ("findViewById", R.GetStatic <int> ("content")); AndroidJavaObject parent = adView.Call <AndroidJavaObject>("getParent"); if (parent != null) { IntPtr ptr = AndroidJNI.GetMethodID(parent.GetRawClass(), "removeView", "(Landroid/view/View;)V"); if (ptr != IntPtr.Zero) { parent.Call("removeView", adView); } else { AndroidJNI.ExceptionClear(); } } layoutParams.Call("setMargins", (int)(x * density), (int)(y * density), 0, 0); linearLayout.Call("addView", adView, layoutParams); view.Call("addView", linearLayout); })); return(true); }
static IntPtr GetJavaMethodID(IntPtr classId, string name, string sig) { AndroidJNI.ExceptionClear(); var mid = AndroidJNI.GetMethodID(classId, name, sig); IntPtr ex = AndroidJNI.ExceptionOccurred(); if (ex != (IntPtr)0) { AndroidJNI.ExceptionDescribe(); AndroidJNI.ExceptionClear(); return (IntPtr)0; } else { return mid; } }
public double GetHighPrecisionSkinTempLevel() { double currentTempLevel = -1.0; try { currentTempLevel = AndroidJNI.CallDoubleMethod(s_GameSDKRawObjectID, s_GetHighPrecisionSkinTempLevelID, s_NoArgs); if (AndroidJNI.ExceptionOccurred() != IntPtr.Zero) { AndroidJNI.ExceptionDescribe(); AndroidJNI.ExceptionClear(); } } catch (Exception) { GameSDKLog.Debug("[Exception] GameSDK.getHighPrecisionSkinTempLevel() failed!"); } return currentTempLevel; }
public int GetSkinTempLevel() { int currentTempLevel = -1; try { currentTempLevel = AndroidJNI.CallIntMethod(s_GameSDKRawObjectID, s_GetSkinTempLevelID, s_NoArgs); if (AndroidJNI.ExceptionOccurred() != IntPtr.Zero) { AndroidJNI.ExceptionDescribe(); AndroidJNI.ExceptionClear(); } } catch (Exception) { GameSDKLog.Debug("[Exception] GameSDK.getSkinTempLevel() failed!"); } return currentTempLevel; }
/// <summary> /// 自定义上报接口 /// </summary> /// <param name="eventName">上报事件名称</param> /// <param name="eventList">上报事件内容</param> public static void ReportEvent(string eventName, Dictionary <string, string> eventList) { #if UNITY_IOS && !UNITY_EDITOR string jsonString = ""; foreach (string key in eventList.Keys) { jsonString = jsonString + key + ":" + eventList[key] + ";"; } gpm_postEventSS(eventName, jsonString); #elif UNITY_ANDROID && !UNITY_EDITOR if (sGPMObj == null) { return; } if (eventList == null || eventName == null) { return; } AndroidJNI.ExceptionClear(); StringBuilder eventParamsBuilder = new StringBuilder("{"); foreach (KeyValuePair <string, string> eventParam in eventList) { if (!eventParam.Equals(default(KeyValuePair <string, string>))) { // NOTE: .NET 4.8文档指出, Append null时StringBuilder不会有更改 eventParamsBuilder .Append('\"').Append(eventParam.Key ?? "null").Append('\"') .Append(':') .Append('\"').Append(eventParam.Value ?? "null").Append('\"') .Append(','); } } // NOTE: Length > 1即JSON串不为空 if (1 < eventParamsBuilder.Length) { eventParamsBuilder.Remove(eventParamsBuilder.Length - 1, 1); } eventParamsBuilder.Append('}'); string eventParams = eventParamsBuilder.ToString(); sGPMObj.Call("postEvent", new object[] { eventName, eventParams }); #endif }
public override bool Show(int uniqueId, double x, double y, double width, double height) { AndroidJavaObject adView = adViewForAdViewId(uniqueId); if (adView == null) { return(false); } AndroidJavaClass androidJavaClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); AndroidJavaObject activity = androidJavaClass.GetStatic <AndroidJavaObject>("currentActivity"); activity.Call("runOnUiThread", (AndroidJavaRunnable) delegate { AndroidJavaObject androidJavaObject = activity.Call <AndroidJavaObject>("getApplicationContext", new object[0]); AndroidJavaObject androidJavaObject2 = androidJavaObject.Call <AndroidJavaObject>("getResources", new object[0]); AndroidJavaObject androidJavaObject3 = androidJavaObject2.Call <AndroidJavaObject>("getDisplayMetrics", new object[0]); float num = androidJavaObject3.Get <float>("density"); AndroidJavaObject androidJavaObject4 = new AndroidJavaObject("android.widget.LinearLayout$LayoutParams", (int)(width * (double)num), (int)(height * (double)num)); AndroidJavaObject androidJavaObject5 = new AndroidJavaObject("android.widget.LinearLayout", activity); AndroidJavaClass androidJavaClass2 = new AndroidJavaClass("android.R$id"); AndroidJavaObject androidJavaObject6 = activity.Call <AndroidJavaObject>("findViewById", new object[1] { androidJavaClass2.GetStatic <int>("content") }); AndroidJavaObject androidJavaObject7 = adView.Call <AndroidJavaObject>("getParent", new object[0]); if (androidJavaObject7 != null) { IntPtr methodID = AndroidJNI.GetMethodID(androidJavaObject7.GetRawClass(), "removeView", "(Landroid/view/View;)V"); if (methodID != IntPtr.Zero) { androidJavaObject7.Call("removeView", adView); } else { AndroidJNI.ExceptionClear(); } } androidJavaObject4.Call("setMargins", (int)(x * (double)num), (int)(y * (double)num), 0, 0); androidJavaObject5.Call("addView", adView, androidJavaObject4); androidJavaObject6.Call("addView", androidJavaObject5); }); return(true); }
public double GetGpuFrameTime() { double gpuFrameTime = -1.0; try { gpuFrameTime = AndroidJNI.CallDoubleMethod(s_GameSDKRawObjectID, s_GetGpuFrameTimeID, s_NoArgs); if (AndroidJNI.ExceptionOccurred() != IntPtr.Zero) { AndroidJNI.ExceptionDescribe(); AndroidJNI.ExceptionClear(); } } catch (Exception) { GameSDKLog.Debug("[Exception] GameSDK.getGpuFrameTime() failed!"); } return gpuFrameTime; }
public static void checkExceptions() { IntPtr jException = AndroidJNI.ExceptionOccurred(); if (jException != IntPtr.Zero) { using (AndroidJavaObject exception = new AndroidJavaObject(jException)) { AndroidJNI.ExceptionClear(); AndroidJavaClass jniExceptionClass = new AndroidJavaClass("com.soomla.store.exceptions.InsufficientFundsException"); if (AndroidJNI.IsInstanceOf(exception.GetRawObject(), jniExceptionClass.GetRawClass())) { Debug.Log("SOOMLA/UNITY Cought InsufficientFundsException!"); throw new InsufficientFundsException(); } jniExceptionClass.Dispose(); jniExceptionClass = new AndroidJavaClass("com.soomla.store.exceptions.VirtualItemNotFoundException"); if (AndroidJNI.IsInstanceOf(exception.GetRawObject(), jniExceptionClass.GetRawClass())) { Debug.Log("SOOMLA/UNITY Cought VirtualItemNotFoundException!"); throw new VirtualItemNotFoundException(); } jniExceptionClass.Dispose(); jniExceptionClass = new AndroidJavaClass("com.soomla.store.exceptions.NotEnoughGoodsException"); if (AndroidJNI.IsInstanceOf(exception.GetRawObject(), jniExceptionClass.GetRawClass())) { Debug.Log("SOOMLA/UNITY Cought NotEnoughGoodsException!"); throw new NotEnoughGoodsException(); } jniExceptionClass.Dispose(); } Debug.Log("SOOMLA/UNITY Got an exception but can't identify it!"); } }
public static void FetchOBB() { using (AndroidJavaClass unity_player = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) { AndroidJavaObject current_activity = unity_player.GetStatic <AndroidJavaObject>("currentActivity"); AndroidJavaObject intent = new AndroidJavaObject("android.content.Intent", current_activity, new AndroidJavaClass("com.unity3d.plugin.downloader.UnityDownloaderActivity")); int Intent_FLAG_ACTIVITY_NO_ANIMATION = 0x10000; intent.Call <AndroidJavaObject>("addFlags", Intent_FLAG_ACTIVITY_NO_ANIMATION); intent.Call <AndroidJavaObject>("putExtra", "unityplayer.Activity", current_activity.Call <AndroidJavaObject>("getClass").Call <string>("getName")); current_activity.Call("startActivity", intent); if (AndroidJNI.ExceptionOccurred() != System.IntPtr.Zero) { Debug.LogError("Exception occurred while attempting to start DownloaderActivity - is the AndroidManifest.xml incorrect?"); AndroidJNI.ExceptionDescribe(); AndroidJNI.ExceptionClear(); } } }
public static void FetchOBB() { using (AndroidJavaClass androidJavaClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) { AndroidJavaObject @static = androidJavaClass.GetStatic <AndroidJavaObject>("currentActivity"); AndroidJavaObject androidJavaObject = new AndroidJavaObject("android.content.Intent", @static, new AndroidJavaClass("com.unity3d.plugin.downloader.UnityDownloaderActivity")); int num = 65536; androidJavaObject.Call <AndroidJavaObject>("addFlags", new object[1] { num }); androidJavaObject.Call <AndroidJavaObject>("putExtra", new object[2] { "unityplayer.Activity", @static.Call <AndroidJavaObject>("getClass", new object[0]).Call <string>("getName", new object[0]) }); @static.Call("startActivity", androidJavaObject); if (AndroidJNI.ExceptionOccurred() != IntPtr.Zero) { Debug.LogError("Exception occurred while attempting to start DownloaderActivity - is the AndroidManifest.xml incorrect?"); AndroidJNI.ExceptionDescribe(); AndroidJNI.ExceptionClear(); } } }
public static void init() { if (Application.get_platform() != 11) { return; } try { using (AndroidJavaClass androidJavaClass = new AndroidJavaClass("com.upsight.android.unity.UpsightPlugin")) { if (((AndroidJavaObject)androidJavaClass).CallStatic <bool>("isEnabled", new object[0]) == null) { Debug.LogWarning((object)"Upsight.init() was called but the SDK is currently disabled from the Upsight SDK Management Extension. Upsight will not initialize."); return; } } } catch { Debug.LogError((object)"Error while checking if the Upsight SDK is enabled. Unable to initialize"); return; } if (Upsight.Initialized) { return; } Upsight.Initialized = true; UpsightManager.init(); try { Upsight._pluginBase = new AndroidJavaObject("com.upsight.android.unity.UpsightPlugin", new object[0]); if (Upsight._pluginBase != null) { if (!(AndroidJNI.ExceptionOccurred() != IntPtr.Zero)) { goto label_15; } } Debug.LogError((object)"Upsight initialization failed! JNI Exception thrown:"); AndroidJNI.ExceptionDescribe(); Upsight._pluginBase = (AndroidJavaObject)null; AndroidJNI.ExceptionClear(); return; } catch (Exception ex) { Debug.LogError((object)"Upsight initialization failed!"); Debug.LogException(ex); Upsight._pluginBase = (AndroidJavaObject)null; return; } label_15: try { Upsight._pluginMarketingExtension = new AndroidJavaObject("com.upsight.android.unity.UpsightMarketingManager", new object[0]); if (Upsight._pluginMarketingExtension != null) { Upsight._pluginBase.Call("registerExtension", new object[1] { (object)Upsight._pluginMarketingExtension }); } } catch { Debug.LogWarning((object)"Upsight Marketing Extension not included."); Upsight._pluginMarketingExtension = (AndroidJavaObject)null; } try { Upsight._pluginPushExtension = new AndroidJavaObject("com.upsight.android.unity.UpsightPushManager", new object[0]); if (Upsight._pluginPushExtension == null) { return; } Upsight._pluginBase.Call("registerExtension", new object[1] { (object)Upsight._pluginPushExtension }); } catch { Debug.LogWarning((object)"Upsight Push Extension not included."); Upsight._pluginPushExtension = (AndroidJavaObject)null; } }