How can I check if a key is down in scene view?

Hi,
I needed to automate placing bits of terrain in my game in editor. For this purpose, I have created a script presented below. It works perfectly in play mode, but it’s supposed to work specifically in edit mode.

using System.Collections;
using UnityEngine;
  [ExecuteInEditMode]
  [InitializeOnLoad]
  public class placeTerrainBits : MonoBehaviour {

    private placeTerrainBits script;

    void OnEnable() {
        script = gameObject.GetComponent<placeTerrainBits>();
		EditorApplication.update += Update;
    }

    void Update() {
		Debug.Log ("Updating");
        if (Input.GetMouseButtonDown(0) && Input.GetKey(KeyCode.LeftControl)) {
            Place();
        }
    }
    void Place() {
      // irrelevant code
    }
}

Unfortunately, this script seems not to be “listening” for keyboard input in scene view. The Update method is working fine, as confirmed by a quick test, but the script seems to fail at the input-checking stage. Is there any way I can actually make the script to check for input in scene view?
Thanks

This is wrong on so many levels ^^.

  • First of all MonoBehaviours are runtime scripts and not editor scripts.
  • ExecuteInEditMode is not meant to implement editing features but just to enable runtime features during edit mode. An example is the particlesystem which is still updated even the game is not playing.
  • InitializedOnLoad only triggers the static constructor and doesn’t work for instance classes.
  • EditorApplication is an editor class which can not be used inside a runtime class.
  • You should never directly call a callback of Unity. You subscribe the Update callback to the update delegate of the editor. You should always use a seperate method which you subscribe to such a delegate.
  • The Input class only works at runtime. It can’t be used at edit time.
  • The update delegate is a generic update which has no relation to the sceneview.

What you should do is creating either a normal class and using InitializedOnLoad and use the static constructor of the class to subscribe your delegate and / or initialize a singleton instance of your class. Or create an EditorWindow in which you can subscribe inside OnEnable (and unsubscribe in OnDisable)

If you want to handle key or mouse events in the scene view you should subscribe a method to SceneView.onSceneGUIDelegate. It works like OnGUI. It allows you to handle any sort of event using the Event class including mouse, keyboard and repaint events.

Keep in mind that InitializeOnLoad can’t be “disabled”. It will initialize the object when Unity opens the project. It’s usually better to create an EditorWindow or a CustomEditor (inspector for a runtime script) to implement any additional editing functions. It allows to easily enable / disable that functionality.

To actually check for a mouse down even while the control key is held down you would do

// inside OnSceneGUI / onSceneGUIDelegate callback
Event e = Event.current;
if (e.control && e.type == EventType.MouseDown && e.button = 0)
{
    Debug.Log("click");
}