Skip to content

Instantly share code, notes, and snippets.

@saint-angels
Created June 16, 2018 08:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saint-angels/deddc61427c07ee3f8d5da67a64f212a to your computer and use it in GitHub Desktop.
Save saint-angels/deddc61427c07ee3f8d5da67a64f212a to your computer and use it in GitHub Desktop.
Lines connecting UI points. Unity
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