Post-build events with MDB files in Visual Studio and Unity
Asked Answered
J

1

1

My source code for my MonoBehaviours in Unity is not scripts inside the Assets folder, but compiled DLL's that I add as Plugins.

I have added post-build events in Visual Studio for my C# project, to try to get around the work with having to copy the DLL file and the MDB file every time I've changed something.

copy /Y "$(TargetDir)$(ProjectName).dll" "$(SolutionDir)\..\Assets\Plugins\$(ProjectName).dll"
copy /Y "$(TargetDir)$(ProjectName).dll.mdb" "$(SolutionDir)\..\Assets\Plugins\$(ProjectName).dll.mdb"

It works like a charm... almost. The problem I'm encountering, is that the DLL builds, then this command is run, but the MDB file hasn't had time to update yet, so I get an old MDB file copied to the Assets/Plugins folder.

Is there a way to wait for the MDB file to update before copying it?

Jokester answered 13/2, 2017 at 7:46 Comment(2)
You update the MDB with what?Jennifer
The MDB file is strictly dependent on a specific version of a DLL, it has to be re-generated by Visual Studio every time the DLL is rebuilt, or else it will cause errors in Unity. As it seems, that is done after the DLL is built, which makes sense. So it will be done mere seconds after the DLL is built, but then the post-build event has already been run.Jokester
J
1

I now have solution that works. Basically, the copying of the DLL and MDB files is part of the process of generating the MDB file from the PDB file in my C# project properties, as build targets, where the copy won't be performed until the MonoMdbGenerator is done executing.

<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="AfterBuild">
    <CallTarget Targets="GenerateMonoSymbols" Condition=" Exists('$(OutputPath)\$(AssemblyName).pdb') " />
</Target>
<Target Name="GenerateMonoSymbols">
    <Message Text="$(ProjectName) -&gt; $(TargetPath).mdb" Importance="High" />
    <Exec Command="$(MonoMdbGenerator) $(AssemblyName).dll" WorkingDirectory="$(MSBuildProjectDirectory)\$(OutputPath)" />
    <CallTarget Targets="CopyDLL" />
</Target>
<Target Name="CopyDLL">
    <Copy SourceFiles="$(OutputPath)\$(AssemblyName).dll" DestinationFolder="$(SolutionDir)..\Assets\Plugins\$(ProjectName)" />
    <Copy SourceFiles="$(OutputPath)\$(AssemblyName).dll.mdb" DestinationFolder="$(SolutionDir)..\Assets\Plugins\$(ProjectName)" />
</Target>

Some good reads on the subject:

Jokester answered 16/2, 2017 at 9:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.