Search Unity

[Editor] is window open(and visible)?

Discussion in 'Scripting' started by BinaryCats, Feb 14, 2020.

  1. BinaryCats

    BinaryCats

    Joined:
    Feb 8, 2016
    Posts:
    317
    Hello,

    Is there a way to know if an EditorWindow (in my case SceneView) is open and visible?

    I can not use
    GetWindow
    as a do not want to open the window
    I can not use
    HasOpenInstances
    as this will return true, if the window is hidden by another tab in the same dock

    Thanks
     
    Neiist and John_Leorid like this.
  2. wileyjerkins

    wileyjerkins

    Joined:
    Oct 13, 2017
    Posts:
    77
    Is this a game? Here is how I tell if an enemy is visible on the screen.
    Code (CSharp):
    1. public static bool IsVisibleToCamera(Camera MainCamera, Transform transform)
    2.     {
    3.         if (MainCamera.enabled != true) { return false; }
    4.         if (transform == null) { return false; }
    5.         Vector3 V = MainCamera.WorldToViewportPoint(transform.position);
    6.         return (V.x >= 0 && V.y >= 0) && (V.x <= 1 && V.y <= 1) && V.z >= 0;
    7.     }
     
  3. BinaryCats

    BinaryCats

    Joined:
    Feb 8, 2016
    Posts:
    317
    No this an editor utility
     
  4. TenaciousDan

    TenaciousDan

    Joined:
    Jul 11, 2019
    Posts:
    31
    This is the top result when searching this problem. As such I will post my (hacky) solution here:

    Code (CSharp):
    1.  
    2. private bool _isDrawingIMGUI = true;
    3.  
    4. protected void OnLostFocus()
    5. {
    6.     _isDrawingIMGUI = false;
    7. }
    8.  
    9. protected void OnGUI()
    10. {
    11.     _isDrawingIMGUI = true;
    12. }
    13.  
    Note that OnGUI() is not called all the time but it is never called automatically when the window is not visible and always called before OnLostFocus(). At least this is what I've gatthered from a few experiments...

    The main issue I'm still having here is that _isDrawingIMGUI is not always trustworthy because the value is set to false until OnGUI() is called. So anything executing in-between (basically after OnFocusLost) is getting potentially false information.
     
    Last edited: Dec 20, 2022
  5. lclemens

    lclemens

    Joined:
    Feb 15, 2020
    Posts:
    761

    Maybe try this?:

    Code (CSharp):
    1.     private void OnBecameVisible()
    2.     {
    3.         m_isVisible = true;
    4.     }
    5.  
    6.     private void OnBecameInvisible()
    7.     {
    8.         m_isVisible = false;
    9.     }
    10.     public void IsVisible() { return m_isVisible; }
    11.  
     
    Fangh likes this.