Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity: Call android function from Unity

AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");

AndroidJavaObject activity = unityPlayer.GetStatic("currentActivity");

activity.CallStatic("testMethod");

This is how I call Android static function without argument, and its works perfect. But I have issue when I'm trying to call non-static function with arguments.

I'm trying to call it like that:

activity.Call("testMethod", "testString");

But Android throws exception:

AndroidJavaException: java.lang.NoSuchMethodError: no non-static method "L**myActivity**(Ljava/lang/String;)V"

What I do wrong ? What is the difference between static and non-static call ? I'm using the same activity. Here is my java method:

public void testMethod(String data) { }

UPDATE

Finally was able to run non static method:

activity.Call("runOnUiThread", new AndroidJavaRunnable(() =>
                                                                   {
                    activity.Call("testMethod", "testString");
            }));

That odd thats not working without runOnUIThread...Probably I did something wrong.

like image 561
Jim Avatar asked Dec 18 '14 15:12

Jim


People also ask

Can I use Android studio with Unity?

This article demonstrates how to integrate Android studio with Unity 3D. There are two Android build process output types in Unity, the output package (APK), and the exported project. An APK is automatically deployed to your device if you select Build and Run.


1 Answers

While you might have fixed the issue, I believe the real issue lies elsewhere. I've helped create quite a few plugins for Unity, so I'm just going to put in my thoughts here for you to read and for anyone else who might face the same or similar issues.

Firstly, you've used a UIThread to call your non-static, public method. This is perfectly fine, but only if some UI operation is performed in the native code, such as showing a toast. Otherwise, the issue lies elsewhere.

Usually, if you create a method, it's going to be in a separate class that extends the UnityPlayerActivity. So for an example, let's say you've called it "MyClass.java"

This is probably how your Java Native class looks like

Native Android Code

package com.companyName.productName;

import com.unity3d.player.UnityPlayerActivity;
import com.unity3d.player.UnityPlayer;

public class MyClass extends UnityPlayerActivity {    

    public void testMethod(String data) { 
        Log.i("TAG", "The data was "+data);
    }
}


Now on the Unity side, your C# script should look like this

Unity C# code

AndroidJavaClass myClass = new AndroidJavaClass("com.companyName.productName.MyClass");

myClass.Call("testMethod", new object[] { "testString" } );


Note how I've used the actual Java Class rather than the UnityPlayerActivity (i.e. "com.companyName.productName.MyClass" rather than "com.unity3d.player.UnityPlayer")


Try the code above, it should print "The data was testString" to the logcat.

In your C# code, you've used the currentActivity field to call your method.

Rather, what you want to do is to create an AndroidJavaObject or AndroidJavaClass of your class that extends UnityPlayerActivity ("MyClass.java" in the above example) and call your methods.

like image 102
Venkat at Axiom Studios Avatar answered Oct 29 '22 05:10

Venkat at Axiom Studios