Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

C++ Wrapper for Unity

Discussion in 'General Discussion' started by TGPTIAlves, Aug 29, 2014.

  1. TGPTIAlves

    TGPTIAlves

    Joined:
    Aug 29, 2014
    Posts:
    3
    Hello,
    I have a lot of code in C++ that I have to use somehow in C#. I've written a wrapper that works fine when I'm using it via C# project in Visual Studio, so I'm trying to use it with Unity.

    I've copied the DLL to Plugins folder and it indeed recognizes the headers, since When I'm writing code in Unity suggestions pop-up with all the functions I coded in C++. When I try to compile and run it throws me this error:

    Code (CSharp):
    1. MissingMethodException: Method contains unsupported native code
    2. <Module>.<CrtImplementationDetails>.LanguageSupport._Initialize (<CrtImplementationDetails>.LanguageSupport* )
    3. <Module>.<CrtImplementationDetails>.LanguageSupport.Initialize (<CrtImplementationDetails>.LanguageSupport* )
    4. Rethrow as ModuleLoadException: The C++ module failed to load.
    5. <Module>.<CrtImplementationDetails>.ThrowModuleLoadException (System.String errorMessage, System.Exception innerException)
    6. <Module>.<CrtImplementationDetails>.LanguageSupport.Initialize (<CrtImplementationDetails>.LanguageSupport* )
    7. <Module>..cctor ()
    8. Rethrow as TypeInitializationException: An exception was thrown by the type initializer for <Module>
    9. WrapperE.Start () (at Assets/WrapperE.cs:20)
    First I thought it was because I was not targeting the framework of Unity, so I changed it to target .NET framework 2.0, which still works fine when using in a C# project in Visual Studio, but it still throws me the error above.


    Many Thanks!
     
  2. schragnasher

    schragnasher

    Joined:
    Oct 7, 2012
    Posts:
    117
    Do you have unity pro?
     
  3. TGPTIAlves

    TGPTIAlves

    Joined:
    Aug 29, 2014
    Posts:
    3
    Yes
     
  4. lilymontoute

    lilymontoute

    Joined:
    Feb 8, 2011
    Posts:
    1,181
  5. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,613
    Are you using any C++/CLI or unsafe code blocks? Neither will work with Unity.
     
  6. TGPTIAlves

    TGPTIAlves

    Joined:
    Aug 29, 2014
    Posts:
    3
    I tried both. Is there any other way to make it work? If so, can you provide me an example/tutorial/documentation on what I need to do?
     
  7. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,613
    You need to use the Native Plugin (P/Invoke) approach that Thinksquirrel linked to in the post above mine. You'll build your C++ code (not C++/CLI) as a DLL, and then write a C#-side wrapper with 'extern' functions that map to functions in the DLL.
     
    deram_scholzara likes this.
  8. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    Unsafe code works fine in Unity, you just have to enable its usage explicitly passing -unsafe parameter to the compiler.

    C++/CLI, like any other .Net language, works as well, but it has to be safe.
     
    Last edited: Aug 30, 2014