- Home /
drag line in node editor
recently I'm follow a tutorial to create a node editor,the thing is the tutorial is using clicking to connect input/output of the nodes, so I try to make it using dragging to create node connection.
You can see the below code in DrawConnecting()
is trying to draw line when the node input is getting dragged. But unfortunately it didn't work out. I put the Debug.Log()
in DrawConnecting()
it show the function is updating everytime. But it seems the DrawBezier()
is not get called at all.
So I make it more aggressive by testing DrawBezier()
in the OnGUI()
function,now I can see the DrawBezier()
is very laggy,it feels like updated every 2s or so.
Why and what can I do to achieve the effect I want?
private void OnGUI()
{
//other thing
DrawConnecting(Event.current);
Handles.DrawBezier(Vector2.zero,
Event.current.mousePosition,
Vector2.right*50f,
Event.current.mousePosition-Vector2.right*50f,
Color.white,
null,
2f
);
}
private void DrawConnecting(Event e)
{
bool ining = selectedInPoint != null;
bool outing = selectedOutPoint != null;
ConnectPoint p = ining ? selectedInPoint : selectedOutPoint;
Vector2 p_dir = ining ? Vector2.left : Vector2.right;
Vector2 t = e.mousePosition;
// if(ining^outing)
if(e.type==EventType.MouseDrag && (ining||outing))
{
Debug.Log("Dragging at " +p.rect.position);
Handles.DrawBezier(p.rect.center,
t,
p.rect.center + p_dir * 50f,
t - p_dir * 50f,
Color.white,
null,
2f
);
}
}
Answer by Bunny83 · Feb 04, 2019 at 05:07 PM
EditorWindows are not auto-redrawn when a drag event is issued. When you want your window to be redrawn you have to call the Repaint method of your window.
Also note that you should not draw the bezier line when the mouse drag event happens. All drawing is done when the repaint event is handled. So you probably want something like this:
if( ining || outing )
{
if ( e.type==EventType.MouseDrag )
Repaint();
else if( e.type==EventType.Repaint )
{
Handles.DrawBezier(
p.rect.center,
t,
p.rect.center + p_dir * 50f,
t - p_dir * 50f,
Color.white, null, 2f
);
}
}
The "Handles.DrawBezier" method does the event check already inside, that's why it hasn't any effect in your original case since you only call DrawBezier when the event is DragMouse. However the DrawBezier method only draws anything when called during the repaint event. So it will never do anything since the event can't be both at the same time.
Your answer
Follow this Question
Related Questions
Why does the SceneGUI disappear when the component is folded? 0 Answers
Multiple attributes 0 Answers
Why can't I cast Editor.target to my CustomEditor type? 1 Answer
Is there an event being fired off when the Inspector is being resized? 1 Answer
GUI Remove focus from an editor text field when clicking elsewhere. 2 Answers