/// <summary> /// Найти ближайшие точки между технологиями (Top, Right, Bottom, Left) /// </summary> /// <param name="source"></param> /// <param name="target"></param> /// <returns></returns> RectTransform[] GetClosestAnchorsBTWTechs(TechnologyOnCanvas source, TechnologyOnCanvas target) { RectTransform[] res = new RectTransform[2]; float minDist = float.MaxValue; int anchorOnSourceID = -1; int anchorOnTargetID = -1; for (int i = 0; i < source.GetUIElement().Anchors.Length; i++) { float distFromSourceCenterToAnchor = Vector2.Distance(source.GetUIElement().transform.position, source.GetUIElement().Anchors[i].position); for (int j = 0; j < target.GetUIElement().Anchors.Length; j++) { float distFromTargetCenterToAnchor = Vector2.Distance(target.GetUIElement().transform.position, target.GetUIElement().Anchors[j].position); float distFromAnchorToAnchor = Vector2.Distance(source.GetUIElement().Anchors[i].position, target.GetUIElement().Anchors[j].position); if (distFromSourceCenterToAnchor + distFromTargetCenterToAnchor + distFromAnchorToAnchor < minDist) { minDist = distFromSourceCenterToAnchor + distFromTargetCenterToAnchor + distFromAnchorToAnchor; anchorOnSourceID = i; anchorOnTargetID = j; } } } res[0] = source.GetUIElement().Anchors[anchorOnSourceID]; res[1] = target.GetUIElement().Anchors[anchorOnTargetID]; return(res); }
/// <summary> /// Найти технологию на канвасе по ID /// </summary> /// <param name="id"></param> /// <returns></returns> TechnologyOnCanvas GetTechOfCanvasByID(TechnologyID id) { TechnologyOnCanvas res = null; foreach (TechnologyOnCanvas t in technologiesOnCanvas) { if (t.GetID() == id) { res = t; } } return(res); }
/// <summary> /// Отрисовать не отрисованные технологии, отрисованным обновить параметры /// </summary> public void InstantiateUpdateTechsOnCanvas() { foreach (Technology t in ttData.GetTechnologies()) { //отрисовать технологию, если такая не отрисована (будет создана в центре, позицию надо выставить вручную if (GetTechOfCanvasByID(t.ID) == null) { GameObject tmp = PrefabUtility.InstantiatePrefab(uiElementPrefab, uiElementsRoot) as GameObject; TechnologyOnCanvas toc = new TechnologyOnCanvas(t.ID, tmp, tmp.GetComponent <UITechnologyElement>()); technologiesOnCanvas.Add(toc); } } UpdateTechParams(); }
/// <summary> /// Создать соединение между двумя технологиями, если такое не существует /// </summary> /// <param name="source"></param> /// <param name="target"></param> void CreateConnection(TechnologyOnCanvas source, TechnologyOnCanvas target) { //Проверить, есть ли соединение, если да, ничего не делать foreach (UITechnologyConnection u in uiConnections) { if (u.GetSource() == source.GetID() && u.GetTarget() == target.GetID()) { return; } } //сначала найти ближайший к центру source якорь на target RectTransform[] anchors = GetClosestAnchorsBTWTechs(source, target); // построить линию между двумя точками UITechnologyConnection connection = Instantiate(uiConnectionPrefab, uiElementsRoot); uiConnections.Add(connection); connection.name = "_" + source.GetID().ToString() + "_to_" + target.GetID().ToString(); connection.Setup(source.GetID(), target.GetID(), anchors[0], anchors[1]); connection.transform.SetAsFirstSibling(); }