- Home /
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?
All runtime classes work in the editor too. You can use Input
anywhere, so detecting when control is being held down is easy.
Editor Extensions are quite limited, I suggest you use a $$anonymous$$enu item as a shortcut for snapping the object to the nearest grid relative to the world origin, ins$$anonymous$$d of trying to replace current editor functionality.
Answer by Jamora · Jan 15, 2014 at 02:07 PM
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.
Thanks that worked.
The reason I want to make grid snapping myself is because it's an inconvenient menu item in unity. If something is off grid (say at 2.1 when you have a grid with spacing of 1) holding control and moving the object will move the object to 1.1 or 3.1. We want it to get back on the grid.