I am using an editor script placed in editor folder, which listens to a key pressed. When the key is pressed, it is supposed to trigger a function from MonoBehavior script. The issue is that the function does not get triggered at all. I am even adding EventSystem in my inspector. What is the problem here? :
[CustomEditor (typeof (Switcher))]
public class SiwtcherEditor : Editor {
void OnSceneGUI () {
Switcher script = (Switcher) target;
Event e = Event.current;
switch (e.type) {
case EventType.KeyDown:
{
if (Event.current.keyCode == (KeyCode.A)) {
Debug.Log ("A pressed");
script.SwitchHere();
e.Use ();
}
break;
}
}
}
}
public class Switcher : MonoBehaviour {
public void SwitchHere () {
Debug.Log ("GotINPUT");
}
}
If someone comes across this problem, the issue was that OnSceneGUI() was not getting called. So the problem is resolved by OnEnable() function as given below:
[CustomEditor (typeof (Switcher))]
public class SiwtcherEditor : Editor {
void OnSceneGUI (SceneView sceneView) {
SceneView.RepaintAll ();
Switcher script = (Switcher) target;
Event e = Event.current;
switch (e.type) {
case EventType.KeyDown:
{
if (Event.current.keyCode == (KeyCode.A)) {
script.SwitchHere();
e.Use ();
}
break;
}
}
}
void OnEnable () {
SceneView.duringSceneGui += OnSceneGUI;
}
void OnDisable () {
SceneView.duringSceneGui -= OnSceneGUI;
}
}