- Home /
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
Answer by Johan 2 · Mar 11, 2011 at 01:21 AM
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.
To confirm -- this is working for me with Unity 3.5.6, $$anonymous$$onoDevelop 2.8.2, and a DLL built with Visual Studio 2010 Express. Thank you!
Correction, this doesn't work with DLLs containing coroutines (pdb2mdb.exe has issues with IEnumerator), but use Unity/Editor/Data/$$anonymous$$onoBleedingEdge/lib/mono/4.0/pdb2mdb.exe and it's all good.
Sorry for the bump - but could someone please clarify how to go about adding a breakpoint in a dll?
I seems work, since It is now 2016 there are many information on the internet to tell someone to debug C# using vs #^%@^&%#^& but no one can clearly define how to debug dll I think you steps works, I will try
Answer by taoa · Dec 12, 2010 at 03:01 PM
[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.
Unfortunately this does not seem to work with .net dlls. I just tried it, but breakpoints are never hit, even when I set the code type to managed. I think it has something to do with the way .net libs are used in Unity. I don't know how it works exactly, but these dlls are interpreted by the mono runtime can also be compiled into a web player package.
I think since .net dlls are interpreted by mono, visual studio does not know what C# code is currently used by the process. This is also described here: http://answers.unity3d.com/questions/23933/how-to-debug-c-dll-code/24108#24108
Quite unfortunate. Unless you want to consider writing your scripts in C++? You might win a few FPS that way :)
Native code is not an option for us, since we want to deploy our project for the webplayer and C++ Code can not be used there.
And to be honest, I happily sacrifice a few FPS for the comfort of C# coding ;-) $$anonymous$$y current solution is to leave all scripts in the unity asset folder, but create a custom solution. This way I can have namespaces (except for $$anonymous$$onoBehaviours), multiple projects AND debugging in $$anonymous$$onoDevelop.
Of course an external dll would be better for reusabily and SVN (no meta files), but I won't sacrifice the ability to step through my code for that.
It does work with .NET DLLs -- working fine for me with Unity 3.5.6 and Windows 7. $$anonymous$$ore details here -- http://forum.unity3d.com/threads/161685-How-to-build-and-debug-external-DLLs
Answer by t-brjon · Jun 24, 2013 at 06:07 PM
I can confirm that this works with Visual Studio 2012, Unity 4.1.5f1 and Windows 8, with .Net DLLs.
@t-brjon, are you debugging with Visual Studio, or just editing your script files? If you are debugging, are you using the UnityVS extension from Syntax Tree? (http://unityvs.com)
I am debugging using $$anonymous$$onoDevelop. I haven't tried UnityVS yet.
I forgot to mention that pdb2mdb.exe needs to be run from the directory that the dll file is in... I had problems with that.
I ended up using post build events as covered here: http://www.youtube.com/watch?v=fphydfXBGW4
I couldn't get pdb2mdb to run from my Assets directory, so I modified the post-build events to:
"C:\Program Files (x86)\Unity\Editor\Data\$$anonymous$$ono\lib\mono\2.0\pdb2mdb.exe" "$(TargetName).dll" echo f | xcopy "$(TargetPath)" "$(ProjectDir)....\Assets\Plugins" /Y echo f | xcopy "$(TargetDir)$(TargetName).pdb" "$(ProjectDir)....\Assets\Plugins" /Y echo f | xcopy "$(TargetDir)$(TargetName).dll.mdb" "$(ProjectDir)....\Assets\Plugins" /Y
Note that my VS solution is two directories below the Assets folder.
Oh, ok, when you said "works with Visual Studio" I thought maybe you'd found a way to debug with VS, now I understand. Thanks for letting us know it's working in Unity 4.
Your answer
Follow this Question
Related Questions
Monodevelop 3 + debugging 0 Answers
Why does the debugger suddenly stop working 2 Answers
MonoDevelop Breakpoints don't stop execution 0 Answers
debugger halt after start(), can only resume in update 0 Answers
Could not connect to debugger 1 Answer