0

I try make a plugin in android studio to use in Unity.

So I make this method in android studio.

public class MainActivity extends UnityPlayerActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    public static String SendMessage(){
        return "Message";
    }



}

and in Unity,

public class PluginWrapper : MonoBehaviour {

    // Use this for initialization
    void Start () {
        TextMesh textMesh = GetComponent<TextMesh> ();
        var plugin = new AndroidJavaClass ("com.thewell_dev.beaconf.MainActivity");
        textMesh.text = plugin.CallStatic<string> ("ReturnMessage");

    }

}

to use SendMessage() method in unity, using AndroidJavaClass and CallStatic.

It is success.

I can check message in device by unity.

But, one error occurs.

If I change method like this,

public class MainActivity extends UnityPlayerActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    public String SendMessage(){
        return "Message";
    }



}

change SendMessage method type.(static String to String)

and in Unity,

public class PluginWrapper : MonoBehaviour {

    // Use this for initialization
    void Start () {
        TextMesh textMesh = GetComponent<TextMesh> ();
        var plugin = new AndroidJavaClass ("com.thewell_dev.beaconf.MainActivity");
        textMesh.text = plugin.Call<string> ("ReturnMessage");

    }


} 

change plugin.CallStaic to plugin.Call

In case, I just delete Static, but it returns no result.

https://docs.unity3d.com/kr/current/ScriptReference/AndroidJavaClass.html

I just change the type. But no returns.

I think I mistake to Call method by AndroidJavaClass,

but I can't find it.

If you know about it, Please help me.

Polaris Nation
  • 1,085
  • 2
  • 18
  • 49
  • https://xinyustudio.wordpress.com/2015/12/31/step-by-step-guide-for-developing-android-plugin-for-unity3d-i/ – David Oct 12 '17 at 02:35
  • https://stackoverflow.com/questions/27550280/unity-call-android-function-from-unity This is i think somewhat similer to your problem. you can also add a debug statement to check in logcat if the function is getting called – killer_mech Oct 12 '17 at 06:34
  • @David Thanks I tried it. – Polaris Nation Oct 13 '17 at 00:24
  • @killer_mech Yes I think this is my problem but I can't find it. – Polaris Nation Oct 13 '17 at 00:25
  • @PolarisNation did you checked with the answer below?I also had similer problem long time ago when i was working on android plugin for unity and had to do similer thing Arshia001 mentioned. One suggestion is try to put debug statements in code and see where it is getting stopped. Try adding static method in code and debug statement in that also. You may also read more about how to create plugin/get a plugin package and try to call in that plugin.I saw some tutorials provided the plugin maybe search for those in google. I think this is the max i can help here with current info :( – killer_mech Oct 13 '17 at 06:05

1 Answers1

1

You can use "types" to invoke static methods, as static things are bound to their defining types. You can't, however, use instance methods (non-static methods, that is) without an instance of the type. That's why your second attempt fails. To invoke instance methods, you must get an instance of the type, either by making one yourself by invoking the java constructor like this:

var object = new AndroidJavaObject ("com.some.class", constructor_param1, constructor_param2, constructor_param3, ...);
result = object.Call<string> ("SomeMethod");

Or, to get as instance of something like an activity which is constructed outside the scope of your C# script, you use static methods to retrieve the instance you're looking for:

var type = new AndroidJavaClass ("com.some.class");
var object = type.CallStatic<AndroidJavaObject>( "SomeStaticGetterMethod" );
result = object.Call<string> ("SomeMethod");
Arshia001
  • 1,854
  • 14
  • 19