Search code examples
androidiosvisual-studiowin-universal-appmonogame

How to create a class library based on MonoGame to be used from UWP, iOS, and Android


I've installed VisualStudio 2015, Xamarin, and MonoGame 3.5. I'm going to build my XNA app for Windows UWP, iOS, and Android.

I've some libraries based on XNA... and I need to build them for the three target systems (Windows UWP, iOS, and Android), but no way to create a library for Windows UWP. I've tried to create a MonoGame Windows 10 Universal Project, but such a project cannot be converted to generate a DLL.

I've also tried to create a Class Library (Universal Windows) project, but then I was unable to add MonoGame.Framework to the references.

I've the same problem when creating a MonoGame Android Project... no way to switch to a library project. The only MonoGame project that can be modified to be a library project is MonoGame iOS Project.

That said, how can I create a class library based on XNA (MonoGame) to be used from UWP, iOS, and Android?


Solution

  • As far as I know you've only got 2 options here.

    I haven't really used Shared Projects much. The way I understand them is that they allow you to share code between multiple projects as if it lives in a library but it actually gets compiled directly into the referencing project. They can be thought of like advanced file linking.

    Portable Class Libraries are a bit of a different beast. I've used them in the MonoGame.Extended project with great success, but it wasn't always easy. The way they work is to provide a subset of functionality that works on all target platforms. They compile to a DLL that can be referenced by any of the projects and you can be confident the code will be the same.

    There are a couple of downsides of using a PCL:

    • you can only use a subset of functionality. Surprisingly, this doesn't get in the way too often.
    • if the PCL references another library, it also has to be a PCL.

    The second point is where things get a little tricky with MonoGame. Typically, MonoGame isn't shipped as a PCL, but a PCL version has been published on NuGet that can be used.

    As it stands right now, the NuGet package is a version behind the official release but surprisingly that doesn't really matter because it's a bait and switch PCL which essentially means it just provides the interface to the real DLL that gets used at runtime. In other words, your game will still happily reference version 3.5 even if the PCL is version 3.2 so long as the interface hasn't changed.

    There is some talk about improving this situation in version 3.6.