Content include raw asset file when build?

Hi, I’m new to Monogame and having hard time getting used to this framework.

I wanted to test cross-platform project in VS 2017, and somehow achieved this with ‘shared project’ way with shared content.

This is my shared project’s .projitem file right now :

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
    <HasSharedItems>true</HasSharedItems>
    <SharedGUID>...</SharedGUID>
  </PropertyGroup>
  <PropertyGroup Label="Configuration">
    <Import_RootNamespace>MonoCrossShared</Import_RootNamespace>
  </PropertyGroup>
  <ItemGroup>
    <Compile Include="$(MSBuildThisFileDirectory)EngineTest.cs" />
    <Compile Include="$(MSBuildThisFileDirectory)Scenes\TestScene.cs" />
  </ItemGroup>
  <ItemGroup>
    <MonoGameContentReference Include="$(MSBuildThisFileDirectory)Content\MonoCrossContent.mgcb" />
  </ItemGroup>
  <ItemGroup>
    <None Include="$(MSBuildThisFileDirectory)Content\Monocle\MonocleDefault.spritefont" />
    <None Include="$(MSBuildThisFileDirectory)Content\Monocle\MonocleDefault.xnb" />
    <None Include="$(MSBuildThisFileDirectory)Content\MonoCrossContent.mgcb" />
  </ItemGroup>
</Project>

So, OK, On Windows and Android Content.Load<> works well.

But when I try to load texture with Texture2D.FromStream() method, I get FileNotFoundException.

This is how I find path to raw .png file :

if (!File.Exists(Path.Combine(Game.Instance.Content.RootDirectory, ImagePath)))
   throw new FileNotFoundException("Texture file does not exist: " + Path.Combine(Engine.Instance.Content.RootDirectory, ImagePath));

FileStream stream = new FileStream(Path.Combine(Game.Instance.Content.RootDirectory, ImagePath), FileMode.Open);
Texture2D tex = Texture2D.FromStream(Engine.Instance.GraphicsDevice, stream);

(*This is ripped from Matt Thorson’s Monocle Engine source code!)

I found out the reason is that in my target project’s build directory(ex. bin\Windows\x86\Debug\Content), there are only .xnb files, no .png files included. When I manually added .png files there, it loads well.

So, how can I add raw asset files to Content folder when I build project?

OK I just found out how to resolve this.

In content pipeline manager, set settings-build action as Copy. Then pipeline just copies your assets.

So here is how I understand this :

Build action is ‘Build’ then you use Content.Load<>.
Build action is ‘Copy’ then you use FromStream().

Simple as that. I feel dumb to find out this…

1 Like