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:

  1. I downloaded the test project "MCPPLIB.zip" from the above mentioned thread. (Note: This is a VS2003 project.)
  2. I copied its pre-built DLL into the Unity assets folder and tested access to it. Everything was good.
  3. 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)
  4. I built the DLL and copied it to the Unit assets folder, replacing the VS2003 version.
  5. 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.)

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:

  1. Start Visual Studio 2008.
  2. Select File > New > Project > Other Languages > Visual C++ > Class Library
  3. After project creation, right-click the project and select Properties.
  4. Select Common Properties.
  5. Set the Targeted Framework to .NET Frameworkd 2.0
  6. Set Confuration (at top of tab) to "All Configurations".
  7. Select Configuration Properties > General
  8. 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:

  1. Bring up the project properties. (Right-click the project and select Properties.)
  2. Select Common Properties.
  3. Click Add New Reference...
  4. Browse to and add the UnityEngine.DLL. (E.g. Under C:\Program Files\Unity\Editor\Data\lib)

nice!well done!

nice!
well done!

u do a good job, man!