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); } }
public static bool TryDisconnect(ConnectItem itemA, ConnectItem itemB) { if (itemA != null && itemB != null) { itemA.OnDisConnectTo(itemB); itemB.OnDisConnectTo(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.positionCount = 0; } return(true); } return(false); }