Mar*_*mán 6 c# java android unity-game-engine xamarin
我在 Unity 中有代码,使用 AndroidJavaClass 和 AndroidJavaObject 调用 Android 的 Java 编程函数,我想将此代码传递给 C# .Net 中的 dll,但我找不到使用 C# .Net 调用 Java 函数的简单方法由于 AndroidJavaObject 和 AndroidJavaClass 类是 Unity dll 独有的,因此以下是我的代码示例:
Java(安卓):
public class DeviceManager {
public interface DeviceListener {
void DoSomething();
}
private DeviceListener deviceListener;
public DeviceManager(DeviceListener deviceListener){
this.deviceListener = deviceListener;
}
public String[] getSomeData(){
return new String[]{};
}
}
Run Code Online (Sandbox Code Playgroud)
C#(统一):
public class DeviceListener : AndroidJavaProxy
{
DeviceManager deviceManager;
public DeviceListener(DeviceManager manager) : base("com.myapp.DeviceManager$DeviceListener")
{
this.deviceManager = manager;
}
public void DoSomething();
}
public class DeviceManager
{
private AndroidJavaObject jo;
public DeviceManager(){
jo = new AndroidJavaObject("com.myapp.DeviceManager", new DeviceListener(this));
}
public string[] GetData(){
AndroidJavaObject returnedData = jo.Call<AndroidJavaObject>("getSomeData");
return AndroidJNIHelper.ConvertFromJNIArray<string[]>(returnedData.GetRawObject());
}
}
Run Code Online (Sandbox Code Playgroud)
在 C# .Net 而不是 Unity 中执行该示例的最佳方法是什么?
编辑:我的主要问题与了解如何从 Unity 的 dll C# 文件内的 .NET Framework 中的 .aar 文件获取对象和调用函数有关
编辑2:我找到了一些有关Xamarin的信息,也许这就是我正在寻找的?:https://github.com/xamarin/monodroid-samples/tree/master/JavaIntegration/AarBinding但即使我可以链接一个.aar文件到 C# 中的 dll 并不能完全回答我的问题,以创建与上述示例相同的代码的最佳方式
编辑3:目前,为了解决这个问题,我在 Visual Studio 项目中使用 UnityEngine .dll,但我想生成一个 .dll 用于其他图形引擎,所以我希望能够找到与使用不同的解决方案Unity 自己的 .dll 可以解决这个问题,因此我提到了 Xamarin,因为它是 .Net Framework 与 Android (Java) 通信的好方法,但我还没有找到任何在 Visual Studio 中创建的移动应用程序之外使用它的示例,因为我的情况我需要从 .aar 文件获取对象并调用函数
看来您正在寻找使用 AAR 插件,我自己还没有这样做过,但是 Unity 有一个关于如何将其集成到您的项目中的文档。
\n然后,一旦该库在 Android studio 中编译并集成到 Unity 中,您就可以查看此示例,了解如何从 C# 代码调用它。
\n我自己还没有这样做过,但是这个例子应该能让你走上正轨。
\n关于脚本后端,我认为它不会对此产生任何影响。
\n编辑\n添加示例摘录
\n\n\nAssets / Plugins / Android\n将本机 android .AAR 文件放入此文件夹中。\n现在让\xe2\x80\x99s 编写一些代码来调用插件。
\n
using System.Collections;\nusing System.Collections.Generic;\nusing UnityEngine;\n\npublic class NativeCodeRunner : MonoBehaviour\n{\n void Start(){\n CallNativePlugin();\n}\n//method that calls our native plugin.\npublic void CallNativePlugin() \n{\n // Retrieve the UnityPlayer class.\n AndroidJavaClass unityPlayerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");\n // Retrieve the UnityPlayerActivity object ( a.k.a. the current context )\n AndroidJavaObject unityActivity = unityPlayerClass.GetStatic<AndroidJavaObject>("currentActivity");\n\n // Retrieve the "Bridge" from our native plugin.\n // ! Notice we define the complete package name. \n AndroidJavaObject alert = new AndroidJavaObject("plugins.vsoft.com.library.Alert");\n\n // Setup the parameters we want to send to our native plugin. \n object[] parameters = new object[2];\n parameters[0] = unityActivity;\n parameters[1] = "Hello World!";\n\n // Call PrintString in bridge, with our parameters.\n alert.Call("PrintString", parameters);\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
1341 次 |
| 最近记录: |