How to get game build to run on Main Display

Technically things seem to work correctly I suppose, canvas/cameras are targeting Display 1, but on my computer that is not my Main Display in Windows, display 2 is.

Every other game on the planet opens on my Main Display, and all builds I’ve ever made in Unity also opened on my Main Display until I upgraded to Unity v2021.

Now all builds open on my secondary monitor, which is not my Main Display, but does show as Display 1 in Windows display settings.

I can change this via shortcuts and can include video settings for players to change it, but is there anyway to have the build work like it did before, or tell unity builds to open on the Main Display regardless of what the display number is?

1 Like

Does this happen on an empty project created in Unity 2021.2 for you? Or only on a project that has been upgraded from a previous Unity version? If you print out monitor names using Unity - Scripting API: Screen.GetDisplayLayout, what order are they reported in?

Today I installed Unity v2021.3.1f1 LTS (cause I wanted to get my project into LTS) and upgraded my older v2020.x LTS project, tested a build once I got through all the upgrade issues and saw the wrong monitor problem.

I have also created multiple builds of test projects with v2021.3.1f1 LTS and see the same problem.

I have just done a new test, new empty project with no changes/other imports, made a Windows 64bit build of the default empty scene and same problem.

Once I shift the EXE to another monitor (using WinKey+SHIFT+ArrowKey) the EXE will then open on the Main Display. Nothing I did in code of course, just Windows 10 doing its thing.

3 Likes

Would you be able to report this as a bug, and include what gets reported by Unity - Scripting API: Screen.GetDisplayLayout on your machine in it? I suspected perhaps we are misinterpreting old saved preferences in upgraded projects, but if an empty project is affected too, we’re probably misdetecting which display is the main display.

Will do, but it does beg the question: How do you tell Unity to use the Main Display instead of whatever is on Display 1?

What if I actually want my game to start on Display 1 even when it isn’t the Main Display?

I don’t see any settings related to this (use Main Display), only the list of Displays for things like Canvas and Cameras.

Unity should by default make your game start on the main display when you run it the first time. Afterwards, it should open up on whatever display it was closed on.

There is no project setting to change that behaviour because when you build a project from the editor, it has no idea what kind of setup the game will run on.

However, there are ways to change it at runtime. There is a C# API to move the game to another display: Unity - Scripting API: Screen.MoveMainWindowTo. We also have command line arguments (“-monitor 3”) to make the game start up on the specified display.

Side note, when making a new unity project, Unity itself opens on my Display 1, not my Main Display.

What exactly did you want from GetDisplayLayout()?

That just gives me a list of my displays in order from Display1, Display2 etc.

The order of the displays it gives you, and how that compares with Windows settings (including which display is set to main display). I suspect it will require us to do some digging in order to reproduce it.

Well it’s giving my displays back in the order Display1, Display2, which I suppose makes sense.

But in my case, Display2 is my Main Display. Are you expecting the order should come back Display2, Display1?

My log info output in order:

Display1
Name: W2340
Height: 1080
Width: 1920
RefreshRate: 148351648/148351648
WorkArea: (x:0, y:0, width:1920, height:1040)

Display2 (Main Display)
Name: B326HUL
Height: 1440
Width: 2560
RefreshRate: 59951/59951
WorkArea: (x:0, y:0, width:2560, height:1400)

8081003--1044911--upload_2022-4-26_17-11-33.png

Bug reported, ticket number: 1423262
Case: 1180011

I’m not sure which order it should return displays in off the top of my head, but that information is going to be very useful when investigating the bug. Thanks for the report!

By the way, when posting bug numbers in the future, only the number part (1423262) is needed. The unique string after that is your unique key that allows you to look at bug report details and that might include your personal info. I edited it out of your post for now, but keep that in mind for the future :).

1 Like

FYI, bug was accepted and can be reproduced, new link to monitor progress: Unity Issue Tracker - Build is launched in the wrong Display when Main Display is changed

This bug affects my project too, and it’s quite a critical issue in my case. Is there a workaround to this until it’s fixed in the 2021 LTS?

1 Like

Not a good one, unfortunately. You could technically detect on startup if it’s the first time you launch the game and you’re not running on the main display, then move the window to it, but that would require using WinAPI directly.

We are in the process of fixing & backporting it. It shouldn’t take much longer.

1 Like

Hey @Tautvydas-Zilys , sorry for nagging, but do know if this issue is still being worked on?
It’s been set as “Fix In Review for 2022.2.X” for quite a while now, and I’m starting to fear it will be another couple of months before this fix is backported into the 2021 LTS.

1 Like

It’s being actively worked on. The fix was ready but then failed an internal test suite so now is being redone. I can’t give you an ETA for getting it done but we’re trying our best to get it in asap.

1 Like

Thank you for letting me know it’s being worked on. I can relax now that I know it’s not in limbo.

Just recently migrated from a 2020 build up to 2021.3.4f1 and had this problem as well. Thanks to this sample https://programtalk.com/vs4/csharp/Unity-Technologies/DesktopSamples/MoveWindowSample/Assets/Scripts/SettingsMenuScript.cs I fixed this using the following code on a script that runs on my splash screen Scene.

    void Awake()
    {
#if !UNITY_EDITOR
        // fixes new Unity2021 bug where the secondary monitor was being chosen when the game launches in a release build
        StartCoroutine("MoveToPrimaryDisplay");
#endif
    }

    IEnumerable MoveToPrimaryDisplay()
    {
        List<DisplayInfo> displays = new List<DisplayInfo>();
        Screen.GetDisplayLayout(displays);
        if (displays?.Count > 0)
        {
            var moveOperation = Screen.MoveMainWindowTo(displays[0], new Vector2Int(displays[0].width / 2, displays[0].height / 2));
            yield return moveOperation;
        }
    }
3 Likes

Hey, I’ve got some technical details to share.

When Unity game starts up, it checks if it’s the first launch by looking at presence of player prefs:

  1. If player prefs are found, then it uses settings from previous run. In other words, if previous run was on a secondary display, so will this one (and this is not a bug).
  2. If player prefs are not found, then we will use the display that Windows happens to spawn it on (we pass CW_USEDEFAULT constant so that Windows decides where to spawn it). This is where the bug seems to occur: for whatever reason, on your machines it gets spawned on non-primary display.

This means that you can only reproduce the bug on the first run, or if you wipe player preferences (which can be found in Windows registry, for instance the path for a new project is “HKEY_CURRENT_USER\SOFTWARE\DefaultCompany\New Unity Project (211)”). We believe we figured out what causes this to happen, but just to make sure we fix the same thing, can you answer a few questions?

  1. Are you launching the game via Build & Run from Unity or are you launching it through Windows Explorer?
  2. When you launch the game, is Windows Explorer window by any chance not on the primary display?

If you need to try these out, please nuke the registry keys before trying each scenario. Otherwise it’ll just use the previous setting.

I deleted the registry keys. Using Build & Run opens it on my main monitor. Opening directly from a Windows Explorer opens it on the screen that contains the window.

I don’t usually open my builds on the secondary monitor, so it’s weird it was set as the select monitor when I ran it for the first time after upgrading to 2021.3