- Home /
Considerations/Gotcha's when Creating a C++ CLR Plugin in VS2008
What are the special considerations and gotcha's involved in creating a C++ CLR plugin for Unity with Visual Studio 2008? I ask this because I'm having problems and have read several posts indicating that others are having problems as well.
Reference:
C++ dll plugin problem (Unanswered - My test projects are similar to the one he describes.)
From the forums (They are discussing a lot of complex hacks in the post. But interspersed are simple test cases that are also showing problems.)
I'm limiting this question to VS2008 for the following reason:
- I downloaded the test project "MCPPLIB.zip" from the above mentioned thread. (Note: This is a VS2003 project.)
- I copied its pre-built DLL into the Unity assets folder and tested access to it. Everything was good.
- I opened the MCPPLIB project and converted it to VS2008 format, including the build fixes described at link text. (Left the targeted framework at .NET 2.0)
- I built the DLL and copied it to the Unit assets folder, replacing the VS2003 version.
- Unity crashes as soon as the play button was clicked.
All my C++ CLR test projects are causing Unity crashes.
(On a side note, by VS2008 C# CLR Plugins have no problems.)
Answer by SteveFSP · Jul 18, 2010 at 09:53 PM
This answer applies to Unity 2.6 and Visual Studio 2008
Unlike C#, the default CLR project configuration in Visual Studio is mono-unfriendly. For some details, see the mono-project documentation.
To create a Managed C++ library that is compatible with the free version of Unity, set up its project as follows:
- Start Visual Studio 2008.
- Select File > New > Project > Other Languages > Visual C++ > Class Library
- After project creation, right-click the project and select Properties.
- Select Common Properties.
- Set the Targeted Framework to .NET Frameworkd 2.0
- Set Confuration (at top of tab) to "All Configurations".
- Select Configuration Properties > General
- Set Common Language Runtime support to Safe.
If you are re-using existing code you may have to refactor it in order to get it to build.
This may not be the only allowed configuration. But any other Common Language Runtime support type will result in a DLL that will crash or cause run-time errors in the Unity editor.
Other Considerations:
If your library will need access to the UnityEngine namespace, you can add it to your project as follows:
- Bring up the project properties. (Right-click the project and select Properties.)
- Select Common Properties.
- Click Add New Reference...
- Browse to and add the UnityEngine.DLL. (E.g. Under C:\Program Files\Unity\Editor\Data\lib)
Is there any way to debug such a $$anonymous$$anaged C++ dll, stepping through the code line by line?
Sorry, I'm afraid that I don't know the answer. But if you are talking about debugging via Unity 3.0, then the answer is most likely no.