- Home /
Draw a handle in editor when key is pressed
Hello, how can i draw a handle in editor when certain key is pressed? I'am currently using this code in OnSceneGUI, but it's not working.
if (Event.current.isKey && Event.current.keyCode == KeyCode.H)
{
Handles.DotCap(0, new Vector3(), Quaternion.identity, 1);
}
Although, this is working fine
if (Event.current.shift)
{
Handles.DotCap(0, new Vector3(), Quaternion.identity, 1);
}
Answer by ocimum · Oct 21, 2015 at 09:37 AM
Before drawing anything, you have to start the Handles
context so that your Handles
methods can be executed.
Handles.BeginGUI();
// Do your drawing stuff
Handles.EndGUI();
If you will have any problems to get access to your mouse events while the Editor
script is enabled try to get the clicks like this:
void OnSceneGUI (){
Event e = Event.current;
int controlID = GUIUtility.GetControlID (FocusType.Passive);
switch (e.GetTypeForControl (controlID)) {
case EventType.MouseDown:
GUIUtility.hotControl = controlID;
CheckForPositions(e.mousePosition);
e.Use ();
break;
case EventType.MouseUp:
GUIUtility.hotControl = 0;
e.Use();
break;
case EventType.MouseDrag:
GUIUtility.hotControl = controlID;
CheckForPositions(e.mousePosition);
e.Use ();
break;
case EventType.KeyDown:
if( e.keyCode == KeyCode.Escape ){
// Do something on pressing Escape
}
if( e.keyCode == KeyCode.Space ){
// Do something on pressing Spcae
}
if( e.keyCode == KeyCode.S ){
// Do something on pressing S
}
break;
}
AllDrawingStuff();
}
And here the drawing
void AllDrawingStuff(){
Handles.BeginGUI();
DrawCircleOnMousePositions();
DrawLine();
Handles.EndGUI();
}
Here is a small tutorial which helped me to understand all the stuff!
Based on Unity's doc, BeginGUI creates a 2D block in a 3D environment. It's used to draw 2D UIs like buttons and labels.. I have the same issue and putting things in BeginGUI block didn't solved this... this might be a bug..
No, it's most likely not a bug, it's most the time not a bug.
You can't draw something during a mouse or key event. You can only draw things during the repaint event. Therefore you have to place all your drawing stuff somewhere, where it's always executed for every event. The drawing methods will automatically only react to the repaint event. If you call a drawing method only during a keydown event it won't do anything.
If you want to enable / disable the drawing of the handle you have to use some sort of flag (boolean, enum, int) which you check before you draw it.
Also keep in $$anonymous$$d that the editor is completely event driven unlike the runtime. If you want the scene to be redrawn you have to request that redraw with SceneView.RepaintAll();
.
Answer by ifdef_ben · Sep 19, 2016 at 04:58 PM
Thanks to Bunny83's comment I was able to figure out the proper way to do this. You have to set a flag outside OnSceneGUI() and update the flag with the KeyDown & KeyUp event. This would do something like this.
[CustomEditor(typeof(Test))]
public class TestInspector : Editor {
private bool bDraw;
void OnSceneGUI (){
if (Event.current.type == EventType.keyDown && Event.current.keyCode == KeyCode.H)
bDraw= true;
else if (Event.current.type == EventType.keyUp && Event.current.keyCode == KeyCode.H)
bDraw= false;
if (bDraw)
Handles.CubeCap(0, Vector3.zero, Quaternion.identity, 1);
}
}