Devlog: Borderless Windowed Mode

Sometime last week I took on the task of figuring out how to get the Unity3D Windows Standalone Player to play The Wild Eternal in a fullscreen borderless window. The only solution I could find was one that required adding a command line argument to the executable. Not exactly user-friendly…

Below is my solution to this problem, which began as a simple fullscreen borderless window toggle but became a general purpose resolution controller. The system remains fairly simple, but it can toggle display modes, resolutions and control the position of the window carefully (even when borderless). It will also guess at the best display settings for a user, and save most of their settings (though you will likely wish to integrate into your own save system).

Resolution Controller Demo
The test build. I expect others will use their own GUI, but this example provides a useful reference for interfacing with the ResolutionController.

How It Works

I found that I needed to use the windows API (user32.dll) to do this, and unfortunately ran into a few problems. I resolved them, but the resolution required deviating from some statements in the windows API’s documentation (scary!)! Thanks to a handful of volunteers, I have tested this algorithm on ~30 unique Windows machines spanning 32-bit and 64-bit Windows 7, and Windows 8. Of the 30-some tests, 100% had identical results. Comforting. The details of what is scary is outlined very carefully in the code in the WindowsHandler.TrySetDisplay method.

Requirements

This is repeated at the header of the ResolutionController.cs script in the shared code, so don’t feel a need to commit it to memory immediately.

For exclusive fullscreen mode, DirectX 9 games must put their Project’s D3D9 Fullscreen Mode in ExclusiveMode, not FullscreenWindow. DirectX 11 games cannot access exclusive fullscreen mode at all due to how it is implemented in Unity. This is likely not a problem as exclusive mode is only necessary for small GPU-related performance considerations, which most DirectX 11 games probably view as negligible. For those who don’t know, as I didn’t, exclusive mode is a mode where the rest of the windows explorer is not rendered (on that display), which can reduce GPU and CPU loads.

I am more than happy to answer any questions in the comments section below. Please let me know of any unexpected behavior or bugs.

Downloads

10 thoughts on “Devlog: Borderless Windowed Mode”

  1. I was just looking into this exact problem! The best Unity has to offer here is -popupwindow command line argument, which is a fragile solution at best. Even using that, if you want to support Fullscreen, Windowed (Fullscreen), and Windowed, you have to figure out how to add and remove the window chrome yourself. I’m not really sure why Unity wouldn’t have added this themselves, but then I’ve found other things missing that leave me scratching my head, too. Thanks for sharing this! I definitely appreciate the research time it will save me.

  2. Thanks for this code. We’ve integrated it in our game for more than 4 months ago and there are hundreds of people who are using and enjoy borderless mode.

    I just noticed that on Windows 10 to achieve full borderless mode you need to disable the “thick frame”. I mean you need to use
    Flags.Unset(ref flags, WS_THICKFRAME);
    together with
    Flags.Unset(ref flags, WS_CAPTION);

    Regards!

    1. Thank you for the follow-up information about Windows 10, I really appreciate that. Do you know if it should only be executed on Windows 10 platforms, or is it backwards compatible with Windows 7 (and others)?

  3. This looks really good! I’m thinking about using this for future games. Have you you heard about any issues people have had with it? Also, thanks Vladimir for the windows 10 update 🙂

    1. When I enable the “Auto Graphics API for Windows” checkbox in the PlayerSettings and fiddle with the DisplayModes / resolutions in a build, weird things start to happen.

      1. In a non-native, smaller resolution, going from fullscreen –> windowed, the screen stays black until you switch to another resolution. (updating the window again)
      2. Going from a non-native, smaller resolution in displaymode windowed/borderless —> seems to scale the window/viewport incorrectly. (Camera only shows small part of screen, cursor gets offset that gets greater as you move it down the screen)

      Screenshot for reference:
      http://imgur.com/rBb7uVt

      I’m confused as to why this would be the case, and the only possible conclusion I’ve been able to reach so far is that Unity does something weird when you turn it on. To ensure it was not due to a specific Direct3D version, I’ve tried all 3 options seperatly (and together) but they all function like they should. I’ve looked through the some of the microsoft docs & ResolutionController script, trying to fiddle with it. So far I haven’t had any success in spotting/solving the issue.

      Do you have any idea what could be causing this?

      1. Hello!
        We had the same issue. It was caused by a bug in Unity engine. I need to say that Unity 5.2.0+ is full of small issues and Unity team with every minor version break something regarding the screen mode switching (and there are many other problems too). Good news is that the latest 5.2.2p4 patch release seems to resolve these issues (I didn’t use DX12, but for DX9 and DX11 it works fine). A few days ago Unity 5.2.3 was released, but we didn’t try it yet, but it also includes a fix from 5.2.2 patches regarding the screen mode switching issue:
        “Fixed render being positioned and sized incorrectly in D3D9 and D3D11 with Fullscreen Window mode.”

        Our game VoidExpanse has been played by more than 20k players and we never heard any complains about the borderless mode – it works just perfect – until Windows 10 came out (my fix described above) and we’ve upgraded to Unity 5.2.0.

        Regards!

        1. I’ve just confirmed it’s also fixed for 5.2.3f1 as expected, thanks for the fast response.

          ps. your game looks pretty cool! Good to hear that this has been used in the wild. It will probably help with me pitching it to my colleagues 🙂

  4. Unity now supports Exclusive Fullscreen in D3D 11. Unfortunately your game will probably crash if it loses focus.

    This demo — which works great in D3D 9 — has the same problem as our game in D3D 11, it will generally become unresponsive and have to be killed from the Task Manager. /huge sigh/

Comments are closed.