EditorWindow OnBecameVisible and OnBecameInvisible

OnBecameVisible and OnBecameInvisible are in the Unity docs as methods of the Renderer and MonoBehaviour classes. It turns out that they work for EditorWindows, too!

They get called when a docked window changes visibility; meaning that when a docked window is hidden by a sibling window within that dock, OnBecameInvisible gets called. Similarly, when the window becomes visible again, of course, OnBecameVisible is called.

using UnityEditor;
using UnityEngine;


public class OnBecameVisibleEditorWindow : EditorWindow
{
	private void OnBecameVisible()
	{
		Debug.Log("Window is visible");
	}

	private void OnBecameInvisible()
	{
		Debug.Log("Window is invisible");
	}


	[MenuItem("Tools/OnBecameVisible Window")]
	public static void Init_ToolsMenu()
	{
		EditorWindow window = EditorWindow.GetWindow<OnBecameVisibleEditorWindow>();
		window.title = "OnBecameVisible";
		window.Show();
	}
}

There is a little gotcha, though. It seems that both methods get called when docking and undocking the window. Not sure why, really. It’s just another Unity quirk!

Leave a Reply