Extending Unity's Grid

You can hold control while manipulating an object to move it in grid increments. I would like to extend the editor to snap an object back to the grid if you move it while holding down control.

I can’t seem to figure out how to do this.

I thought about making a custom EditorWindow, but those only have their update called while the window is open.

I can add a delegate to the editor’s update using [InitializeOnLoad], but then I can’t detect when control is being held down.

Any ideas?

I don’t really understand why you would want to implement the snapping to grid yourself, as Unity already provides the functionality.

The way you can catch the control-key being pressed in a scene view (or rather, while a scene view is being shown) is by the following code:

using UnityEngine;
using UnityEditor;

public static class ControlDown{

	[MenuItem("NoticeControlDown/Start")]
	public static void InitializeStart(){
		Debug.Log("started");
		SceneView.onSceneGUIDelegate += SceneGUI;
	}

	[MenuItem("NoticeControlDown/Stop")]
	public static void InitializeStop(){
		Debug.Log("stopped");
		SceneView.onSceneGUIDelegate -= SceneGUI;
	}

	private static void SceneGUI(SceneView sceneView){
		if(Event.current.control)
			Debug.Log("Control is down");
	}
}

This script should be located in an Editor folder.