private void TryConnect(ConnectItem otherItem) { if (!Input.GetMouseButtonDown(0)) { return; } ConnectItem element1 = firstItem; ConnectItem element2 = otherItem; bool canConnect = false; ConnectNode.PointGroup groupInfo = null; var node = SuarchOneNode(element1.Name, element2.Name, out groupInfo); if (node != null) { canConnect = ConnectUtil.TryConnect(element1, element2, groupInfo); node.TryComplete(); } ClearStarteds(); if (!canConnect && onError != null) { onError.Invoke(string.Format("{0}和{1}两点不需要连接", element1, element2)); } }
public static LineRenderer TryConnect(ConnectItem itemA, ConnectItem itemB, ConnectNode.PointGroup pointInfo) { if (itemA.OperateAble && itemB.OperateAble) { itemA.OnConnectTo(itemB); itemB.OnConnectTo(itemA); Transform parent = itemA.GetInstanceID() > itemB.GetInstanceID() ? itemA.transform : itemB.transform; string targetName = itemA.GetInstanceID() > itemB.GetInstanceID() ? itemB.Name : itemA.name; var target = parent.Find(targetName); var lineRender = target == null ? null : target.GetComponent <LineRenderer>(); if (lineRender == null) { lineRender = new GameObject(targetName, typeof(LineRenderer)).GetComponent <LineRenderer>(); lineRender.transform.SetParent(parent); } UpdateLineStyle(lineRender, pointInfo.width, pointInfo.material); lineRender.positionCount = 2; var posA = itemA.RetriveFeature <ClickAbleFeature>().collider.transform.position; var posB = itemB.RetriveFeature <ClickAbleFeature>().collider.transform.position; lineRender.SetPositions(new Vector3[] { posA, posB }); // return(lineRender); } else { if (HaveConnected(itemA, itemB)) { Transform parent = itemA.GetInstanceID() > itemB.GetInstanceID() ? itemA.transform : itemB.transform; string targetName = itemA.GetInstanceID() > itemB.GetInstanceID() ? itemB.Name : itemA.name; return(parent.Find(targetName).GetComponent <LineRenderer>()); } return(null); } }
private ConnectNode SuarchOneNode(string itemA, string itemB, out ConnectNode.PointGroup groupInfo) { groupInfo = null; foreach (ConnectNode node in lockList) { groupInfo = node.GetConnectInfo(itemA, itemB); if (groupInfo != null) { return(node); } } return(null); }