Call static methods in plugin

Hi,

I have an error when I called this methods :

package com.baringsprod.naandroidunity;

import com.unity3d.player.UnityPlayerActivity;

public class UnityBridge extends UnityPlayerActivity
{
public static final String STATS_NAME = “NaStats”;
public static final String STATS_MAXLEVEL_TOK = “NaStatsMaxLevel”;

public enum NaGameType
{
kEasy,
kMedium,
kExpert
}

private int getMaxLevelForType_( NaGameType type )
{
return getSharedPreferences(STATS_NAME, 0).getInt(STATS_MAXLEVEL_TOK + type, 0);
}

public int getMaxLevelEasy()
{
return getMaxLevelForType_( NaGameType.kEasy );
}

public int getMaxLevelMedium()
{
return getMaxLevelForType_( NaGameType.kMedium );
}

public int getMaxLevelExpert()
{
return getMaxLevelForType_( NaGameType.kExpert );
}

I call them with this C# script :

// Get Android context
AndroidJavaClass unityPlayer = new AndroidJavaClass(“com.unity3d.player.UnityPlayerActivity”);
AndroidJavaObject activity = unityPlayer.GetStatic(“currentActivity”);
AndroidJavaObject app = activity.Call(“getApplicationContext”);
// Install DeployGate, make sure it will not called twice in your app
deployGate = new AndroidJavaClass(“com.baringsprod.naandroidunity.UnityBridge”);
activity.Call(“runOnUiThread”, new AndroidJavaRunnable(() =>
{
recupDiff0 = deployGate.CallStatic(“getMaxLevelEasy”, app);
recupDiff1 = deployGate.CallStatic(“getMaxLevelMedium”, app);
recupDiff2 = deployGate.CallStatic(“getMaxLevelExpert”, app);

NAUtil.UnlockLevelForDifficulty(recupDiff0,0);
NAUtil.UnlockLevelForDifficulty(recupDiff1,1);
NAUtil.UnlockLevelForDifficulty(recupDiff2,2);

Debug.Log("!!! GetPreferenceString success - level returned = " + recupDiff0 + " / " + recupDiff1 + " / " + recupDiff2);
}));

What am I doing wrong ?

Thanks for the help !

What is the error your are receiving ? and on which line is it thrown ?

Basically, if you extend Unity’s default activity, then your activity (e.g: UnityBridge is the default activity now).
You should define it in the AndroidManifest.xml as well, but after you do that, this call will return UnityBridge:

AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"); // This is UnityBridge, returned as an Activity reference

The code you’re later invoking on UnityBridge is using calling it on the class and not on the specific instance.

Try the following code (i can’t test it, but it should be the right direction):

// Get Android context
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayerActivity");
AndroidJavaObject deployGate = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");

deployGate.Call("runOnUiThread", new AndroidJavaRunnable(() =>
{
    recupDiff0 = deployGate.Call<int>("getMaxLevelEasy");
    recupDiff1 = deployGate.Call<int>("getMaxLevelMedium");
    recupDiff2 = deployGate.Call<int>("getMaxLevelExpert");

    NAUtil.UnlockLevelForDifficulty(recupDiff0,0);
    NAUtil.UnlockLevelForDifficulty(recupDiff1,1);
    NAUtil.UnlockLevelForDifficulty(recupDiff2,2);

    Debug.Log("!!!!!! GetPreferenceString success - level returned = " + recupDiff0 + " / " + recupDiff1 + " / " + recupDiff2);
}));

Let me know if that helped :slight_smile:

1 Like

Hi,

First of all :thank you very much for your help.

But I still have an error :

10-02 11:58:31.945: W/Unity(15034): !!! EXCEPTION PlayerPrefsAndroid : UnityEngine.AndroidJavaException: java.lang.NoSuchFieldError: no “Ljava/lang/Object;” field “currentActivity” in class “Lcom/unity3d/player/UnityPlayerActivity;” or its superclasses
10-02 11:58:31.945: W/Unity(15034): at UnityEngine.AndroidJNISafe.CheckException () [0x00000] in :0
10-02 11:58:31.945: W/Unity(15034): at UnityEngine.AndroidJNISafe.GetStaticFieldID (IntPtr clazz, System.String name, System.String sig) [0x00000] in :0
10-02 11:58:31.945: W/Unity(15034): at UnityEngine._AndroidJNIHelper.GetFieldID (IntPtr jclass, System.String fieldName, System.String signature, Boolean isStatic) [0x00000] in :0
10-02 11:58:31.945: W/Unity(15034): at UnityEngine.AndroidJNIHelper.GetFieldID (IntPtr javaClass, System.String fieldName, System.String signature, Boolean isStatic) [0x00000] in :0
10-02 11:58:31.945: W/Unity(15034): at UnityEngine._AndroidJNIHelper.GetFieldID[AndroidJavaObject] (IntPtr jclass, System.String fieldName, Boolean isStatic) [0x00000] in :0
10-02 11:58:31.945: W/Unity(15034): at UnityEngine.AndroidJNIHelper.GetFieldID[AndroidJavaObject] (IntPtr jclass, Syste

My manifest :

<?xmlversion="1.0"encoding="utf-8"?>

<manifestxmlns:android=“http://schemas.android.com/apk/res/android”
package=“com.baringsprod.naandroidunity”
android:versionCode=“1”
android:versionName=“1.0”>

And the code I used :

try{
intrecupDiff0=0;
intrecupDiff1=0;
intrecupDiff2=0;

//GetAndroid context
AndroidJavaClassunityPlayer=newAndroidJavaClass(“com.unity3d.player.UnityPlayerActivity”);
AndroidJavaObjectdeployGate=unityPlayer.GetStatic(“currentActivity”);

deployGate.Call(“runOnUiThread”,newAndroidJavaRunnable(()=>
{
recupDiff0=deployGate.Call(“getMaxLevelEasy”);
recupDiff1=deployGate.Call(“getMaxLevelMedium”);
recupDiff2=deployGate.Call(“getMaxLevelExpert”);

NAUtil.UnlockLevelForDifficulty(recupDiff0,0);
NAUtil.UnlockLevelForDifficulty(recupDiff1,1);
NAUtil.UnlockLevelForDifficulty(recupDiff2,2);

Debug.Log(“!!!PlayerPrefsAndroidsuccess-levelreturned=”+recupDiff0+“/”+recupDiff1+“/”+recupDiff2);
}));
}catch(Exception ee){
Debug.LogWarning(“!!!EXCEPTIONPlayerPrefsAndroid:”+ee.ToString());
}

Sorry and thanks again !

Ok i see 2 things now:

  • The currentActivity static field is on com.unity3d.player.UnityPlayer and not on UnityPlayerActivity. Change the code to init the androidjavaclass with that.
  • Are you declaring your new activity in the manifest? You should do that in case you don’t.

Let me know if that helped :slight_smile:

Hi,

I test it asap, thanks !

About 2/ => I create a new AndroidManifest :

<?xmlversion="1.0"encoding="utf-8"?>

<manifestxmlns:android=“http://schemas.android.com/apk/res/android”
package=“com.baringsprod.naandroidunity”
android:versionCode=“1”
android:versionName=“1.0”>

Thanks !