Search code examples
xcodeunity-game-enginebundlestatic-librariesstatic-linking

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


I know that Unity couldn’t dynamically call 3rd party library (non-standard library) in the plugin when it runs. Therefore, we need to include some static library in the plugin before we build. But, I don’t clearly know how to set up my Xcode project which includes some static library and the setup process.

I try to find some resources or tutorials which are talking about this topic, but I just find tutorials which are using Visual Studio, not Xcode.

Anyone familiar with this topic?


Solution

  • Finally, I try this method and successfully build .bundle which include another 3rd party library to give Unity to use.

    Let me take the OpenCV library an example.

    First, click project and you can see the Build Setting button. Click it and modify Header Search Paths and Library Search Paths. In my case, I enter /usr/local/Cellar/opencv/3.4.3/include/** and /usr/local/Cellar/opencv/3.4.3/lib/**, then, click targets and do the same things.

    Also, we need to add OpenCV library in the project because unity couldn’t dynamically call the 3rd part library in the plugin when it runs. So, you need to package them and then Xcode will automatically make the framework.

    So, click the Build Phases button. Now, you can see Link Binary With Libraries in this page and then click the + button and click add other.... Then, go to your OpenCV library path

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

    Select all of the files without "pythonx.x".

    Now, you should see the Frameworks list in your Xcode IDE and then you can do some test and check that add 3rd party library successful or not.

    enter image description here

    c++:

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

    c++ Header

    #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());
    

    Result

    enter image description here