- Home /
Problem importing a mixed-mode (C++/CLI) library
I am re-familiarising myself with Unity after roughly 2 years working in non-Unity projects, so there may be new features that I am not familiar with. I am developing a topological software library called Topologic (https://topologic.app/) and would like to make it work in Unity. This library contains 2 layers: native layer (C++) and a managed layer (written in C++/CLI). My problem is that I cannot simply load the managed DLL. After loading the DLL (adding it as a reference in Visual Studio), the classes inside it, Unity fails to run the code.
What I wrote in the Unity code (.cs) is as follows.
Topologic.Vertex vertex = Topologic.Vertex.ByCoordinates(0, 0, 0);
This is fine in Visual Studio, but Unity gives me the following messages.
Assets\TestUnity.cs(14,9): error CS0246: The type or namespace name 'Topologic' could not be found (are you missing a using directive or an assembly reference?)
Assets\TestUnity.cs(14,35): error CS0103: The name 'Topologic' does not exist in the current context
I have been reading existing posts and (please correct me if I am wrong) there seems an incompatibility issue with C++/CLI. It has been suggested that this happens because Unity uses Mono .NET which does not support mixed-mode assemblieslink. This post comes from 2016, so I am wondering if if this is still the case?
Another question is, is there any solution to circumnavigate this problem? I am trying to find methods simpler than using P/Invoke. What I have tried are:
Create a purely managed DLL (C#) that inherits classes in the C++/CLI DLL. Produces the above error messages.
Compile the C++/CLI DLL with /pure:clr (as suggested in the link above), but this produces tons of error messages in Visual Studio.
Many thanks,
Nicholas
Did you finally find any solution or workaround? From what I'm reading through the forums it seems that Unity or $$anonymous$$ono don't support C++/CLI
Answer by Cornelis-de-Jager · Feb 25, 2019 at 12:09 AM
I don't think Unity Supports C++.
Please correct me if I am wrong, but my impression is that it does support C++ but through a managed script/DLL. As for the C++/CLI language, it is supposed to create a managed DLL I believe, but it is detected as native in Unity.
Answer by Bunny83 · Feb 25, 2019 at 11:31 AM
You should copy your DLL into your plugins folder in Unity. Adding references manually to the C# project in Visual Studio has no effect since Unity does not use that project at all. Unity compiles the scripts itself by running the compiler directly. Any references has to be setup directly in Unity. The VS project only exists so IDEs like visual studio can work with it. If you have the dll inside your plugins folder (inside your assets folder), Unity will add the references automatically.
the method u suggested dont work. i have tested it. it seems unity cant recognize the cpp clr library.
two step can work: 1, cpp cli complie option: /clr:safe 2, unity allow unsafe code.