Created
June 16, 2018 08:44
-
-
Save saint-angels/deddc61427c07ee3f8d5da67a64f212a to your computer and use it in GitHub Desktop.
Lines connecting UI points. Unity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for (int pIdx = 0; pIdx < points.Count - 1; pIdx++) | |
{ | |
RectTransform point1 = points[pIdx]; | |
RectTransform point2 = points[pIdx + 1]; | |
Vector2 midpoint = (point2.position - point1.position) / 2 + point1.position; | |
float xDifference = point2.position.x - point1.position.x; | |
float yDifference = point2.position.y - point1.position.y; | |
float distance = (point2.position - point1.position).magnitude; | |
float angle = Mathf.Acos(Mathf.Abs(xDifference)/ distance) * 57.2958f; | |
if ((yDifference < 0 && xDifference > 0) || (yDifference > 0 && xDifference < 0)) | |
{ | |
angle = 180 - angle; | |
} | |
var newLine = Instantiate(linePrefab, linesParent, true) as RectTransform; | |
newLine.SetPositionAndRotation(midpoint, Quaternion.Euler(0, 0, angle)); | |
newLine.sizeDelta = new Vector2(distance, newLine.sizeDelta.y); | |
newLine.name = string.Format("Line {0}-{1}", pIdx, pIdx + 1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment