简体   繁体   中英

Unity call android static function of java class

Hi i'd like to call this piece of android java code in unity with c# here the java code:

SmsDialog.getInstance().init(this);
//this is context of android activity

And right now i'm doing it like this in my c# code:

void ShowPaymentDialog()
{

    AndroidJavaClass smsDialog = new AndroidJavaClass("com.mobagym.testsdkmobagym.SmsDialog");
    smsDialog.CallStatic<AndroidJavaObject>("getInstance").Call("init",getContext());
}
AndroidJavaObject getContext()
{
    AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
    return jo;
}

There are no crashes or anything, Just that game stops and doesnt execute lines after ShowPaymentDialog.

void Start () 
{
    ShowPaymentDialog();
    GoogleAnalyticsV4.getInstance().LogScreen(MyMenuManager.SPLASH_SCREEN);
    StartCoroutine(Next());
}
IEnumerator Next()
{
    yield return new WaitForSeconds(duration);
    SceneManager.LoadScene(MyMenuManager.MAIN_MENU);
}

so i'd like to know if i'm doing sth wrong with c# syntax. And if there are any ways to log this.

You should try to log the error as you mention. Try setting up a try catch block.

Try {
    //do some logic
} catch (Exception e){
    //Log exception
}

Check this link: https://docs.unity3d.com/ScriptReference/Debug.LogException.html

Maybe when you have stacktrace with exact error we can provide more assistance.

You are probably having a native java exception and missing it. You should connect a device to it and monitor the device log with adb logcat looking for your method call. This should give you enough information to continue debugging.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM