Deactivate display (multi-display)

Says it in the title but to clarify… When using multiple monitors you can call the Display.displays.Activate() to activate the application on more than one screen. But how do you deactivate a secondary screen without having to restart.
Else is it advisable not to use multi-displays, because I haven’t seen much support for it?

Once activated, you cannot deactivate a display. It’s explained in the documentation.

About if it’s advisable using multi-display… well, sometimes you don’t have another option and it works, but the functions are limited and I think that not everything works as expected. I hope they improve it in the future.

I finally got a workaround but only for Windows.

Since the name of the Secondary Display is always “Unity Secondary Display” I just created a .exe using .Net Framework to find and hide it fully.

The script and the exe can be downloaded here.

Will need to figure out code for linux and mac however.

To apply in runtime just have that .exe file somewhere and use the code:

Process process = new Process();

// Stop the process from opening a new window
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;

// Setup executable and parameters
process.StartInfo.FileName = @Application.streamingAssetsPath + "/" +  "Third Party Functions/Minimise Secondary Display/Windows/Windows-Minimise-Secondary.exe";
//process.StartInfo.Arguments = "--test";

// Go
 process.Start();

In my case I put it into my Streaming Asset Path.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;

public class SetWindowState : MonoBehaviour
{
    public enum ShowStates
    {
        Hide = 0,
        Normal = 1,
        Minimized = 2,
        Maximized = 3,
        ShowNoActivateRecentPosition = 4,
        Show = 5,
        MinimizeActivateNext = 6,
        MinimizeNoActivate = 7,
        ShowNoActivate = 8,
        Restore = 9,
        ShowDefault = 10,
        ForceMinimize = 11
    }
    static int hWnd = 0;
 
    private const int SW_HIDE = 0;
    private const int SW_SHOW = 5;
    [DllImport("User32")]
    private static extern int ShowWindowAsync(int hwnd, int nCmdShow);
 
    [DllImport("user32.dll", EntryPoint = "FindWindowEx", CharSet = CharSet.Auto)]
    static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
 
    [DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
 
 
    public static void Hide()
    {
        hWnd = FindWindow(null, "Unity Secondary Display").ToInt32();
        ShowWindowAsync(hWnd, SW_HIDE);
    }
 
    public static void Show()
    {
        if (hWnd != 0)
        {
            ShowWindowAsync(hWnd, SW_SHOW);
            hWnd = 0;
        }
    }
 
    static List<IntPtr> GetAllChildrenWindowHandles(IntPtr hParent, int maxCount)
    {
        List<IntPtr> result = new List<IntPtr>();
        int ct = 0;
        IntPtr prevChild = IntPtr.Zero;
        IntPtr currChild = IntPtr.Zero;
        while (true && ct < maxCount)
        {
            currChild = FindWindowEx(hParent, prevChild, null, null);
            if (currChild == IntPtr.Zero) break;
            result.Add(currChild);
            prevChild = currChild;
            ++ct;
        }
        return result;
    }
 
}