add refresh on change in global scale change

pull/480/head
hugoymh 2024-10-02 18:57:57 +08:00
parent 0e8a588fca
commit 0886131d18
1 changed files with 35 additions and 20 deletions

View File

@ -13,18 +13,27 @@ namespace UnityEngine.UI.Extensions
public RectTransform[] transforms; public RectTransform[] transforms;
private Vector3[] previousPositions; private Vector3[] previousPositions;
private Vector3 previousLrPos; private Vector3 previousLrPos;
private Vector3 previousGlobalScale;
private RectTransform rt; private RectTransform rt;
private UILineRenderer lr; private UILineRenderer lr;
private void Awake() private void Awake()
{ {
rt = GetComponent<RectTransform>(); rt = GetComponent<RectTransform>();
lr = GetComponent<UILineRenderer>(); lr = GetComponent<UILineRenderer>();
} }
// Update is called once per frame private void OnEnable()
void Update() {
if (transforms == null || transforms.Length < 1)
{
return;
}
CalculateLinePoints();
}
private void Update()
{ {
if (lr.RelativeSize) if (lr.RelativeSize)
{ {
@ -43,6 +52,7 @@ namespace UnityEngine.UI.Extensions
/*Performance check to only redraw when the child transforms move, /*Performance check to only redraw when the child transforms move,
or the world position of UILineRenderer moves */ or the world position of UILineRenderer moves */
bool updateLine = lrWorldPos != previousLrPos; bool updateLine = lrWorldPos != previousLrPos;
updateLine = rt.lossyScale != previousGlobalScale;
if (!updateLine && previousPositions != null && previousPositions.Length == transforms.Length) 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 // Calculate delta from the local position
Vector2[] points = new Vector2[transforms.Length]; CalculateLinePoints();
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 //save previous states
lr.Points = points;
lr.RelativeSize = false;
lr.drivenExternally = true;
//save previous positions
previousLrPos = lrWorldPos; previousLrPos = lrWorldPos;
previousGlobalScale = rt.lossyScale;
previousPositions = new Vector3[transforms.Length]; previousPositions = new Vector3[transforms.Length];
for (int i = 0; i < transforms.Length; i++) for (int i = 0; i < transforms.Length; i++)
{ {
@ -91,7 +88,25 @@ namespace UnityEngine.UI.Extensions
} }
previousPositions[i] = transforms[i].position; 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;
} }
} }
} }