0
\$\begingroup\$

I have a Gameplayer window running Unity2020.3.32f1.

I'm making an options window to fullscreen the game. I'm trying pull up the list of available monitors to fullscreen the game to. However, when I run the following bit of code, as found on the Unity tutorial regarding Multi-display.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ChangeActiveScreen : MonoBehaviour
{

    public int currentMonitor;

    private List<monitorItem> monitors = new List<monitorItem>();

    public Text monitorsLabel;

    // Start is called before the first frame update
    void Start()
    {
        // Obtain a list of possible monitor targets:
        //monitors = Display.displays.Select(t => new monitorItem { monitor = t.ind }).ToList();

        for (int i = 0; i < Display.displays.Length; i++)
        {
           Display.displays[i].Activate();
           monitors.Add(new monitorItem { monitor = i });
        }

        //resolutions = Screen.resolutions.Select(t => new resItem { horizontal = t.width, vertical = t.height }).ToList();

        // Which monitor is activate? Set it to that monitor?
        for (int i = 0; i < Display.displays.Length; i++)
        {
            if (Display.displays[i].active) currentMonitor = i;
        }

        UpdateMonitorLabel();
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void ChangeDisplay(Camera camera)
    {
        camera.targetDisplay = monitors[currentMonitor].monitor;

    }

    public void MonitorLeft()
    {
        currentMonitor--;

        if (currentMonitor < 0) currentMonitor = Display.displays.Length - 1;

        UpdateMonitorLabel();
    }

    public void MonitorRight()
    {
        currentMonitor++;

        if (currentMonitor > Display.displays.Length - 1) currentMonitor = 0;

        UpdateMonitorLabel();
    }

    public void UpdateMonitorLabel()
    {
        monitorsLabel.text = "Display # " + (currentMonitor + 1).ToString();
    }
}

public class monitorItem
{
    public int monitor;
}

The 2nd monitor does "activate" but it comes up as a black fullscreen which is not my intention. I only want to move the game player window into the second monitor and fullscreen the game into the second display. NOT stretch the one window into multiple monitors.

I've looked into other functions but they seem to rely on the resolution and the toggle of fullscreen. Nothing of the sort regarding screen location and so on.

I know that you can toggle the fullscreen option off, drag and move it into the second window and fullscreen the game and it will apply there. I'm more or less looking for a way to do this programmatically. (Additionally, running this code seems to do something with the unfullscreen title bar and not given an option to drag it.)

\$\endgroup\$
3
  • \$\begingroup\$ So, what about your scene's camera? did you change it to display 2? \$\endgroup\$
    – Arian_ki
    Apr 7, 2022 at 3:30
  • \$\begingroup\$ @ArianKeshvari Changing the camera to camera.display = 2 still send me to a blue screen. (Not a Windows screen, but a window filled with blue.) \$\endgroup\$ Apr 7, 2022 at 4:04
  • \$\begingroup\$ Also @ArianKeshvari Updated the question with my full script instead of just the tutorial. This script is attached onto the camera. \$\endgroup\$ Apr 7, 2022 at 4:06

1 Answer 1

1
\$\begingroup\$

Looking at Unity's tutorial, one sentence caught my eyes:

Once you activate a display, you can’t deactivate it.

And here's the part of your code the problem is:

for (int i = 0; i < Display.displays.Length; i++)
{
    if (Display.displays[i].active) currentMonitor = i;
}

And according to what unity said, currentMonitor will be always 1 So your camera's target will never be the second monitor.

What you can do is use a drop-down list in your game/app settings and let the player change the current monitor manually or if you know that you're going to always use the first two monitors then you can use a switch-like method every time you want to go between monitors:

void FlipCamera()
{
    camera.targetDisplay = (currentMonitor + 1) % 2;
}

or a more functional one:

void FlipCamera(int num)
{
    if (currentMonitor + num < 0)
        num = -currentMonitor;
    camera.targetDisplay = (currentMonitor + num) % Display.displays.Length; // num can be negative too
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .