Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Listen for key input from editor script

Discussion in 'Input System' started by bluepine810, Mar 11, 2021.

  1. bluepine810

    bluepine810

    Joined:
    May 1, 2019
    Posts:
    2
    I use 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? :

    Code (CSharp):
    1. [CustomEditor (typeof (Switcher))]
    2. public class SiwtcherEditor : Editor {
    3.     void OnSceneGUI () {
    4.         Switcher script = (Switcher) target;
    5.         Event e = Event.current;
    6.         switch (e.type) {
    7.             case EventType.KeyDown:
    8.                 {
    9.                     if (Event.current.keyCode == (KeyCode.A)) {
    10.                         Debug.Log ("A pressed");
    11.  
    12.                         script.SwitchHere();
    13.                         e.Use ();
    14.                     }
    15.                     break;
    16.                 }
    17.         }
    18.     }
    19. }
    20. public class Switcher : MonoBehaviour {
    21.     public void SwitchHere () {
    22.         Debug.Log ("GotINPUT");
    23.     }
    24. }
     
  2. Putcho

    Putcho

    Joined:
    Jun 1, 2013
    Posts:
    246
    Is your Editor script inside Assets/Editor folder?
     
  3. bluepine810

    bluepine810

    Joined:
    May 1, 2019
    Posts:
    2
    Yes, of course, Editor script is inside Assets/Editor folder.
     
  4. Putcho

    Putcho

    Joined:
    Jun 1, 2013
    Posts:
    246
    its work only if you actively selected on the scene, if you select from Hierarchy/Inspector or other windows it will not run

    select the object on the scene and press "A" you should see the log