How to debug external .net Dlls

Hi,

We are currently evaluating Unity 3.1 for our next project. As recommended in this posting, we moved all of our game code to a Visual Studio solution outside of the asset folder. This enables us to use namespaces and multiple projects inside of this one solution. The build target of the managed assembly is a folder in the Asset folder of our Unity project. This works fine, created scripts are available in the Unity editor and the managed dll can also be used when deploying the project for the web player.

However, I have found no way to debug the built dll. I tried to debug it by adding the vsproj created by Unity automatically to my custom solution as the startup project and starting Unity from MonoDevelop. I can debug and step through scripts inside Unity without any problems, but breakpoints in the files that are compiled to the external dll are never reached.

Is there a way to enable debugging for external, managed dlls? (Note: the .pdb file for the assemby is build and copied to the same destination as the corresponding dll inside the asset folder)

Being able to debug and step through our code is a crucial feature for us as it is a very convienient way to find and fix problems in the code.

Thanks in advance and with best regards,

Johannes

Unfortunately Visual Studio does not support debugging Unity projects for the moment. However, since Unity 3.2 one can attach to the running Unity process from MonoDevelop. This does not have to be from your Unity solution, but from any external dll project that is being used in your solution.

The trick is to create an mdb file for the external assembly and place it in the same folder as the dll that you import into your assembly.

"C:\Program Files (x86)\Unity\Editor\Data\Mono\lib\mono\2.0\pdb2mdb.exe" extlib.dll

1.)Set the output path of the mono external dll project to be somewhere in your asset folder. As a post build step, you can run pdb2mdb.exe to create the mdb file in the same folder.

2.)Attach to the Unity process.

3.)Set a breakpoint anywhere you like

4.) Now exercise the code by running your scene in the Unity Editor.

It should break in the external dll.

[Edit]It seems this doens't work with .NET dlls[/Edit]

I've made several external dlls myself to be used in Unity. I only made C++ dlls, but that should still apply to C# ones. Obviously you'll want to make debug builds of your dlls as you'll have way more information for debugging (but be careful as someone without Visual C++ or C# installed might not be able to get your dll to run properly. In this case, build your dll in Release mode for everyone and in Debug for yourself). Then run Unity, open your debugger (Visual C++ or C#), attach it to Unity (Debug->Attach to Process->Unity), open the files you need breakpoints in, see VS stop the process once those breakpoints are reached.

Again, I can't be 100% sure it'll work with C# dlls, as I only did that with C++ dlls, but I did get breakpoints working in VS C++ on dlls I made myself while they were being used in Unity.

Best of luck.

I can confirm that this works with Visual Studio 2012, Unity 4.1.5f1 and Windows 8, with .Net DLLs.