Question

I am on Mac.

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

I have a static library (.a).

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

I looked into Unity's documentation for plug-ins (http://unity3d.com/support/documentation/Manual/Plugins.html), which says for PC and Mac stand alones, .bundle files seem to be the only solution. All example plugin-in projects that Unity give have .bundle plugins.

But I have seen prime31 plugins using .a library in Unity!

Anyone has a clue how they did that?

Here is all that I can tell from analyzing prime31 plugin:

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

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

I tried to do the same:

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

(2) I then 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) By doing 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 wonder why I have the source code but not the bundle. I am trying to build a bundle from those .c and .h but hasn't succeeded yet. The source code was compiled with make tools, but I don't know anything else that could build a bundle except for Xcode. So I guess I have to use Xcode to build my bundle. My problem is when I tried to build it with Xcode I get millions of errors saying that I have duplicate main entries. I checked the source code and found that it does have duplicate main()s because the source code has lots of utilities that come with it. I tried deleting those utilities but the same error doesn't go away...

I am planning to ask this question somewhere else because this does not seem very Unity-related. But if someone here happens to know the answer, please don't hesitate -- let me know!

Was it helpful?

Solution

Okay I got an email from the prime31 people. It's impossible to do that.

Unity does not build an Xcode project for OSX, so you can't link a static library. Unity does build an Xcode project for iOS, that's how the prime31 developers could use the .a library(in Xcode, not in Unity)

To sum up, to use unmanaged code in Unity on MacOS, the only correct way is to build a bundle from your source code and then import it to Unity. Just like their documentation said!

I think I will try to build a bundle from source code or try to build a bundle from the .a library that I am now able to build, though Mac OS documentation says it's pointless:

Note: Some Xcode targets (such as shell tools and static libraries) do not result in the creation of a bundle or package. This is normal and there is no need to create bundles specifically for these target types. The resulting binaries generated for those targets are intended to be used as is.

I may post another question and if that question got answered, I will include a link here...

Alright I am coming back to edit:

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

OTHER TIPS

I am using my own library for an iOS project and it works fine. I don't use Unity3D's mechanism of copying from plugins folder but set it up completely in XCode. In the above HelloWorld example try [DllImport("__Internal")].

Follow the links in my previous answer How to use an xcode game on unity3d look at my blog posting, it deals exactly with this problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top