3.5 cross platform game, how to find OS?

Sorry if this is noobish, but I’ve searched a bit and can’t find any answers.

Is there a way to find out what OS the game is currently running on? For instance, one thing I’d like to do is default to a borderless window in Windows, but go fullscreen on OSX by default.

If (someAwesomeClass.isWindows)
{
    doThisWindowsThing();
} 
else if (someAwesomeClass.isOSX)
{
    doThisOSXThing();
}

That typically is done making different builds for different platforms (the #if WINDOWS compiler switches).
And that is typically aided by using protobuild.
That tool creates and/or builds different project-files for different setups.
You may import different files for different platforms, etc… by defining that in XML files.

There is a PR on making some MG internal classes public that provide that functionality: https://github.com/mono/MonoGame/pull/4585

For now you can do:

var pid = Environment.OSVersion.Platform;

switch (pid) {
    case PlatformID.Win32NT:
    case PlatformID.Win32S:
    case PlatformID.Win32Windows:
    case PlatformID.WinCE:
       // Windows Platform
        break;
    case PlatformID.MacOSX:
        // MacOS Platform
        break;
    case PlatformID.Unix:
        var buf = IntPtr.Zero;
        try {
            buf = Marshal.AllocHGlobal (8192);

            if (uname (buf) == 0) {
                var sos = Marshal.PtrToStringAnsi (buf);

                if (sos == "Darwin") {
                    // MacOS Platform
                    return;
                }
            }
        } catch {
        } finally {
            if (buf != IntPtr.Zero)
                Marshal.FreeHGlobal (buf);
        }

        // Linux Platform
        break;
    default:
        // Unknown Platform
        break;
}

Thanks! I’ll give that a shot. :slightly_smiling:

I see that we can now use PlatformInfo.MonoGamePlatform to grab the platform, which is awesome, but if your platform is DesktopGL, how do you differentiate between windows, macOS, and linux?

Is the answer above still the recommended approach?

Environment.OSVersion.Platform is part of dotnet in general (not MonoGame specific), and is one of the recommended ways of determining the operating system.

There are a couple of other ways in dotnet to do this as well.

RuntimeInformation Class

There is the System.Runtime.InteropServices.RuntimeInformation class that has the IsOsPlatform(OsPlatform) method that can be used to determine the operating system.

Example

//  Add namespace
using System.Runtime.InteropServices;

// Then in your code....
if(RuntimeInformation.IsOsPlatform(OsPlatform.Windows)
{
    //  Windows stuff
}
else if(RunTimeInformation.IsOsPlatform(OsPlatform.Linux)
{
   // Linux stuff
}
// ... etc

OperatingSystem Class

There is also the System.OperatingSystem class which has static methods to check for operating system.

Example

if(OperatingSystem.IsWindows())
{
    // Do windows stuff
}
else if (OperatingSystem.IsLinux())
{
    // Do Linux Stuff
}
// ... etc
1 Like

This actually returns Unix even for MacOS systems (instead of MacOSX). I prefer to use RuntimeInformation.IsOsPlatform.