想熟悉XNode的可以移步:XNode XNode简单使用
- XNode的连线,是在检测用户的鼠标点击事件,判定并检测有没有输入输出接口,那么问题来了,总有一些要求,是需要自己去实现的,在各大博客中寻找对应的解决办法无果,只能自己看下源码
- 于是在 NodeEditorAction中找到对应的源码如下
NodeEditorAction :
public void Controls()
{
.....
case EventType.MouseUp:
if (e.button == 0)
{
//Port drag release
if (IsDraggingPort)
{
// If connection is valid, save it
if (draggedOutputTarget != null && draggedOutput.CanConnectTo(draggedOutputTarget)) {
XNode.Node node = draggedOutputTarget.node;
if (graph.nodes.Count != 0) draggedOutput.Connect(draggedOutputTarget);
// ConnectionIndex can be -1 if the connection is removed instantly after creation
int connectionIndex = draggedOutput.GetConnectionIndex(draggedOutputTarget);
if (connectionIndex != -1) {
draggedOutput.GetReroutePoints(connectionIndex).AddRange(draggedOutputReroutes);
if (NodeEditor.onUpdateNode != null) NodeEditor.onUpdateNode(node);
EditorUtility.SetDirty(graph);
}
}
// Open context menu for auto-connection if there is no target node
else if (draggedOutputTarget == null && NodeEditorPreferences.GetSettings().dragToCreate && autoConnectOutput != null) {
GenericMenu menu = new GenericMenu();
graphEditor.AddContextMenuItems(menu);
menu.DropDown(new Rect(Event.current.mousePosition, Vector2.zero));
}
//Release dragged connection
draggedOutput = null;
draggedOutputTarget = null;
EditorUtility.SetDirty(graph);
if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets();
}
.......
}
.....
}
上述代码中可以看到,当EventType = Up(鼠标抬起时),且在拖拽对应Port时,dragOutput.Connect(draggedOutputTarget)
所以这里只需要去获取对应的Port即可实现
NodePort curInputPort = node.GetInputPort("Input");
NodePort preOutputPort = preNode.GetOutputPort("Output");
preOutputPort.Connect(curInputPort);
curInputPort.Connect(preOutputPort);