I swear I searched for answers, but either my google skills have failed me or I need a step-by-step process. So, sorry for taking your time.
I found two related questions in the forum (Question about shared code folders in different projects and another which I can’t link because of link limit for new accounts) but the answers left me with more questions or simply didn’t explain things in a way I could understand with my limited mind.
So, the main question is: how do I set up a project to deploy on different platforms (namely OpenGL + Android + iOS later)?
I’m a complete newbie in Monogame, as you can probably guess by the question itself. One of the answers in the previous questions led to this: https://github.com/Apostolique/Apos.Framework which shows a very interesting setup… Yet I have no idea how to accomplish that.
Here I am, with .Net and Monogame installed. I’m starting a new project on either VS or Rider using the templates. Then comes the smaller questions:
Do I start a Desktop OpenGL one first and then an Android? The opposite? None of these options?
Where do I keep the shared data? I tried setting up a .Net Shared Project, but it didn’t let me import the stuff I wanted to use (like Nez). Then I tried the .Net Standard, but I don’t think it’s intended for what I’m doing.
I know that the answer will leave me with questions of how to deploy later, but I’ll cross that bridge when it comes to it. I just want to start correctly.
That first topic is mine, trying to do the same thing a couple of years ago! The best way I’ve found (and the one I use in any MG project now, even if I’m only deploying to one platform) is to put all your core code in an MG shared project and then to add a reference to that shared project in all your platform-specific ones.
So, for example, if you want an OpenGL and an Android project, you could first create the OpenGL project, and then right-click your solution and go Add → New project. This time, choose an MG shared project. Any re-usable code (which will be pretty much all of it) can then be created in the shared project. You can create the Android project in the same way, so you end up with a solution containing three different projects within.
To use your shared code, simply add a dependency for the shared project onto each of your platform-specific ones. This is done by right-clicking, in this case, your OpenGL or Android project and choosing Add → Project reference. You then simply tick the box next to your shared project, and you will have access to your core code as normal.
Content files (stuff like images, shaders, sounds that are used within your game) are a little more awkward, since each .mgcb file has to declare which platform it is compiled for. So, basically you’ve got to have one .mgcb for each platform-specific project (as far as I know…) but you can still re-use the same resources within that .mgcb. I do this in the content editor by going Add → Existing item and then adding a link to the content file I want to use. You have to repeat this for each platform but it’s not the end of the world. An easy workaround is to copy and paste a completed .mgcb file and change the ‘/platform:’ line in a text editor.
I have Platform specific projects for Android / DesktopGL / DirectX
(I delete the Content Folder with the Content.mgcb file in these projects)
All my assets (but not code) go into a Shared Project which also has the Content.mgcb file in it.
This shared project is referenced by each of the Platform Projects.
This works perfectly for the assets as MG passes the correct platform flag to MGCB on build.
i.e. I don’t need to manage a content.mgcb file for each platform.
My game code is in a separate library project targeting net standard 2.1 + DesktopGL which is also referenced by each platform project.
In the project file, set ExcludeAssets=“runtime” to prevent the project’s version of MonoGame being included as a dependency in the Platform Project (we want the platform specific version to be used) <PackageReference Include="MonoGame.Framework.DesktopGL" Version="126.96.36.1991" ExcludeAssets="runtime" />
Any additional libraries e.g. UI Library etc. all target net standard 2.1 + DesktopGL and are referenced by the ‘game’ project. Make sure to also set ExcludeAssets=“runtime” in the project files.
The Kickstarter script is aiming for a setup with one shared mgcb file plus another one for platform specific content for each platform if I remember correctly. I think I had this working but I don’t know at the moment, has been to long ago since I worked on the script…
Maybe new templates are already there coming with the next MonoGame version / release? Then I would say it is maybe not worth the time to try and make the script working. Since I also don’t know if Android is now supported using the command line interface of dotnet core with net core 6 now being released I don’t know if this is possible? Anybody knows if setting up Android projects and adding them to a solution is now possible from the command line? If yes and if I find some time with a bit of luck there is a chance I could get it working to a point where it helps to setup a complete solution for windx, desktopgl and android with shared content. Everything setup automatically.
Just wanted to add that when you want to add specific behaviours for a platform, you don’t need to relay in clustering your code with preprocessor directives for each platform. You can organize it better by using C# partial classes so that those files stay in the platform projects.