如何通过 Xcode 构建插件,包括 OpenCV 库(或其他第 3 方库)以供 Unity 使用?

How to build plugin by Xcode include OpenCV library (or another 3rd party library) to give Unity to use?

我知道Unity在运行时无法动态调用插件中的3rd party library(非标准库)。因此,我们需要在构建之前在插件中包含一些静态库。但是,我不清楚如何设置我的 Xcode 项目,其中包括一些静态库和设置过程。

我试图找到一些讨论这个主题的资源或教程,但我只找到 tutorials 使用 Visual Studio,而不是 Xcode。

有人熟悉这个话题吗?

您可以从下面提供的链接中获得所需的帮助-

https://github.com/darshanpv/OpenCV4Unity

https://forum.unity.com/threads/building-opencv-plugin-for-mac-osx.623662/#post-4179892

请尝试一下,如果遇到任何问题请告诉我。只关注 Mac OSX 部分。

最后,我尝试了这个方法并成功构建了 .bundle,其中包含另一个第三方库供 Unity 使用。

让我以 OpenCV 库为例。

首先,点击project,可以看到Build Setting按钮。单击它并修改Header Search PathsLibrary Search Paths。在我的例子中,我输入 /usr/local/Cellar/opencv/3.4.3/include/**/usr/local/Cellar/opencv/3.4.3/lib/**,然后单击 targets 并执行相同的操作。

另外,需要在项目中添加OpenCV库,因为unity在运行时无法动态调用插件中的第3部分库。所以,你需要打包它们然后 Xcode 会自动制作框架。

因此,单击 Build Phases 按钮。现在,您可以在此页面中看到 Link Binary With Libraries,然后单击 + 按钮并单击 add other...。然后,转到您的 OpenCV 库路径

/usr/local/Cellar/opencv/3.4.3/lib (For my case)

Select 没有 "pythonx.x".

的所有文件

现在,您应该会在 Xcode IDE 中看到 Frameworks 列表,然后您可以进行一些测试并检查添加第 3 方库是否成功。

c++:

int ProcessImage()
{
    cv::Mat test(10, 10, CV_8UC1);  //use opencv library
    return test.rows;   // should return 10
}

c++ 头文件

#include <opencv2/imgproc.hpp>
#include <stdio.h>

extern "C"
{
    int ProcessImage();
}

c#

[DllImport("test")]   /*the name of Plugin is Test*/
private static extern int ProcessImage();

Debug.Log(ProcessImage().ToString());

结果