diff --git a/Scripts/Editor/NodeEditorGUI.cs b/Scripts/Editor/NodeEditorGUI.cs index 9cc4b1c..a09588f 100644 --- a/Scripts/Editor/NodeEditorGUI.cs +++ b/Scripts/Editor/NodeEditorGUI.cs @@ -217,6 +217,8 @@ namespace XNodeEditor { /// Draws all connections public void DrawConnections() { + List drawnReroutes = new List(); + foreach (XNode.Node node in graph.nodes) { //If a null node is found, return. This can happen if the nodes associated script is deleted. It is currently not possible in Unity to delete a null asset. if (node == null) continue; @@ -224,16 +226,29 @@ namespace XNodeEditor { foreach (XNode.NodePort output in node.Outputs) { //Needs cleanup. Null checks are ugly if (!portConnectionPoints.ContainsKey(output)) continue; + + Color connectionColor = graphEditor.GetTypeColor(output.ValueType); + Vector2 from = _portConnectionPoints[output].center; for (int k = 0; k < output.ConnectionCount; k++) { - XNode.NodePort input = output.GetConnection(k); + + // Error handling if (input == null) continue; //If a script has been updated and the port doesn't exist, it is removed and null is returned. If this happens, return. if (!input.IsConnectedTo(output)) input.Connect(output); if (!_portConnectionPoints.ContainsKey(input)) continue; - Vector2 to = _portConnectionPoints[input].center; - Color connectionColor = graphEditor.GetTypeColor(output.ValueType); - DrawConnection(from, to, connectionColor); + + Vector2 to = Vector2.zero; + int[] rerouteIndices = output.GetReroutes(k); + for (int i = 0; i < rerouteIndices.Length + 1; i++) { + if (i != rerouteIndices.Length) to = graph.reroutes[rerouteIndices[i]]; + else to = _portConnectionPoints[input].center; + DrawConnection(from, to, connectionColor); + from = to; + + if (drawnReroutes.Contains(i)) break; + else drawnReroutes.Add(i); + } } } } diff --git a/Scripts/NodeGraph.cs b/Scripts/NodeGraph.cs index 399ea74..f7d70d0 100644 --- a/Scripts/NodeGraph.cs +++ b/Scripts/NodeGraph.cs @@ -10,11 +10,8 @@ namespace XNode { /// All nodes in the graph. /// See: [SerializeField] public List nodes = new List(); - -#if UNITY_EDITOR - /// Nodes used primarily for organization (Editor only) - public List reroutes = new List(); -#endif + /// Nodes used primarily for organization + [SerializeField] public List reroutes = new List(); /// Add a node to the graph by type public T AddNode() where T : Node { diff --git a/Scripts/NodePort.cs b/Scripts/NodePort.cs index b609066..299559e 100644 --- a/Scripts/NodePort.cs +++ b/Scripts/NodePort.cs @@ -250,6 +250,12 @@ namespace XNode { } } + /// Get reroute indices. This is used for graph organization purposes + /// Connection index + public int[] GetReroutes(int i) { + return connections[i].reroutes; + } + /// Swap connected nodes from the old list with nodes from the new list public void Redirect(List oldNodes, List newNodes) { foreach (PortConnection connection in connections) { @@ -263,6 +269,8 @@ namespace XNode { [SerializeField] public string fieldName; [SerializeField] public Node node; public NodePort Port { get { return port != null ? port : port = GetPort(); } } + /// Used for organization + [SerializeField] public int[] reroutes = new int[0]; [NonSerialized] private NodePort port;