Unity:从Unity调用android函数 [英] Unity: Call android function from Unity

查看:566
本文介绍了Unity:从Unity调用android函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

activity.CallStatic("testMethod");

这是我不带参数地调用Android静态函数的方式,它的工作原理非常完美.但是当我尝试使用参数调用非静态函数时遇到问题.

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.

我正试图这样称呼它:

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

但是Android抛出异常:

But Android throws exception:

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

我做错了什么?静态呼叫和非静态呼叫有什么区别?我正在使用相同的活动.这是我的java方法:

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) { }

更新

最终能够运行非静态方法:

Finally was able to run non static method:

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

如果没有runOnUIThread,那奇怪的事情就不起作用了……可能是我做错了.

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

推荐答案

虽然您可能已解决了该问题,但我认为真正的问题出在其他地方.我已经帮助创建了许多Unity插件,所以我将在这里发表自己的看法,供您阅读以及其他可能遇到相同或相似问题的人阅读.

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.

首先,您使用了UIThread来调用您的非静态公共方法.这是完全可以的,但前提是要使用本机代码执行某些UI操作(例如显示吐司).否则,问题就出在其他地方.

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.

通常,如果创建方法,它将位于扩展UnityPlayerActivity的单独类中.举个例子,假设您将其命名为"MyClass.java"

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"

这可能是您的Java Native类的样子

This is probably how your Java Native class looks like

本地Android代码

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);
    }
}


现在在Unity端,您的C#脚本应如下所示

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

Unity C#代码

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

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


请注意我如何使用实际的Java类而不是UnityPlayerActivity(即" com.companyName.productName.MyClass "而不是" com.unity3d.player.UnityPlayer ") strong>)

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")


尝试上面的代码,它应该在logcat上显示"数据为testString ".

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

在C#代码中,您已经使用currentActivity字段来调用您的方法.

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

相反,您要做的是创建扩展UnityPlayerActivity(在上面的示例中为"MyClass.java")的类的AndroidJavaObjectAndroidJavaClass并调用您的方法.

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.

这篇关于Unity:从Unity调用android函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆