How do I call unmanaged functions in C from Unity3d if I don't have a .bundle? (Mac)

I am on a Mac.

I have a bunch of c source code (.c & & .h).

I have a static library (.a).

I want to use this .a library from within Unity.

I have looked at the Unity documentation for plugins (http://unity3d.com/support/documentation/Manual/Plugins.html) which says that for PC and Mac stand alones .bunlde files seem to be the only solution.All the examples of plugins. which Unity gives have .bundle plugins.

But I've seen prime31 plugins using the .a library in Unity!

Does anyone know how they did it?

Here's all I can tell by parsing the prim31 plugin:

(1) they put their .a library in the Unity Editor folder

(2) they have a C # script that contains a lot of [DllImport ("__Internal")]

I tried to do the same:

(1) I wrote a simple hello_world.c in Xcode and created a .a library. I put libhelloworld.a in Asset / Editor

(2) Then I wrote a C # script that looks like this:

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

public class testlib : MonoBehaviour {
    [DllImport ("libhelloworld")]
    static public extern System.String helloworld();
}

      

(3) Then I wrote a test script:

using UnityEngine;
using System.Collections;

public class test : MonoBehaviour {

    // Use this for initialization
    void Start () {
        print(testlib.helloworld());
    }


}

      

(4) So I get a runtime error:

DllNotFoundException: libhelloworld
test.Start () (at Assets/Script/test.cs:8)

      

Many, many thanks!

PS: In case some of you are wondering why I have the source code but not the package. I am trying to build a package from these .c and .h but have not succeeded so far. The source code was compiled with the make tools, but I don't know of anything else that could build the package except for Xcode. So I think I need to use Xcode to build my package. My problem is that when I tried to build it with Xcode, I get millions of errors saying that I have duplicate master records. I checked the source code and found that it has a duplicate of main (), because the source code contains many utilities that come with it. I tried to uninstall these utilities, but the same error persists ...

I plan to ask this question elsewhere because it doesn't seem like unity. But if anyone here knows the answer, please don't hesitate - let me know!

+3


source to share


2 answers


Okay, I got an email from great people. It cannot be done.

Unity does not build an Xcode project for OSX, so you cannot link a static library. Unity builds an Xcode project for iOS which as prime31 developers could use the .a library (in Xcode, not Unity)

To summarize, to use unmanaged code in Unity on MacOS, the only correct way is to build the package from source and then import it into Unity. Just like their documentation says!

I think I'll try to build a package from source, or try to build a package from a .a library, which I can now build, even though it's pointless in the Mac OS documentation:



Note. Some Xcode targets (such as wrapper tools and static libraries) will not create a package or package. This is fine and there is no need to create packages specifically for these types of purposes. The resulting binaries generated for this purpose are intended to be used as is.

I can post another question and if this question is answered I will include a link here ...

Ok, I go back to editing:

Finally, I built a package from the .a static library and was able to call the library functions from Unity. This is how I did it: How to organize the C source file previously compiled by GCC Make and build them into an Xcode package? I have Duplicate Symbol _main Error

+2


source


I am using my own library for an iOS project and it works great. I am not using the Unity3D mechanism to copy from the plugins folder, but have completely customized it in Xcode. In the above HelloWorld example, try it [DllImport("__Internal")]

.



Follow the links in my previous answer How to use an xcode game on unity3d , take a look at my blog post, it addresses exactly this issue.

0


source







All Articles