为Unity构建C ++插件 [英] Build C++ plugin for Unity

查看:74
本文介绍了为Unity构建C ++插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试创建我的第一个插件. cpp代码为:

Trying to create my first plugin. The cpp code is:

标题:

#pragma once
#ifndef __MY_DLL_H
#define __MY_DLL_H

extern "C" int add_1(int number);

#endif

来源:

//FirstDLL.cpp
#include "FirstDLL.h"

extern "C" int add_1(int number) {
    return number + 1;
}

然后我编译该DLL并将其放在Assets/Plugins文件夹中,该dll文件为FirstDLL.dll.从统一的角度来看,我有一个用于组件的简单C#脚本:

Then I compile and put the DLL in Assets/Plugins folder, the dll file is FirstDLL.dll. From the unity side I have a simple C# script for a component:

using UnityEngine;

public class MyBehaviour : MonoBehaviour {

    // Use this for initialization
    [Header("Nuts!")]
    public int my_curr_val;
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        print(add_1(my_curr_val));
    }

    [DllImport("FirstDLL")]
    public static extern int add_1(int number);
}

但是当我尝试运行脚本时,出现以下错误:

But when I try to run the script I get the following error:

插件:无法加载"Assets/Plugins/FirstDLL.dll",并显示错误 '此操作仅在应用程序容器的上下文中有效. '. 插件:无法加载资产/插件/FirstDLL.dll",错误为此操作仅在应用程序容器的上下文中有效. '. DllNotFoundException:FirstDLL MyBehaviour.Update()(位于Assets/MyBehaviour.cs:17)

Plugins: Failed to load 'Assets/Plugins/FirstDLL.dll' with error 'This operation is only valid in the context of an app container. '. Plugins: Failed to load 'Assets/Plugins/FirstDLL.dll' with error 'This operation is only valid in the context of an app container. '. DllNotFoundException: FirstDLL MyBehaviour.Update () (at Assets/MyBehaviour.cs:17)

文档似乎很差,有什么建议吗? 有此答案,但不确定我在做什么错.我试图创建一些解决方案(Windows通用平台,Windows 8.1等)仍然无法正常工作.

The documentation seems to be quite poor, any suggestions? There's this answer, but not sure what I'm doing wrong. I've tried to create several solution (windows universal platform, windows 8.1, etc) still doesn't work.

推荐答案

您正试图在非UWP环境中加载UWP插件,这是因为生成dll的方式.

You are trying to load UWP plugin in a non UWP environment and this is because of the way you build your dll.

这篇文章介绍了如何在Unity中创建,编译和构建C ++插件.

This post describes how to create, compile and build C++ plugin in Unity.

用于此目的的软件版本(也应适用于其他旧版本.为防止将来有更新和不同的UI,会提及此信息):

  • Microsoft Visual Studio 2015

  • Microsoft Visual Studio 2015

Unity 2017.2.0f3

Unity 2017.2.0f3

1 .转到文件 ---> 新建 ---> 项目...

1.Go to File ---> New ---> Project...

2 .转到已安装-> 模板 ---> Visual C ++ ,然后进入 Win32控制台应用程序.输入项目名称,然后单击确定".

2. Go to Installed --> Templates ---> Visual C++ then Win32 Console Application. Type the name of the project then click Ok.

3 .单击下一步,而不是完成:

4 .选择 DLL ,然后取消选择 Precompiled标头,然后单击完成:

4.Select DLL and unselect Precompiled header then click finish:

5 .您现在可以创建源文件(.cpp)和头文件(.h).

5.You can now create your source(.cpp) and header(.h) files.

A .创建源文件:

此文件应放置在 Source Files 文件夹中.右键单击源文件--->添加--->新建项目...

This should be placed in the Source Files folder. Right click on Source Files ---> Add---> New Item...

B .选择C ++ File(.cpp),键入文件名"FirstDLL.cpp",然后单击添加".

B.Select C++ File(.cpp), type the name of the file "FirstDLL.cpp" then click Add.

示例C ++测试源:

#include "FirstDLL.h"

int add(int num1, int num2)
{
    return num1 + num2;
}

int multiply(int num1, int num2)
{
    return num1 * num2;
}

int substract(int num1, int num2)
{
    return num1 - num2;
}

int divide(int num1, int num2)
{
    return num1 / num2;
}


A .创建头文件:


A.Create a header file:

此文件应放置在 Header Files 文件夹中.右键单击头文件--->添加--->新项...

This should be placed in the Header Files folder. Right click on Header Files ---> Add---> New Item...

B .选择 Header File(.h),键入文件"FirstDLL.h"的名称,然后单击添加".

B.Select Header File(.h), type the name of the file "FirstDLL.h" then click Add.

对应的标题示例:

#ifndef FIRSTDLL_NATIVE_LIB_H
#define FIRSTDLL_NATIVE_LIB_H

#define DLLExport __declspec(dllexport)

extern "C"
{
    DLLExport int add(int num1, int num2);
    DLLExport int multiply(int num1, int num2);
    DLLExport int substract(int num1, int num2);
    DLLExport int divide(int num1, int num2);
}
#endif

就是这样.现在,您可以在此处编写C ++插件代码.

That's it. You can now write your C++ plugins code there.

6 .请确保将构建版本设置为release,并将平台设置为64位

6.Make sure to set the build to release and the platform to 64 bit

如果使用32位,则将平台设置为x86.

If using 32-bit, set the platform to x86.

7 .建筑插件:

转到构建 ---> 构建解决方案

8 .导入Unity:

PC,Mac& Linux独立版:

将64位dll文件放入Assets/Plugins文件夹.

Put the 64-bit dll file into Assets/Plugins folder.

如果您只想支持32位,则将插件放在Assets/Plugins/x86中.

If you just want to support 32-bit then put the plugin in Assets/Plugins/x86.

如果要支持通用(32位和64位平台),则应以这种方式构建dll并将其放在Assets/Plugins/x86_64文件夹中.

If you want to support universal (both 32-bit and 64-bit platform) then build the dll as such and put it in the Assets/Plugins/x86_64 folder.

Android :

可以从 Android Studio 构建.

Can be build from Android Studio.

要从Visual Studio生成:

To build from Visual Studio:

A .转到文件 ---> 新建 ---> 项目...

A.Go to File ---> New ---> Project...

B .转到已安装-> 模板 ---> Visual C ++ ,然后是跨平台.点击安装Android对C ++的支持(更新x).然后按照说明进行安装.

B. Go to Installed --> Templates ---> Visual C++ then Cross Platform. Click on Install Android support for C++ (Update x). then follow direction to install it.

C .转到已安装-> 模板 ---> Visual C ++ ---> 跨平台.然后是 Android .选择动态碎片库(Android),然后输入项目名称,然后单击确定".现在,您可以跳回到步骤#5 继续使用C ++进行编码.

C. Go to Installed --> Templates ---> Visual C++ ---> Cross Platform. then Android. Select Dynamic Shard Library (Android) then type the name of the project and click Ok. Now, you can jump back to step #5 to continue coding in C++.

将Android插件文件(不是dll)放入Assets/Plugins/Android文件夹. 受支持的C ++插件扩展.so.

Put the Android plugin file (not dll) into the Assets/Plugins/Android folder. The supported C++ plugin extension .so.

注意:如果Android插件的名称为libFirstDLL-lib.so,请从C#引用时删除lib前缀和.so,在这种情况下,其名称为#9中的情况不同.

Note: If the name of the Android plugin is libFirstDLL-lib.so, remove the lib prefix and the .so when referencing it from C#.In this case, it would be [DllImport("FirstDLL-lib")]unlike what it would have been in #9.

如果同时具有armeabi-v7ax86 Android .so插件,则分别将它们放在Assets\Plugins\Android\libs\armeabi-v7aAssets\Plugins\Android\libs\x86文件夹中.

If you have both armeabi-v7a and x86 Android .so plugins then put them in Assets\Plugins\Android\libs\armeabi-v7a and Assets\Plugins\Android\libs\x86 folders respectively.

iOS

可以从 Xcode 构建,或将源文件包含到Unity中.您也可以使用visual Studio创建它.只需按照上述Android步骤操作,但这次使用安装iOS对C ++(更新x)的支持,而不是安装Android对C ++(更新x)的支持.请注意,您需要Mac计算机才能为iOS构建或使用虚拟机.完成此操作后,请按照 Microsoft指令完成安装,以便Visual Studio可以在Mac OS上进行通信并构建项目.

Can be build from Xcode or include the source file into Unity. You can also create it with visual Studio too. Just follow the Android step above but use Install iOS support for C++ (Update x) this time instead of Install Android support for C++ (Update x). Note that you need Mac computer to build for iOS or use virtual machine. Once you do this, follow this Microsoft instruction to finish the setup so that Visual Studio can communicate and build the project on your Mac OS.

将iOS插件文件(不是dll)放入Assets/Plugins/iOS文件夹.受支持的插件扩展为.a.m.mm.c.cpp.

Put the iOS plugin file (not dll) into the Assets/Plugins/iOS folder. The supported plugins extension are .a, .m, .mm, .c, .cpp.

必须使用[DllImport ("__Internal")]而不是[DllImport("PluginName")][DllImport("FirstDLL")],如以下#9 所示.

Must use [DllImport ("__Internal")] instead of [DllImport("PluginName")] or [DllImport("FirstDLL")] as seen below in #9.

9 .从Unity/C#调用C ++函数:

9.Calling C++ function from Unity/C#:

[DllImport("FirstDLL")]
public static extern int add(int num1, int num2);
[DllImport("FirstDLL")]
public static extern int multiply(int num1, int num2);
[DllImport("FirstDLL")]
public static extern int substract(int num1, int num2);
[DllImport("FirstDLL")]
public static extern int divide(int num1, int num2);


void Start()
{
    Debug.Log("Add: " + add(10, 2));
    Debug.Log("Multiply: " + multiply(10, 2));
    Debug.Log("Substract: " + substract(10, 2));
    Debug.Log("Divide: " + divide(10, 2));
}

输出:

10 .故障排除插件错误:

1 .得到错误:

DllNotFoundException:

DllNotFoundException:

解决方案1 ​​:

DllImport中指定的DLL的名称与Dll名称不匹配.通过重命名它们来确保它们匹配,然后重新启动Unity.

The name of the DLL specified in DllImport does not match the Dll name. Make sure they match by renaming them then restart Unity.

解决方案2 :

该DLL放置在错误的文件夹中.该文件夹必须命名为Assets/Plugins.拼写也区分大小写.

The DLL is placed in the wrong folder. The folder must be named Assets/Plugins. The spelling is also case sensitive.

2 .得到错误:

EntryPointNotFoundException:

EntryPointNotFoundException:

解决方案1 ​​:

声明为DllImport的函数名不存在或与C ++侧声明的函数名匹配.确保双方的拼写相同.拼写也区分大小写.

The function name declared DllImport doesn't exist or match with the one declared on the C++ side. Make sure that the spellings are the-same on both sides. The spelling is also case sensitive.

解决方案2 :

C ++ DLL函数未包含在C ++插件中.在Windows上,dllexport用于使这些函数自己导出到DLL中.在其他平台或操作系统中,这不是必需的.通常在头文件中执行此操作只是为了保持源文件的干净.请参见上方头文件中的示例或下方屏幕截图.

The C++ DLL functions are not being included in the C++ plugin. On Windows, dllexport is used to make these function export themselves in the DLL. This is not required in other platforms or OS. This is usually done in the header file only to keep the source file clean. See example in the header file above or screenshot below.

解决方案3 :

您的编译器正在重命名C ++函数.您可以通过使用extern关键字将它们括起来来防止这种情况.同样,请参见上方头文件中的示例或下方屏幕截图:

You compiler is renaming the C++ functions. You can prevent this by enclosing them with the extern keyword. Again, see example in the header file above or screenshot below:

2 .没有错误,但结果不正确或有线:

2.Getting no error but incorrect or wired result:

解决方案1 ​​:

参数不匹配.确保C ++和C#端的函数参数匹配,并且具有相同数量的参数.数据类型也必须匹配.如果不这样做,请期待未定义的行为.

The parameter doesn't match. Make sure that the parameter of the function on the C++ and C# side match and have the-same amount of parameter. Also the datatype must match. If they don't, expect undefined behavior.

这篇关于为Unity构建C ++插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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