public static GraphPath Build(Wire[] wires) { GraphPath graphPath = new GraphPath(); int indexCount = 0; for (int i = 0; i < wires.Length; i++) { Wire wire = wires[i]; Vector3 endPoint1, endPoint2; wire.GetEndPoints(out endPoint1, out endPoint2); if (!graphPath.NodeExist(endPoint1)) { GraphPathNode graphPathNode1 = new GraphPathNode(); graphPathNode1._id = indexCount++; graphPathNode1._endPoint = endPoint1; if (!graphPath.NodeExist(endPoint2)) { GraphPathNode graphPathNode2 = new GraphPathNode(); graphPathNode2._id = indexCount++; graphPathNode2._endPoint = endPoint2; graphPathNode1._adjacentNodes.Add(graphPathNode2._id); graphPathNode2._adjacentNodes.Add(graphPathNode1._id); graphPath.AddNode(graphPathNode1); graphPath.AddNode(graphPathNode2); } else { // If the node already exist, don't add the node, but update the // adjacent list of the existing node. GraphPathNode existedNode = graphPath.GetNodeByEndpoint(endPoint2); existedNode._adjacentNodes.Add(graphPathNode1._id); graphPathNode1._adjacentNodes.Add(existedNode._id); graphPath.AddNode(graphPathNode1); } } else { if (!graphPath.NodeExist(endPoint2)) { // endpoint1 already exist, but endpoint2 doesn't GraphPathNode existedNode = graphPath.GetNodeByEndpoint(endPoint1); GraphPathNode graphPathNode2 = new GraphPathNode(); graphPathNode2._id = indexCount++; graphPathNode2._endPoint = endPoint2; //graphPathNode2._gameObject = wire.gameObject; graphPathNode2._adjacentNodes.Add(existedNode._id); existedNode._adjacentNodes.Add(graphPathNode2._id); graphPath.AddNode(graphPathNode2); } else { // both endpoint1 and endpoint2 exist, simply connect them GraphPathNode existedNode1 = graphPath.GetNodeByEndpoint(endPoint1); GraphPathNode existedNode2 = graphPath.GetNodeByEndpoint(endPoint2); existedNode1._adjacentNodes.Add(existedNode2._id); existedNode1._adjacentNodes.Add(existedNode1._id); } } } return(graphPath); }