Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

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:
    62
    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:
    968
  3. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    1,654
    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:
    62
    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:
    63
  6. danbg

    danbg

    Joined:
    May 1, 2017
    Posts:
    63
    @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.  
     
  7. danbg

    danbg

    Joined:
    May 1, 2017
    Posts:
    63
    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
  8. LilFire

    LilFire

    Joined:
    Jul 11, 2017
    Posts:
    62
    @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:
    63
    @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.
     
unityunity