OnEnable and OnFocus in EditorWindow executed after Play?

I have an EditorWindow that calls OnEnable() and OnFocus() functions when i hit the button. How do i prevent it from happening?

Specifically, if i have an editor window specified by the code below open than it will print “Focus Gained” in the console immediately after the Play button is hit.

using UnityEditor;
using UnityEngine;
using System.Collections;

public class BoxEdTest : EditorWindow {
	[MenuItem ("Box Editor/BoxEd Dev Test")]
	public static void ShowWindow () {
		EditorWindow.GetWindow (typeof(BoxEdTest));
	}

	[ExecuteInEditMode]
	public void OnFocus() {
		SubFunction();
	}
	[ExecuteInEditMode]
	public void SubFunction() {
		Debug.Log ("focus gained");
	}

	public void OnLostFocus() {
		Debug.Log ("focus lost");
	}
}

Came up with my own answer for OnEnable(). It is ugly, but it looks like this:

private bool isEnabled = false;
public void OnEnable() {
if (!isEnabled) {
isEnabled = true;
DoStuff();
}
}

This basically ensures OnEnable is only ran when the EditorWindow is first displayed and never again.

Regarding OnFocus, still haven’t found a good way to prevent it from running after “play” is hit. Using a boolean doesn’t work because you want OnFocus to run every time a window regains focus, and also because hitting the “play” button does actually make the window lose focus before calling OnFocus in playmode.

Try this:

private void OnFocus()
{
    if(focusedWindow == this)
    {
        DoThings();
    }
}