用C ++编写的C#包中的导入(Unity3D项目)

时间:2015-03-17 00:57:50

标签: c# c++ unity3d dllimport

我尝试使用Unity3D中的.bundle文件中的函数,但每次调用函数时都会出错:

DllNotFoundException: libant
connectANT.Start () (at Assets/connectANT.cs:13)

这是我用来调用Assets / Plugin文件夹中的库antlib.bundle的脚本:

using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;

public class connectANT : MonoBehaviour {
[DllImport("libant")] static extern bool ANT_Init(int ucUSBDeviceNum_, int ulBaudrate_); 
void Start () {
    ANT_Init (1, 50000);
}
}
在包中

这个函数声明如下:

#ifdef __cplusplus
extern "C" {
#endif
EXPORT BOOL ANT_Init(UCHAR ucUSBDeviceNum_, ULONG ulBaudrate_);  //Initializes and opens USB connection to the module
#ifdef __cplusplus
}
#endif

这只是捆绑中只有一个函数的示例。如果我只是在我的脚本中导入函数,我不会在Unity中收到任何错误。 有人能帮我解决一下吗?

2 个答案:

答案 0 :(得分:0)

试试这个:

[DllImport("antlib.bundle")]
static extern int ANT_Init(byte ucUSBDeviceNum_, uint ulBaudrate_); 

您的代码中存在两个问题:

  1. 错误的文件名
  2. 错误的参数类型和返回类型
  3. 如果你想使用bool作为返回类型,则需要另一个属性:

    [return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
    [DllImport("antlib.bundle")]
    static extern bool ANT_Init(byte ucUSBDeviceNum_, uint ulBaudrate_); 
    

答案 1 :(得分:0)

Sam是对的,你的返回值和参数必须匹配。 此外,请确保捆绑包含info.plist以及:

<key>CFBundleExecutable</key>
<string>antlib.dylib</string>

您的包名称和您的库名称不必匹配,但您的plist需要通过指向右侧的lib来解决此问题。