OnSceneGUI is only called if an object by the given type is selected in the editor. The given type is denoted by the C# Attribute CutomEditor. The below example is almost there as it is called when any type of object is selected.
I want to get OnSceneGUI() called at all times even when no objects are selected. Anyone got an idea on how to make that work?
using UnityEditor;
using UnityEngine;
using System.Collections;
[CustomEditor(typeof(GameObject))]
public class SceneGUITest : Editor
{
void OnSceneGUI()
{
Debug.Log("OnSceneGUI with any object selected");
}
}
using UnityEditor;
using UnityEngine;
public class TestWindow : EditorWindow
{
[MenuItem("Window/" + "Test")]
public static void Init()
{
// Get existing open window or if none, make a new one:
TestWindow window = GetWindow<TestWindow>();
window.title = "Test";
SceneView.onSceneGUIDelegate += OnScene;
}
private static void OnScene(SceneView sceneview)
{
Debug.Log("This event opens up so many possibilities.");
}
public void OnDestroy()
{
SceneView.onSceneGUIDelegate -= OnScene;
}
}
Skjalg is correct. However, when Play mode is entered and then returned to Editor mode, OnScene() will not be called anymore. Furthermore, any other initialization code that is placed in Init() will be lost as well when you go from Play mode back to Editor mode. To solve this problem, use this:
using UnityEditor;
using UnityEngine;
public class TestWindow : EditorWindow
{
[MenuItem("Window/" + "Test")]
public static void Init()
{
// Get existing open window or if none, make a new one:
TestWindow window = GetWindow<TestWindow>();
window.title = "Test";
// SceneView.onSceneGUIDelegate += OnScene;
}
private static void OnScene(SceneView sceneview)
{
Debug.Log("This event opens up so many possibilities.");
}
public void OnDestroy()
{
SceneView.onSceneGUIDelegate -= OnScene;
}
public void Update()
{
if(SceneView.onSceneGUIDelegate == null){
SceneView.onSceneGUIDelegate += OnScene;
//Any other initialization code you would
//normally place in Init() goes here instead.
}
}
}