How to tell if the Scene View (as opposed to Game View) is active in the editor?

I recently discovered that WaitForEndOfFrame does not function properly when the Scene View is active, which has caused a lot of issues in my testing that wouldn’t have happened under normal circumstances.

As a result, I’ve been looking for some way to determine in my code whether Scene View is currently active instead of the Game View, but I haven’t been able to figure out how to achieve this. Any ideas?

You can use the SceneView class, here is a simple example:

using System.Collections;
using UnityEditor;
using UnityEngine;
//using System.Linq;
#if UNITY_EDITOR
[ExecuteInEditMode]
public class SceneViewChecker : MonoBehaviour
{
  private void OnEnable()
  {
    EditorWindow.windowFocusChanged += OnWindowFocusChanged;
  }

  private void OnDisable()
  {
    EditorWindow.windowFocusChanged -= OnWindowFocusChanged;
  }
  void OnWindowFocusChanged()
  {
    StartCoroutine(OnFocusChanged());
  }
  // We have to wait at least one frame for windows that get docked to be recognized as focused.
  IEnumerator OnFocusChanged()
  {
    yield return null;
    //bool anyActive = SceneView.sceneViews.Cast<SceneView>().Any(window => window.hasFocus); // Focus check in LINQ
    bool anyActive = false;
    foreach (SceneView window in SceneView.sceneViews)
    {
      if (window.hasFocus)
      {
        anyActive = true;
        if (!window.docked)
        {
          // you could allow in undocked windows for debugging.
        }
        break;
      }
    }
    // For example, control a shader parameter
    Shader.SetGlobalFloat("_SceneViewActive", anyActive ? 1 : 0);
  }
}
#endif

What exactly do you mean by active? Having keyboard focus or being visible? Keep in mind you can have more than one sceneview / gameview open at the same time. You can also undock the gameview so it’s always visible. The Unity Editor consists of several seperate EditorWindows and the Game / Scene view are just one type of EditorWindows. The Console, Inspector, Animation Window, … are also EditorWindows which could be active / have keyboard focus. So maybe you should just make sure your gameview is visible at all times. You can undock or dock the windows any way you link.