@@ -12,32 +12,50 @@ 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 ;
16+ private Vector3 previousGlobalScale ;
1617 private RectTransform rt ;
1718 private UILineRenderer lr ;
1819
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
30- // Update is called once per frame
31- void Update ( )
26+ private void OnEnable ( )
3227 {
3328 if ( transforms == null || transforms . Length < 1 )
3429 {
3530 return ;
3631 }
37- //Performance check to only redraw when the child transforms move
38- if ( previousPositions != null && previousPositions . Length == transforms . Length )
32+
33+ CalculateLinePoints ( ) ;
34+ }
35+
36+ private void Update ( )
37+ {
38+ if ( lr . RelativeSize )
39+ {
40+ 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" ) ;
41+ lr . RelativeSize = false ;
42+ }
43+
44+ if ( transforms == null || transforms . Length < 1 )
45+ {
46+ return ;
47+ }
48+
49+ // Get world position of UILineRenderer
50+ Vector3 lrWorldPos = rt . position ;
51+
52+ /*Performance check to only redraw when the child transforms move,
53+ or the world position of UILineRenderer moves */
54+ bool updateLine = lrWorldPos != previousLrPos ;
55+ updateLine = rt . lossyScale != previousGlobalScale ;
56+
57+ if ( ! updateLine && previousPositions != null && previousPositions . Length == transforms . Length )
3958 {
40- bool updateLine = false ;
4159 for ( int i = 0 ; i < transforms . Length ; i ++ )
4260 {
4361 if ( transforms [ i ] == null )
@@ -47,56 +65,48 @@ void Update()
4765 if ( ! updateLine && previousPositions [ i ] != transforms [ i ] . position )
4866 {
4967 updateLine = true ;
68+ break ;
5069 }
5170 }
52- if ( ! updateLine ) return ;
53- }
71+ }
72+ if ( ! updateLine ) return ;
5473
55- // Get the pivot points
56- Vector2 thisPivot = rt . pivot ;
57- Vector2 canvasPivot = canvas . pivot ;
5874
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 ] ;
62- Vector2 [ ] points = new Vector2 [ transforms . Length ] ;
75+ // Calculate delta from the local position
76+ CalculateLinePoints ( ) ;
77+
6378
64- // First, convert the pivot to worldspace
79+ //save previous states
80+ previousLrPos = lrWorldPos ;
81+ previousGlobalScale = rt . lossyScale ;
82+ previousPositions = new Vector3 [ transforms . Length ] ;
6583 for ( int i = 0 ; i < transforms . Length ; i ++ )
6684 {
6785 if ( transforms [ i ] == null )
6886 {
6987 continue ;
7088 }
71- worldSpaces [ i ] = transforms [ i ] . TransformPoint ( thisPivot ) ;
72- }
73-
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 ) ;
89+ previousPositions [ i ] = transforms [ i ] . position ;
8490 }
91+ }
8592
86- // And assign the converted points to the line renderer
87- lr . Points = points ;
88- lr . RelativeSize = false ;
89- lr . drivenExternally = true ;
90-
91- previousPositions = new Vector3 [ transforms . Length ] ;
93+ private void CalculateLinePoints ( )
94+ {
95+ Vector2 [ ] points = new Vector2 [ transforms . Length ] ;
9296 for ( int i = 0 ; i < transforms . Length ; i ++ )
9397 {
9498 if ( transforms [ i ] == null )
9599 {
96100 continue ;
97101 }
98- previousPositions [ i ] = transforms [ i ] . position ;
102+ var offsetPos = rt . InverseTransformPoint ( transforms [ i ] . position ) ;
103+ points [ i ] = new Vector2 ( offsetPos . x , offsetPos . y ) ;
99104 }
105+
106+ // And assign the converted points to the line renderer
107+ lr . Points = points ;
108+ lr . RelativeSize = false ;
109+ lr . drivenExternally = true ;
100110 }
101111 }
102112}
0 commit comments