Is there a way to get the current editor game window display ?

I need to cast rays from the camera related to the display used in the editor and i c’ant find the information, can somebody help me?

It looks like you would have to go through all the cameras in the scene (gather them by using Unity - Scripting API: Camera.allCameras) and for each one check camera.targetDisplay to find out which display number it refers to.

1 Like

This is rather specific. What do you need this for? There may be some better solution depending on why you need to do this. Be aware that whatever you do, the code would only be run while in editor mode. When you build it, you either need to cast from one or all cameras. So i assume it’s for an editor script?

This is the code for the GameView:
https://github.com/Unity-Technologies/UnityCsReference/blob/master/Editor/Mono/GameView/GameView.cs
It seems there is no public property for the currently viewed target display, so you’d have to write an Extension for the class, if that’s possible. Maybe somebody did something like that before.

That information does not help him, unless he can compare it to the selected target display in the GameView. So his question is about how to get that. For which i’m not sure there is an intended solution.

1 Like

Thanks for your answers, i will take a look at extension methods. I’m working on a multi display application, you can see it like many touch screen (using my own native plugin to get inputs). But in the editor I need to handle inputs using the mouse to interact. So in the editor i must know with what I will interact (game window display number).

@LilFire I’m trying that too. @karl_jones did that here: Multi-Display Canvases Not Working in 5.4.2 Although I’m getting an error with his code trying to access “m_TargetDisplay”

@Yoreki , instead of an Extension, @karl_jones from Unity used reflection and modified directly UI package code, it seems that there is no “m_TargetDisplay”, “targetDisplay” or “TargetDisplay” field in UnityEditor.GameView.

I checked the code in UnityCsReference/Editor/Mono/GameView/GameView.cs at master · Unity-Technologies/UnityCsReference · GitHub and I can see targetDisplay, but it seems I’m not accessing it. This is the code he used. Any tips?

        Vector3 RelativeMouseAt()
        {
            var mouseOverWindow = EditorWindow.mouseOverWindow;
            System.Reflection.Assembly assembly = typeof(UnityEditor.EditorWindow).Assembly;
            Type type = assembly.GetType("UnityEditor.GameView");
  
            int displayID = 0;
            if (type.IsInstanceOfType(mouseOverWindow))
            {
                var displayField = type.GetField("m_TargetDisplay", BindingFlags.NonPublic | BindingFlags.Instance);
                //displayField is empty
                displayID = (int)displayField.GetValue(mouseOverWindow);
                //displayID gives a null error
            }
  
            var pos = Input.mousePosition;
            pos[2] = displayID;
            return pos;
        }
1 Like

Ok, I found the problem. I was using “UnityEditor.GameView” instead of “UnityEditor.PlayModeView”, at least in Unity 2019.4.0f1.

You can access the display number with:

using UnityEditor;
using System.Reflection;
...
#if UNITY_EDITOR
        int DisplayTarget()
        {
            var mouseOverWindow = EditorWindow.mouseOverWindow;
            System.Reflection.Assembly assembly = typeof(UnityEditor.EditorWindow).Assembly;
            Type type = assembly.GetType("UnityEditor.PlayModeView");
 
            int displayID = 0;
            if (type.IsInstanceOfType(mouseOverWindow))
            {
                var displayField = type.GetField("m_TargetDisplay", BindingFlags.NonPublic | BindingFlags.Instance);
                displayID = (int)displayField.GetValue(mouseOverWindow);
            }
            return displayID;
        }
#endif
...

If you want a solution to use the UI in multiple displays in editor, I posted the solution based on @karl_jones code in Multi-Display Canvases Not Working in 5.4.2

1 Like

@danbg Thanks a lot, it works! I’m using Unity 2018 and need to use “UnityEditor.GameView” in this version.

@LilFire glad it works :slight_smile: Keep in mind that if you use displays with different resolutions and you want to use UI, probably you’ll need Unity 2020.I had problems with UI using any Unity version behind it.

It’s really just one line of code (plus the two using statements): var game = UnityEditor.EditorWindow.GetWindow(typeof(UnityEditor.EditorWindow).Assembly.GetType(“UnityEditor.GameView”));

Here’s a useful function to force the mouse to click in the center of the game window no matter which display it is using at any time during runtime. Note: this only works in Editor mode!

using UnityEngine.InputSystem;
using UnityEngine.InputSystem.LowLevel;

public static void ForceClickMouseButtonInCenterOfGameWindow()
    {
#if UNITY_EDITOR
        var game = UnityEditor.EditorWindow.GetWindow(typeof(UnityEditor.EditorWindow).Assembly.GetType("UnityEditor.GameView"));
        Vector2 gameWindowCenter = game.rootVisualElement.contentRect.center;

        Event leftClickDown = new Event();
        leftClickDown.button = 0;
        leftClickDown.clickCount = 1;
        leftClickDown.type = EventType.MouseDown;
        leftClickDown.mousePosition = gameWindow;

        game.SendEvent(leftClickDown);
#endif
    }