以C+编写的C#bundle导入+;(Unity3D项目)

以C+编写的C#bundle导入+;(Unity3D项目),c#,c++,unity3d,dllimport,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;

我尝试使用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);
}
}
在bundle中,此函数的声明如下:

#ifdef __cplusplus
extern "C" {
#endif
EXPORT BOOL ANT_Init(UCHAR ucUSBDeviceNum_, ULONG ulBaudrate_);  //Initializes and opens USB connection to the module
#ifdef __cplusplus
}
#endif
这只是一个示例,其中只有一个bundle函数。如果我只是在脚本中导入函数,我不会在Unity中得到任何错误。 有人能帮我弄清楚吗?

试试这个:

[DllImport("antlib.bundle")]
static extern int ANT_Init(byte ucUSBDeviceNum_, uint ulBaudrate_); 
您的代码中有两个问题:

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

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

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

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

    您的捆绑包名称和库名称不必匹配,但您的plist需要通过指向正确的库来解决此问题。

    我也尝试了您的解决方案,但遇到了相同的问题。我尝试使用的库来自一个传感器,该传感器将信号发送到无线和U盘。也许我用错误的方式从bundle调用函数。这里是我使用的捆绑包的链接:(捆绑包在ANT_Library_MacOSX_Package/Bin中)我也尝试过你的解决方案,但同样的问题。我尝试使用的库来自一个传感器,该传感器将信号发送到无线和U盘。也许我用错误的方式从bundle调用函数。以下是我使用的捆绑包的链接:(捆绑包位于ANT_Library_MacOSX_Package/Bin中)