开发者

C++/CLI wrapper for native C/C++ code, unable to load in Unity3D

I've been looking and looking all over the net about how to load an assembly in Unity3D, and I'm having a real hard time. I'm sorry if this post will be long, but I'll be putting everything I've learned and how far I've gotten since this is my first time working in .net and dlls.

I have a native dll, it has a whole bunch of extern "C" so I can load everything at runtime. This works in unity if I use the [DLLImport] attribute and such. However, it's cumbersome, and not really reusable code. It would become even more cumbersome later when I will have to abstract my system between more than 1 native library.

So I figured I'd do a C+++/CLI wrapper and then load it in Unity3d like any other dll and just link the namespace: "using MyWrapper;"

I created the simplest C++/CLI lib I could think of. all my lib does is have a class (Class1) and it has a function int getnum() {return 5;}. I'm using VC++ 2010 express, and I'm building with V90, and modified the vcxproj file to target 2.0. I know unity only supports 2.0. I'm building in /clr in order to be able to have native and .net code.

It completely crashes unity3d. This is my Error log at GameManager.Awake () [0x0001d] in Manager\GameManager.cs:116 at GameManager.Awake () [0x00000] in Manager\GameManager.cs:107 at (wrapper runtime-invoke) GUIRadioButton.runtime_invoke_void (object,intptr,intptr,intptr) <0xffffffff> Receiving unhandled NULL exception

if I build it in /clr:safe instead it works fine. /clr:pure also doesn't work.

So I decided to do an external c# command line project to test everything. I load my lib, and compile in 2.0 and /clr (mixed mode) works without a problem.

I'm using Unity 2.6 Pro.

It's possibly what I'm looking to do is impossible, I don't know.. I mean I figured that was what .NET was all about. I just want to have a system which can be reused in other projects (C++).

Thanks for any insight.

Here's my test c++/cli project.

#pragma once

using namespace System;

namespace CLRTest {

    public ref class Class1
    {
        // TODO: Add your methods for this class here.
    public:

        Class1(){}

        int getnum (){return 5;}
    };
}

When I create a C# command line project like this. it开发者_StackOverflow中文版 works.

using System;
using System.Collections.Generic;
using System.Text;
using CLRTest;

namespace CLRTestLoad
{
    class Program
    {
        static void Main(string[] args)
        {
            Class1 c = new Class1();

            Console.WriteLine ("num is = " + c.getnum());
        }
    }
}

Executing this line in Unity3d crashes the whole editor. (Yes I did put in the using statement)

Class1 c = new Class1();


AFAIK, C++/CLI assemblies are not working with Unity, as Unity is relying on Mono that doesn't support mixed assemblies. The only workaround is to call functions from a dll with classic p/invoke

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜