- Home /
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");
}
}
Answer by uniMaxi · May 08, 2014 at 11:42 AM
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.
Answer by mikeburnsshadow · Aug 03, 2017 at 08:27 AM
Try this:
private void OnFocus()
{
if(focusedWindow == this)
{
DoThings();
}
}
Your answer
Follow this Question
Related Questions
Texturing Marching Cubes terrain when digging 0 Answers
Troubleshooting the spawn mechanic in a puzzle game 1 Answer
How do I force the player to press buttons in order? 2 Answers
When to use the inspector vs finding an object reference via script? 0 Answers
Is there a way to see Unity's code for their functions in Visual Studio? 1 Answer