This is the 9th day of my participation in the August More Text Challenge. For details, see:August is more challenging

An overview of the

In daily development, we often encounter multi-screen development, which is called split screen. This article focuses on how to use the split screen feature in Unity and how to get the current mouse click on which screen.

Function implementation

There are many ways to use the split screen function in Unity, which needs to be analyzed according to the specific requirements of the project. This article mainly uses the multi-camera split screen, which is divided into five screens.

Split screen scene construction

Create five cameras and five Canvas in Unity

Then change the camera and Canvas parameters. In this case, change the Target Display. Only if change the Display to the corresponding Display, the content on the Canvas will be displayed in the camera screen of the UI

The next step is to change the Dispay in the Game view. Only if the Display in the Game view is also changed can the corresponding camera screen be displayed

The Display,

You can use multiple monitors to display up to eight different application views on up to eight different monitors at the same time. It can be used in Settings such as PC games, arcade consoles or public displays. Unity supports multiple monitors:

  • Standalone platforms (Windows, macOS X, and Linux)
  • Android (OpenGL ES and Vulkan)
  • IOS

UnityDisplay split screen Currently supports a maximum of 8 screens

The scene setup has been written above, but the scene setup is not complete, you need to activate the screen before you can use it, because Unity’s default display mode is only one display, so you need to use it when running the applicationDisplay.Activate()Explicitly activate other displays.

Once the display is activated, it cannot be disabled.

for (int i = 0; i < Display.displays.Length; i++)
{
    Display.displays[i].Activate();
    Screen.SetResolution(Display.displays[i].renderingWidth, Display.displays[i].renderingHeight, true);
}
Copy the code

See the Light emitting API for more information on Display methods

Get screen number

One thing to note here is the display.displays.Length attribute. Under Editor, the value of display.displays.Length is 1 regardless of how many monitors are connected to your host. The return value is the actual number of connected monitors only when the package is run. The main idea of how we get the screen number is. It is obtained by the position of the mouse on the screen and then divided by the width of the resolution. The downside is that in Editor mode, it’s not quite the same as in packaged mode. We use the mouse input. mousePosition, which takes the bottom left corner of the screen as (0,0) and the top right corner as the vertex. The mouse range should be (0,0) – (9600,1080) when packaged. In this case, my computer screen has a resolution of 1920*1080, so the maximum coordinate of the mouse between the five screens is (9600,1080). Well, in this case, it’s easy to calculate the screen number, so the x axis of the mouse/the width of the individual screen +1 is the current screen number, but sometimes there are decimals, so we’re going to round down. The following code

sceneNumber = (int)Mathf.Floor(mousePosition.x / (float)SceneWidth) + 1;
Copy the code

So how to get the position of the mouse on the screen? In fact, it is very simple, as long as you know the number of the screen, the horizontal coordinate of the mouse minus the corresponding screen width.

Vector3 pos =new Vector3(mousePosition.x- (SceneWidth*(sceneNumber-1)), mousePosition.y, mousePosition.z);
Copy the code

To the pit

It is very easy to get the screen number in the packaging state above, but how about in the Editor? Is it the same? The answer is no… When you use the above code you will notice that the screen number is not necessarily a negative number. That’s because, in the editor state, the default screen (Game view) is the main screen, and the mouse position is (0,0) in the lower left corner of the screen and (1920,1080) in the upper right corner, so the excess is either positive or extremely large. For example, the following picture, the mouse position has been more than negative 1000, obviously wrong

Through test, found that when the mouse point on which screen, which will not be the default screen main screen, and when we click the mouse when want to calculate, you will find, the location of the mouse is always the current position, through the discovery, when we click the mouse, the program will in main screen click on at the same time to change the current screen, We tested the following code in the Update and found that the mouse position had changed before the mouse click and tap broke execution.

Debug.Log(Input.mousePosition); If (input.getMouseButtonDown (0)) {debug.log (" mouse click "); }Copy the code

So we couldn’t pack every test, so we worked on a solution

  1. When run, first click on screen one to determine screen one
  2. Create private float[] positions = {1, 1}; Array that stores the last mouse’s horizontal axis coordinates and the current horizontal axis coordinates
  3. Calculates the current screen number from the last clicked screen number and the mouse position

First identify screen one

if (Input.GetMouseButtonDown(0))
{
    if (!isSureScene1)
    {
        isSureScene1 = true;
        sceneNumber = 1;
    }
}
Copy the code

Then record the mouse position, last and current

// Positions [0] = positions[1]; // Positions [0] = positions[1]; positions[1] = Input.mousePosition.x;Copy the code

And then calculating the screen number, which is a little bit more complicated than packaging, but only a little bit, just becomes adding the calculated screen number to the current screen number

/ / / < summary > / / / screen by mouse position calculation, / / / < summary > / / / < returns > < / returns > void GetSceneNumberFromMousePosition () { sceneNumber += (int)Mathf.Floor(positions[0] / (float)SceneWidth); }Copy the code

This is a perfect solution to the problem of not being able to get the screen number in the editor state. You can get the camera directly from the screen number, and then you can use it to emit radiation and other functions. The effect is as follows:

conclusion

This method is not very perfect, but in Unity this use is enough, the disadvantage is that the resolution is required, the resolution should be the same, the second is, in the Editor, the game view to ensure the size of the same, and do not put, as the above picture, otherwise it will be calculated wrong.

Write in the last

All the shared content are the author in the daily development process used a variety of small function points, share out also in a disguised review, if there is a bad place to write also please give advice. The source code of the Demo will be sorted out and shared later. Welcome to learn from each other and make progress.