首页 文章

用c编写的C#bundle导入(Unity3D项目)

提问于
浏览
0

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

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

这是我用来调用资源/插件文件夹中的库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

这只是捆绑中只有一个函数的示例 . 如果我只是在我的脚本中导入函数,我在Unity中没有任何错误 . 有人可以帮我搞清楚吗?

2 回答

  • 0

    试试这个:

    [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_);
    
  • 0

    山姆是对的,你的回报 Value 和论据必须匹配 . 另外,请确保捆绑包含info.plist,如下所示:

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

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

相关问题