Creating a cross platform project


is it possible to make the same project run on Windows, Android and IOS, or am I supposed to copy the code into a new project like in the sample “Platformer2D”? That would mean copying every change, every time, which is not a very good thing…
Thank you.

In Visual Studio you can create “Links” to other files.

Including linked files from outside the project directory in ASP.NET Core.

You can use a “shared project”, which will allow you to use the same code files and content files for different frameworks/platforms. I can post a basic step-by-step in about an hour.

So I wrote this a month ago to reference off of whenever I would want to create new shared projects. Hope it’s useful for you:

“Monogame Shared Library Project”, “Shared Library Project”, and “Shared Project” may be used interchangeably at times.

  1. [If you have already created a new “Shared Library Project”, skip to step 4]

  2. Select “File > New > Project” .

  3. Select “Monogame Shared Library Project”. Ensure you keep the checkbox “Place solution and project in the same directory” unchecked.

  4. Right-click the solution, then select “Add > New Project”.

  5. Select whichever platform specific project you wish to start with. I began with the OpenGL version first.

  6. When you add a project, it will automatically add a “Game1.cs” to that project. We only need the “Game1.cs” from the “Shared Project”. So for each platform specific project, delete each newly created “Game1.cs”.

  7. Everything I said for step 6, do the same thing with the “Content” folders for each of the platform specific projects.

  8. For each of these platform specific projects added, we will need to add a reference to the “Shared Project”. Right-click on the platform specific project, then select “Add > Shared Project Reference”.

  9. Select the name for the “Shared Project” and click “Ok”.

  10. Right-click the “Shared Project”, then select “Open Folder in File Explorer”.

  11. After the file explorer opens, open the file with extension “.projitems” in a text editor.

  12. After this file opens, scroll down until you find a line(s) that may say both or either of the following:

< MonoGameContentReference Include=“$(MSBuildThisFileDirectory)Content\Content.mgcb” />

< None Include=“$(MSBuildThisFileDirectory)Content\Content.mgcb” />

  1. As long as the “MonoGameContentReference Include” line is there, you can close out. Otherwise, just add it within the “ItemGroup” section (“< ItemGroup >your stuff< /ItemGroup >” ).

Also if you need code to only execute on only certain platforms, you can check preprocessor flags/directives:

...your code (no brackets {} required)

#if __ANDROID__
...your code

There’s also WINDOWS, OPENGL, etc.

1 Like

Thanks for replying. I assume that there is no need to create a cross platform project, I could use linking, or make a shared project using a DirectX template as well.

The cross platform project should be selected if you want to create a project that uses OpenGL, which is compatible with Windows, Linux, and Mac. And yes, you can use the DirectX, Android, and/or IOS templates within a shared project.

The shared project doesn’t actually have a specified platform, so it can’t launch on it’s own. Your platform specific projects (Cross-platform (OpenGL), DirectX, Android, IOS, etc.) use the files within the shared project to launch within their framework. Each platform specific project will still have their own Program.cs, Icon.bmp (or Icon.ico), dependencies, etc, but all the code files, content files, etc. should be put in the shared project.

There’s probably other ways of doing what you require, but this is how I’ve been doing it. There are some issues I’ve had with using this setup initially, however I figured it out. Like for example: sometimes .cs files will display as “Miscellaneous” instead of OpenGL, DirectX, or whatever and won’t be recognized (bunch of errors). To fix it, you have to exclude the file from the project, then reinclude it, and it’ll work fine. Small oddities like that, but it rarely happens.

Thanks again smile:

@Mateo_Loooong , almost end of 2022 and your step-by-step is great ! Thanks for the hint to how setup a MonoGame Shared Projects !!! Quick, clean and usefull !!!

1 Like

Great :+1: I will try
thank you

1 Like