- Home /
Does Unity support mixed assemblies (with both native and .Net code) in a single DLL?
Hi,
I have a DLL written in C++. It also contains a .NET API written in C++/CLI, and these are linked together in a single DLL. The C++ code uses some Win32-specific calls, so I'm happy to be restricted to Windows platforms for now.
From any regular C# project in Visual Studio, I can just add it as a reference and use the .NET API to seamlessly interact with the library - no need for P/Invoke, DllImport
or writing a C API.
However, when I import it as an asset into a Unity project, it's recognized as a native plugin, and I can't use the C# API from any scripts in the project - they all behave as if the DLL doesn't exist. The C++/CLI code is compiled against .Net Framework 4.0, so I've set the Scripting Runtime Version to '.NET 4.x Equivalent', and the Api Compatibility Level to '.NET 4.x'.
I tried working around this by writing a C# library in Visual Studio that references the mixed C++/.Net library, and then importing both this C# library and the mixed library into the Unity project. When I do that the scripts in the Unity project compile, but the entire editor crashes when I press play.
Are mixed assemblies just not supported in Unity? If so, I couldn't find this documented anywhere. If they are, how can I figure out why Unity is treating my mixed assembly DLL as a native plugin rather than a managed plugin?
Hi, I am having same issue. Does anybody knows a solution? Best regards $$anonymous$$,Hi, I am having the same problem. Does anybody knows an answer?
Answer by AP-IGD · Jun 20, 2019 at 12:53 PM
Same problem here, even after a year of having so many followers and no answers....
Hi everyone, no solution but some debugging insights.
I've tried the same thing (Unity 19.3.0a5 x64, Visual Studio 2017) and got the same result. As recommended by MS I've written a CLR (mixed) assembly to wrap my native c++11 lib. To be fully C# compliant, I've written the intermediate C# assembly, basically wrapping only the needed mixed classes/methods. All C# applications I've created/tested, don't cause any problems. Even with multi-threading, hardware controlers etc. no warnings, errors or crashes...
Try, debugging in Editor: Calling the C# lib from a Unity script crashes the editor... I've debugged the whole thing (attached VS debugger to Unity Editor using my libs compiled in debug) and the crash occurs immediatly when my C# lib calls the mixed library (actually its code is never reached...). To be more precise: during some dynamic class generation magic in the mono libs seemingly an error occurs, an exception message is generated and a log-file-write is called. Then good old c-style 'abort()' is called, not very -Stroustrup-...
Try, generating VS solution and debug only my part in pure C. Unfortunatly, 'File'->'Build setting...'->'Build' generation fails with the message
"...But the dll is not allowed to be included or could not be found." (the mixed one is meant here)
Since I've copied the lib to nearly all directories included in path and the project folders, I don't think it's not found. Remaining question:
Why is the library not allowed to be included?
I can only guess, that there's some kind of versioning conflict. .Net, C system libs orororor. I've not found any documentation on that.
This leaves me with to questions to the comunity:
: Does someone here has any info on the exact versions fot .Net, C-std... used by Unity?
: (Most likely answer is NO) Any guess how to wrap native C++ to Unity WITHOUT using global C functions, void pointer and all these unsafe (defined name by MS) methods?
I've quite some experience with tracking down bugs, versioning problems and all that. But, the long work and the complete absence of an error message makes me think, that I've found my master-of-problems...
Your answer
Follow this Question
Related Questions
Unity iOS - "Assertion at reflection.c:6869" only in Development Build 0 Answers
Texture2D manipulation in c++ dll Plugin 0 Answers
DllNotFoundException while using unity native c++ Plugins 0 Answers
C++ native plugin Failed with error '%1 is not a valid Win32 application 0 Answers
How can I get the Native Plugin example to work on Unity 5? 0 Answers