Skip to content

Commit 0e8a588

Browse files
committed
UILineConnector point array calculation
1 parent 35b9985 commit 0e8a588

File tree

2 files changed

+26
-35
lines changed

2 files changed

+26
-35
lines changed

Runtime/Scripts/Primitives/UILineRenderer.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -499,10 +499,6 @@ protected override void OnEnable()
499499
{
500500
m_points = new Vector2[1];
501501
}
502-
if (transform.GetComponent<RectTransform>().position != Vector3.zero)
503-
{
504-
Debug.LogWarning("A Line Renderer component should be on a RectTransform positioned at (0,0,0), do not use in child Objects.\nFor best results, create separate RectTransforms as children of the canvas positioned at (0,0) for a UILineRenderer and do not move.");
505-
}
506502
}
507503
}
508504
}

Runtime/Scripts/Utilities/UILineConnector.cs

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,40 @@ public class UILineConnector : MonoBehaviour
1212
// The elements between which line segments should be drawn
1313
public RectTransform[] transforms;
1414
private Vector3[] previousPositions;
15-
private RectTransform canvas;
15+
private Vector3 previousLrPos;
1616
private RectTransform rt;
1717
private UILineRenderer lr;
1818

19+
1920
private void Awake()
2021
{
21-
var canvasParent = GetComponentInParent<RectTransform>().GetParentCanvas();
22-
if (canvasParent != null)
23-
{
24-
canvas = canvasParent.GetComponent<RectTransform>();
25-
}
2622
rt = GetComponent<RectTransform>();
2723
lr = GetComponent<UILineRenderer>();
2824
}
2925

3026
// Update is called once per frame
3127
void Update()
3228
{
29+
if (lr.RelativeSize)
30+
{
31+
Debug.LogWarning("While using UILineConnector, UILineRenderer should not use relative size, so that even if this RectTransform has a zero-size Rect, the positions of the points can still be calculated");
32+
lr.RelativeSize = false;
33+
}
34+
3335
if (transforms == null || transforms.Length < 1)
3436
{
3537
return;
3638
}
37-
//Performance check to only redraw when the child transforms move
38-
if (previousPositions != null && previousPositions.Length == transforms.Length)
39+
40+
// Get world position of UILineRenderer
41+
Vector3 lrWorldPos = rt.position;
42+
43+
/*Performance check to only redraw when the child transforms move,
44+
or the world position of UILineRenderer moves */
45+
bool updateLine = lrWorldPos != previousLrPos;
46+
47+
if (!updateLine && previousPositions != null && previousPositions.Length == transforms.Length)
3948
{
40-
bool updateLine = false;
4149
for (int i = 0; i < transforms.Length; i++)
4250
{
4351
if (transforms[i] == null)
@@ -47,47 +55,33 @@ void Update()
4755
if (!updateLine && previousPositions[i] != transforms[i].position)
4856
{
4957
updateLine = true;
58+
break;
5059
}
5160
}
52-
if (!updateLine) return;
53-
}
61+
}
62+
if (!updateLine) return;
5463

55-
// Get the pivot points
56-
Vector2 thisPivot = rt.pivot;
57-
Vector2 canvasPivot = canvas.pivot;
5864

59-
// Set up some arrays of coordinates in various reference systems
60-
Vector3[] worldSpaces = new Vector3[transforms.Length];
61-
Vector3[] canvasSpaces = new Vector3[transforms.Length];
65+
// Calculate delta from the local position
6266
Vector2[] points = new Vector2[transforms.Length];
63-
64-
// First, convert the pivot to worldspace
6567
for (int i = 0; i < transforms.Length; i++)
6668
{
6769
if (transforms[i] == null)
6870
{
6971
continue;
7072
}
71-
worldSpaces[i] = transforms[i].TransformPoint(thisPivot);
72-
}
7373

74-
// Then, convert to canvas space
75-
for (int i = 0; i < transforms.Length; i++)
76-
{
77-
canvasSpaces[i] = canvas.InverseTransformPoint(worldSpaces[i]);
78-
}
79-
80-
// Calculate delta from the canvas pivot point
81-
for (int i = 0; i < transforms.Length; i++)
82-
{
83-
points[i] = new Vector2(canvasSpaces[i].x, canvasSpaces[i].y);
74+
var offsetPos = rt.InverseTransformPoint(transforms[i].position);
75+
points[i] = new Vector2(offsetPos.x, offsetPos.y);
8476
}
8577

8678
// And assign the converted points to the line renderer
8779
lr.Points = points;
8880
lr.RelativeSize = false;
8981
lr.drivenExternally = true;
9082

83+
//save previous positions
84+
previousLrPos = lrWorldPos;
9185
previousPositions = new Vector3[transforms.Length];
9286
for (int i = 0; i < transforms.Length; i++)
9387
{
@@ -97,6 +91,7 @@ void Update()
9791
}
9892
previousPositions[i] = transforms[i].position;
9993
}
94+
10095
}
10196
}
10297
}

0 commit comments

Comments
 (0)