如何将JNI C#类传递给Java或处理这种情况? [英] How to pass a JNI C# class into Java or handle this situation?

查看:143
本文介绍了如何将JNI C#类传递给Java或处理这种情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从C#调用Java方法,而从Java中这样调用它:

I'm trying to call a Java method from C#, it's called like this from java:

EgamePay.pay(thisActivity, payAlias, new EgamePayListener() {
            @Override
            public void paySuccess(String alias) {
            }

            @Override
            public void payFailed(String alias, int errorInt) {
            }

            @Override
            public void payCancel(String alias) {
            }
        });

前两个参数都可以,但是如何在C#中传递EgamePayListner?简单地创建具有相同功能的C#类是行不通的...

The first two parameters are ok, but how do I pass the EgamePayListner through in C#? Simply creating a C# class with the same functions won't work...

这是我目前正在做的事情:

Here's what I'm doing currently:

using (AndroidJavaClass jc = new AndroidJavaClass("cn.egame.terminal.smspay.EgamePay"))
        {
            System.IntPtr cls_Activity = AndroidJNI.FindClass("com/unity3d/player/UnityPlayer");
            System.IntPtr fid_Activity = AndroidJNI.GetStaticFieldID(cls_Activity, "currentActivity", "Landroid/app/Activity;");
            object[] p = { fid_Activity, "payAlias", new EgamePayListener() };
            jc.CallStatic("pay", p);
        }

..

 class EgamePayListener
    {
        public void paySucess(string alias)
        {
        }

        public void payFailed(string alians, int errorInt)
        {
        }

        public void payCancel(string alias)
        {
        }
    }

显然那是错误的,我该如何处理这种情况,以便在这些功能被触发时可以在C#领域得到通知?

Obviously that's wrong, how can I handle this situation so that I can get notified back in C# land when those functions are fired?

推荐答案

如果您在Unity3D的上下文中,从Java本机代码与Unity3D C#脚本进行交互的更好方法是调用UnitySendMessage方法.您可以在Java代码中调用此方法,然后一条消息将发送到C#,这样您就可以在C#中执行指定的方法.

if you are in a context of Unity3D, a better way to interact from Java native code to Unity3D C# script would be the calling the UnitySendMessage method. You can call this method in you Java code, and a message will be sent to C#, so you can get a specified method executed in C#.

您可以在Unity场景中添加一个gameObject并创建一个MonoBehavior脚本,其中包含这三种方法(paySucess(字符串消息),payFailed(字符串消息)和payCancel(消息)).然后将新创建的脚本附加到gameObject(让我们假定此gameObject的名称为"PayListener"),并确保在执行Java代码时场景中存在该gameObject(您可以调用

You could add a gameObject in your Unity scene and create a MonoBehavior script which contains these three methods (paySucess(string message), payFailed(string message) and payCancel(message)). Then attach the new created script to the gameObject (let us assume the name of this gameObject to be "PayListener") and make sure the gameObject existing in your scene when the Java code be executed (you can call DontDestroyOnLoad on the gameObject in Awake, for example).

然后,在您的Java代码中,编写如下代码:

Then, in your Java code, just write like these:

EgamePay.pay(thisActivity, payAlias, new EgamePayListener() {
    @Override
    public void paySuccess(String alias) {
        com.unity3d.player.UnityPlayer.UnitySendMessage("PayListener","paySuccess", alias);
    }

    @Override
    public void payFailed(String alias, int errorInt) {
        com.unity3d.player.UnityPlayer.UnitySendMessage("PayListener","payFailed", alias + "#" + errorInt);
    }

    @Override
    public void payCancel(String alias) {
        com.unity3d.player.UnityPlayer.UnitySendMessage("PayListener","payCancel", alias);
    }
});

如果没有com.unity3d.player.UnityPlayer类,它将阻止Java文件成功构建.您可以找到/Applications/Unity/Unity.app/Contents/PlaybackEngines/AndroidPlayer/bin/classes.jar文件,并将其添加为Java项目的依赖库.然后,您可以毫无错误地构建,生成和使用新的jar文件.

It would prevent the java file to build successfully without the com.unity3d.player.UnityPlayer class. You could find the /Applications/Unity/Unity.app/Contents/PlaybackEngines/AndroidPlayer/bin/classes.jar file and add it as a dependency library for your java project. Then you can build, generate and use the new jar file without error.

由于UnitySendMessage方法只能使用1个参数,因此您可能必须通过策略将payFailed结果别名和errorInt连接起来,并在Unity C#端将其解析回去.

Because of the UnitySendMessage method can only take 1 parameter, so you might have to connect the payFailed result alias and errorInt by a strategy, and parse it back in Unity C# side.

通过使用新生成的jar文件并将其作为AndroidJavaClassAndroidJavaObject加载,应在调用Java中的相应方法时调用PayListener脚本中的相应方法.

By using the new built jar file and load it as a AndroidJavaClass or AndroidJavaObject, the corresponding method in the PayListener script should be called when the one in Java is get called.

有关带有UnitySendMessage的Android插件的文档,您可以访问有关如何实现Android JNI插件的官方指南.此处,在示例3中.

For documentation of the Android plugin with UnitySendMessage, you can visit the official guide of how to implement a Android JNI plugin here, in the example 3.

这篇关于如何将JNI C#类传递给Java或处理这种情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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