@@ -13,18 +13,27 @@ public class UILineConnector : MonoBehaviour
1313 public RectTransform [ ] transforms ;
1414 private Vector3 [ ] previousPositions ;
1515 private Vector3 previousLrPos ;
16+ private Vector3 previousGlobalScale ;
1617 private RectTransform rt ;
1718 private UILineRenderer lr ;
1819
19-
2020 private void Awake ( )
2121 {
2222 rt = GetComponent < RectTransform > ( ) ;
2323 lr = GetComponent < UILineRenderer > ( ) ;
2424 }
2525
26- // Update is called once per frame
27- void Update ( )
26+ private void OnEnable ( )
27+ {
28+ if ( transforms == null || transforms . Length < 1 )
29+ {
30+ return ;
31+ }
32+
33+ CalculateLinePoints ( ) ;
34+ }
35+
36+ private void Update ( )
2837 {
2938 if ( lr . RelativeSize )
3039 {
@@ -43,6 +52,7 @@ void Update()
4352 /*Performance check to only redraw when the child transforms move,
4453 or the world position of UILineRenderer moves */
4554 bool updateLine = lrWorldPos != previousLrPos ;
55+ updateLine = rt . lossyScale != previousGlobalScale ;
4656
4757 if ( ! updateLine && previousPositions != null && previousPositions . Length == transforms . Length )
4858 {
@@ -59,39 +69,44 @@ or the world position of UILineRenderer moves */
5969 }
6070 }
6171 }
62- if ( ! updateLine ) return ;
72+ if ( ! updateLine ) return ;
6373
6474
6575 // Calculate delta from the local position
66- Vector2 [ ] points = new Vector2 [ transforms . Length ] ;
76+ CalculateLinePoints ( ) ;
77+
78+
79+ //save previous states
80+ previousLrPos = lrWorldPos ;
81+ previousGlobalScale = rt . lossyScale ;
82+ previousPositions = new Vector3 [ transforms . Length ] ;
6783 for ( int i = 0 ; i < transforms . Length ; i ++ )
6884 {
6985 if ( transforms [ i ] == null )
7086 {
7187 continue ;
7288 }
73-
74- var offsetPos = rt . InverseTransformPoint ( transforms [ i ] . position ) ;
75- points [ i ] = new Vector2 ( offsetPos . x , offsetPos . y ) ;
89+ previousPositions [ i ] = transforms [ i ] . position ;
7690 }
91+ }
7792
78- // And assign the converted points to the line renderer
79- lr . Points = points ;
80- lr . RelativeSize = false ;
81- lr . drivenExternally = true ;
82-
83- //save previous positions
84- previousLrPos = lrWorldPos ;
85- previousPositions = new Vector3 [ transforms . Length ] ;
93+ private void CalculateLinePoints ( )
94+ {
95+ Vector2 [ ] points = new Vector2 [ transforms . Length ] ;
8696 for ( int i = 0 ; i < transforms . Length ; i ++ )
8797 {
8898 if ( transforms [ i ] == null )
8999 {
90100 continue ;
91101 }
92- previousPositions [ i ] = transforms [ i ] . position ;
102+ var offsetPos = rt . InverseTransformPoint ( transforms [ i ] . position ) ;
103+ points [ i ] = new Vector2 ( offsetPos . x , offsetPos . y ) ;
93104 }
94105
106+ // And assign the converted points to the line renderer
107+ lr . Points = points ;
108+ lr . RelativeSize = false ;
109+ lr . drivenExternally = true ;
95110 }
96111 }
97112}
0 commit comments