实现节点连线
打开StoryGraphView.cs,新增以下方法:
C#
public override List GetCompatiblePorts(Port startPort, NodeAdapter nodeAdapter)
{
// 获取所有端口
List result = ports.ToList();
// 执行筛选
result = result.Where
(
// 两个端口的逻辑方向不能相同(即数据流向是输入还是输出)
// 两个端口不能为同一个端口
endport => endport.direction != startPort.direction && endport.node != startPort.node
).ToList();
return result;
}
删除节点连线
选中连线后直接点击键盘Delete
键或鼠标右键删除即可
更新连接信息
- 打开StoryGraphView.cs,新增以下方法:
C#
// 当视图发生变化
private void OnGraphViewChanged()
{
// 定义事件
graphViewChanged = (changes) =>
{
// 当有连线即将创建时
if (changes.edgesToCreate != null)
{
// 遍历所有即将创建的连线,同步相应数据
foreach (Edge edge in changes.edgesToCreate)
{
// 获取连线输入端的节点(下一个节点)
BaseNode nextNode = (BaseNode)edge.input.node;
// 获取连线输出端的节点(上一个节点)引用的选项数据
ChoiceData choiceData = (ChoiceData)edge.output.userData;
// 记录下一个节点的GUID
choiceData.NextNodeID = nextNode.GUID;
}
}
// 当有元素即将删除时
if (changes.elementsToRemove != null)
{
// 遍历所有即将删除的元素,同步相应数据
foreach (GraphElement element in changes.elementsToRemove)
{
// 当被删除的不是连线,则跳过
if (element is not Edge) continue;
// 当连线的输出端口为空时,也跳过
Edge edge = (Edge)element;
if (edge.output == null) continue;
// 获取连线输出端的节点(上一个节点)引用的选项数据
ChoiceData choiceData = (ChoiceData)edge.output.userData;
// 清空记录
choiceData.NextNodeID = "";
}
}
return changes;
};
}
- 修改以下方法:
C#
// 构造器
public StoryGraphView(StoryEditorWindow window)
{
// 关联窗口类
storyEditorWindow = window;
// 添加小组件
AddGridBackground();
AddManipulators();
AddNodeCreationBox();
// 定义事件
OnOpenNodeCreationBox();
OnGraphViewChanged();
// 添加默认数据
AddDefaultNodes();
}
测试效果
最终窗口效果如下:

相关链接
- 完整代码:https://gitee.com/helloestar/e-story/wikis/DevelopNotes/08
- 视频版本:制作中……