From 0886131d180750e846f9b4f74b0e80d080847415 Mon Sep 17 00:00:00 2001 From: hugoymh Date: Wed, 2 Oct 2024 18:57:57 +0800 Subject: [PATCH] add refresh on change in global scale change --- Runtime/Scripts/Utilities/UILineConnector.cs | 55 +++++++++++++------- 1 file changed, 35 insertions(+), 20 deletions(-) diff --git a/Runtime/Scripts/Utilities/UILineConnector.cs b/Runtime/Scripts/Utilities/UILineConnector.cs index 454e235..7423d53 100644 --- a/Runtime/Scripts/Utilities/UILineConnector.cs +++ b/Runtime/Scripts/Utilities/UILineConnector.cs @@ -13,18 +13,27 @@ namespace UnityEngine.UI.Extensions public RectTransform[] transforms; private Vector3[] previousPositions; private Vector3 previousLrPos; + private Vector3 previousGlobalScale; private RectTransform rt; private UILineRenderer lr; - private void Awake() { rt = GetComponent(); lr = GetComponent(); } - // Update is called once per frame - void Update() + private void OnEnable() + { + if (transforms == null || transforms.Length < 1) + { + return; + } + + CalculateLinePoints(); + } + + private void Update() { if (lr.RelativeSize) { @@ -43,6 +52,7 @@ namespace UnityEngine.UI.Extensions /*Performance check to only redraw when the child transforms move, or the world position of UILineRenderer moves */ bool updateLine = lrWorldPos != previousLrPos; + updateLine = rt.lossyScale != previousGlobalScale; if (!updateLine && previousPositions != null && previousPositions.Length == transforms.Length) { @@ -59,29 +69,16 @@ namespace UnityEngine.UI.Extensions } } } - if (!updateLine) return; + if (!updateLine) return; // Calculate delta from the local position - Vector2[] points = new Vector2[transforms.Length]; - for (int i = 0; i < transforms.Length; i++) - { - if (transforms[i] == null) - { - continue; - } + CalculateLinePoints(); - var offsetPos = rt.InverseTransformPoint(transforms[i].position); - points[i] = new Vector2(offsetPos.x, offsetPos.y); - } - // And assign the converted points to the line renderer - lr.Points = points; - lr.RelativeSize = false; - lr.drivenExternally = true; - - //save previous positions + //save previous states previousLrPos = lrWorldPos; + previousGlobalScale = rt.lossyScale; previousPositions = new Vector3[transforms.Length]; for (int i = 0; i < transforms.Length; i++) { @@ -91,7 +88,25 @@ namespace UnityEngine.UI.Extensions } previousPositions[i] = transforms[i].position; } + } + private void CalculateLinePoints() + { + Vector2[] points = new Vector2[transforms.Length]; + for (int i = 0; i < transforms.Length; i++) + { + if (transforms[i] == null) + { + continue; + } + var offsetPos = rt.InverseTransformPoint(transforms[i].position); + points[i] = new Vector2(offsetPos.x, offsetPos.y); + } + + // And assign the converted points to the line renderer + lr.Points = points; + lr.RelativeSize = false; + lr.drivenExternally = true; } } } \ No newline at end of file