Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

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

Discussion in 'Scripting' started by LilFire, Mar 13, 2020.

  1. LilFire

    LilFire

    Joined:
    Jul 11, 2017
    Posts:
    74
    upload_2020-3-13_15-26-43.png

    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?
     
  2. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,093
  3. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,606
    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.
     
  4. LilFire

    LilFire

    Joined:
    Jul 11, 2017
    Posts:
    74
    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).
     
  5. danbg

    danbg

    Joined:
    May 1, 2017
    Posts:
    65
  6. danbg

    danbg

    Joined:
    May 1, 2017
    Posts:
    65
    @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 https://github.com/Unity-Technologies/UnityCsReference/blob/master/Editor/Mono/GameView/GameView.cs and I can see targetDisplay, but it seems I'm not accessing it. This is the code he used. Any tips?

    Code (CSharp):
    1.  
    2.         Vector3 RelativeMouseAt()
    3.         {
    4.             var mouseOverWindow = EditorWindow.mouseOverWindow;
    5.             System.Reflection.Assembly assembly = typeof(UnityEditor.EditorWindow).Assembly;
    6.             Type type = assembly.GetType("UnityEditor.GameView");
    7.  
    8.             int displayID = 0;
    9.             if (type.IsInstanceOfType(mouseOverWindow))
    10.             {
    11.                 var displayField = type.GetField("m_TargetDisplay", BindingFlags.NonPublic | BindingFlags.Instance);
    12.                 //displayField is empty
    13.                 displayID = (int)displayField.GetValue(mouseOverWindow);
    14.                 //displayID gives a null error
    15.             }
    16.  
    17.             var pos = Input.mousePosition;
    18.             pos[2] = displayID;
    19.             return pos;
    20.         }
    21.  
     
    dananqqdeqd likes this.
  7. danbg

    danbg

    Joined:
    May 1, 2017
    Posts:
    65
    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:
    Code (CSharp):
    1.  
    2. using UnityEditor;
    3. using System.Reflection;
    4. ...
    5. #if UNITY_EDITOR
    6.         int DisplayTarget()
    7.         {
    8.             var mouseOverWindow = EditorWindow.mouseOverWindow;
    9.             System.Reflection.Assembly assembly = typeof(UnityEditor.EditorWindow).Assembly;
    10.             Type type = assembly.GetType("UnityEditor.PlayModeView");
    11.  
    12.             int displayID = 0;
    13.             if (type.IsInstanceOfType(mouseOverWindow))
    14.             {
    15.                 var displayField = type.GetField("m_TargetDisplay", BindingFlags.NonPublic | BindingFlags.Instance);
    16.                 displayID = (int)displayField.GetValue(mouseOverWindow);
    17.             }
    18.             return displayID;
    19.         }
    20. #endif
    21. ...
    22.  
    If you want a solution to use the UI in multiple displays in editor, I posted the solution based on @karl_jones code in https://forum.unity.com/threads/multi-display-canvases-not-working-in-5-4-2.439429/#post-5988683
     
    Last edited: Jun 16, 2020
    tim12332000 likes this.
  8. LilFire

    LilFire

    Joined:
    Jul 11, 2017
    Posts:
    74
    @danbg Thanks a lot, it works! I'm using Unity 2018 and need to use "UnityEditor.GameView" in this version.
     
  9. danbg

    danbg

    Joined:
    May 1, 2017
    Posts:
    65
    @LilFire glad it works :) 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.
     
  10. AlienFreak

    AlienFreak

    Joined:
    Jul 2, 2012
    Posts:
    40
    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!

    Code (CSharp):
    1.  
    2. using UnityEngine.InputSystem;
    3. using UnityEngine.InputSystem.LowLevel;
    4.  
    5. public static void ForceClickMouseButtonInCenterOfGameWindow()
    6.     {
    7. #if UNITY_EDITOR
    8.         var game = UnityEditor.EditorWindow.GetWindow(typeof(UnityEditor.EditorWindow).Assembly.GetType("UnityEditor.GameView"));
    9.         Vector2 gameWindowCenter = game.rootVisualElement.contentRect.center;
    10.  
    11.         Event leftClickDown = new Event();
    12.         leftClickDown.button = 0;
    13.         leftClickDown.clickCount = 1;
    14.         leftClickDown.type = EventType.MouseDown;
    15.         leftClickDown.mousePosition = gameWindow;
    16.  
    17.         game.SendEvent(leftClickDown);
    18. #endif
    19.     }