Tutorial: Dotnet core 3.0 / 3.1 with monogame
This tutorial will allow you to run monogame with dotnet core 3.0 / 3.1 . It requires some setup, once done, its pretty sweet.
Initialization:
- We will be using Visual Studio Code. Make sure visual studio code is installed before you do anything below. Can get it here: https://code.visualstudio.com/
- Make sure you have Monogame libraries because we will need the content pipeline application to build our png files into xnb. Can get monogame libraries here: http://www.monogame.net/downloads/
Before we start, make sure you have dotnet core installed:
- Load up Command Prompt. Can easily do this on windows: Microsoft Key + R and type cmd and press enter.
- type: dotnet --version
- if it doesnt say 3.0 or greater, need to visit https://dotnet.microsoft.com/download and install the dotnet core latest runtime libraries
After All The Setup
We need to install a very simple dotnet core 3.0 application. For this tutorial we will be using dotnet core console application. Make sure your command prompt is in the folder where you want your application.
Open Command prompt, which was displayed above. We want to create a simple starting point for the monogame to work with dotnet core 3.0 / 3.1 .
- type dotnet new console
- Once its done, type code .
Visual studio code should of loaded the console project. Press F5 to make sure no errors accrued.. In visual studio debug console, should have seen "Hello World" with no errors.
Tip: If it pops up with a little menu, click dotnet core and press F5 again.
Nuget Package Installation:
We need a few nuget packages, we will need: 'Monogame.Content.Builder' and 'MonogameFramework.DesktopGl.Core'.
In your visual studio code terminal type these these commands:
- dotnet add package MonoGame.Framework.DesktopGL.Core --version 3.8.0.1
- dotnet add package MonoGame.Content.Builder --version 3.7.0.9
Setting Up The Content:
In order for content to work, we will need a folder inside your project path.
- Create a new folder called Content (in the same case).
- If on windows 10, in your search at the bottom left, type Monogame Pipeline and open it. If not on windows 10, press the button at the bottom left, locate Monogame and click on Content Pipeline.
- Once opened, click File -> new and direct to your project content folder. Make sure the Content Pipline file is saved as Content
Setting Up Content Pipeline:
A few things have to be adjusted for the content to work, nothing major.
- Select 'Content' inside your content pipeline application. A property list should appear at the bottom left of the application. We need to change a few things.
- Click the right side IntermediateDir. We cannot have it as 'obj' but type obj/$(Platform)
- We need the same thing for OutputDir. Type: bin/$(Platform)
- Lastly, change Platform to DesktopGL
- Add any texture to the content pipeline and press F6 to build your content.
Displaying the content:
Before we start, we need a starting point for monogame to work. Add a class called App.cs and type the stuff below:
Note: Make sure to change 'Game Namespace' to your application namespace and make sure to change "yourtexturename" to your texture that was added in the content pipeline application.
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Game
{
public class App : Game
{
SpriteBatch spriteBatch;
Texture2D texture2D;
public App()
{
Content.RootDirectory = "Content";
var graphics = new GraphicsDeviceManager(this)
{
PreferredBackBufferWidth = 1280,
PreferredBackBufferHeight = 720,
};
graphics.ApplyChanges();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
texture2D = Content.Load<Texture2D>("yourtexturename");
base.LoadContent();
}
protected override void Draw(GameTime gameTime)
{
spriteBatch.Begin();
spriteBatch.Draw(texture2D, Vector2.Zero, Color.White);
spriteBatch.End();
}
}
}
We also need to adjust the Program.cs to this:
namespace Game
{
class Program
{
static void Main(string[] args)
{
using(var app = new App())
app.Run();
}
}
}
Lastly if you try to run it, you're going to get an error about content not able to load. We also need to adjust a few things inside your application csproj file.
Tips:
- If you want your application to show up as an "EXE" not an "DLL", change your output from Exe to WinExe
- Best tip: dont copy all the code below but add the lines that isn't in your csproj file.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<MonoGamePlatform>DesktopGL</MonoGamePlatform>
</PropertyGroup>
<ItemGroup>
<MonoGameContentReference Include="**\*.mgcb" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="MonoGame.Content.Builder" Version="3.7.0.9" />
<PackageReference Include="MonoGame.Framework.DesktopGL.Core" Version="3.8.0.1" />
</ItemGroup>
</Project>
Press F5 and should see your texture with no errors. Hope it helps!
Posted Dec 23, 2019, 10:50 AM UTC · edited