Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
649 views
in Technique[技术] by (71.8m points)

unity3d - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...