Skip to content

Instantly share code, notes, and snippets.

@tgjones
Last active August 17, 2021 08:43
Show Gist options
  • Save tgjones/8c1c46e1950a7b9a4769 to your computer and use it in GitHub Desktop.
Save tgjones/8c1c46e1950a7b9a4769 to your computer and use it in GitHub Desktop.
Automatically adding MonoGame XNBs into an assembly as Embedded Resources
Replace the "BeforeBuild" target in your csproj with the XML above.
"ExtraContent" is defined in MonoGame.Content.Builder.targets. It contains the paths
to all the XNB files that have just been built by the MonoGame Content Pipeline.
The "LogicalName" mess converts paths delimited by backslashes into paths
delimited by periods, suitable for use as embedded resource names.
The end result of this is that all your XNB files for this project
will be added to your output assembly as Embedded Resources.
Additionally, the XNB files will not be copied to the output directory
(you can remove lines 9 and 10 if you *do* want that to happen).
<Target Name="BeforeBuild" DependsOnTargets="BuildContent">
<ItemGroup>
<!-- Include just-built XNB files as embedded resources in output assembly -->
<EmbeddedResource Include="@(ExtraContent)">
<LogicalName>Apollo.Resources.Compiled.$([System.String]::new('%(RecursiveDir)').Replace('\', '.'))%(Filename)%(Extension)</LogicalName>
</EmbeddedResource>
<!-- Don't copy XNB files to output directory -->
<Content Remove="@(ExtraContent->'$(ParentOutputDir)\%(RecursiveDir)%(Filename)%(Extension)')" Condition="$(MonoGamePlatform) != 'Android'" />
<AndroidAsset Remove="@(ExtraContent->'$(ParentOutputDir)\%(RecursiveDir)%(Filename)%(Extension)')" Condition="$(MonoGamePlatform) == 'Android'" />
</ItemGroup>
</Target>
@georg-eckert-zeiss
Copy link

Thanks for sharing @tgjones. How is it loaded with the contentmanager? Cheers, Georg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment