简体   繁体   English

适用于Android的Unity Native C ++插件

[英]Unity Native C++ Plug-in for Android

I am making a native plug-in for Android written in C++ and packaged in a shared library for the Unity Game Engine. 我正在制作一个用C ++编写的Android原生插件,并打包在Unity Game Engine的共享库中。 However, I'm getting an EntryPointNotFoundException with even the default native-lib.cpp generated by Android Studio. 但是,我什至得到了EntryPointNotFoundException甚至Android Studio生成的默认native-lib.cpp。

#include <jni.h>
#include <string>

extern "C" JNIEXPORT jstring JNICALL Java_com_example_willgauthier_stublibrary_MainActivity_stringFromJNI(
    JNIEnv *env,
    jobject /* this */) {
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}

In Android Studio I built the project. 在Android Studio中,我构建了项目。 Then in Unity I made the folder structure Assets/Plugins/Android/libs and copied over the shared libnative-lib.so libraries armeabi-v7a and x86 . 然后在Unity中,我制作了文件夹结构Assets / Plugins / Android / libs并复制到共享的libnative-lib.soarmeabi-v7ax86上

I attached this script to an empty game object: 我将此脚本附加到一个空的游戏对象上:

using System;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.UI;

public class LibLoadTest : MonoBehaviour
{
    public Text debugText;

    [DllImport("native-lib")]
    private static extern string stringFromJNI();

    private void Start()
    {
        try
        {
             debugText.text = stringFromJNI();
        }
        catch(Exception exception)
        {
            debugText.text = exception.ToString();
        }
    }
}

My debugText shows "System.EntryPointNotFoundException: stringFromJNI at (wrapper managed-to-native) LibLoadTest::stringFromJNI() at LibLoadTest.Start() [0x00000] in :0" instead of "Hello from C++". 我的debugText显示“ System.EntryPointNotFoundException :(从包装器托管到本地)LibLoadTest :: stringFromJNI()在0处LibLoadTest.Start()[0x00000]处的stringFromJNI”,而不是“从C ++发出的Hello”。

Does anyone know how to successfully use a native plug-in shared library for Android in Unity and where I'm going wrong in my process? 有谁知道如何在Unity中成功使用Android的本机插件共享库以及我的处理过程出了什么问题? Thanks. 谢谢。

I figured out the issue thanks to Bonfire-Boy on the Unity Answers forums: the name of the function I should have been trying to import was Java_com_example_willgauthier_stublibrary_MainActivity_stringFromJNI , not just stringFromJNI . 我在Unity Answers论坛上通过Bonfire-Boy找出了问题所在:我应该尝试导入的函数的名称是Java_com_example_willgauthier_stublibrary_MainActivity_stringFromJNI ,而不仅仅是stringFromJNI I incorrectly assumed that because the long name was auto-generated and included the package (not sure that's the correct term) structure, that it could be ignored. 我错误地认为,由于长名称是自动生成的,并且包含了包(不确定这是正确的术语)结构,因此可以忽略它。 Deleting everything before stringFromJNI in the library function definition worked, though. 不过,删除库函数定义中stringFromJNI之前的所有内容都可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM