Merge pull request #480 from hugoymh/uilineconnector

UILineConnector point array calculation
pull/482/head
Simon (Darkside) Jackson 2024-10-04 11:40:30 +01:00 committed by GitHub
commit 2d5c46ed35
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 56 additions and 50 deletions

View File

@ -499,10 +499,6 @@ namespace UnityEngine.UI.Extensions
{
m_points = new Vector2[1];
}
if (transform.GetComponent<RectTransform>().position != Vector3.zero)
{
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.");
}
}
}
}

View File

@ -12,32 +12,50 @@ namespace UnityEngine.UI.Extensions
// The elements between which line segments should be drawn
public RectTransform[] transforms;
private Vector3[] previousPositions;
private RectTransform canvas;
private Vector3 previousLrPos;
private Vector3 previousGlobalScale;
private RectTransform rt;
private UILineRenderer lr;
private void Awake()
{
var canvasParent = GetComponentInParent<RectTransform>().GetParentCanvas();
if (canvasParent != null)
{
canvas = canvasParent.GetComponent<RectTransform>();
}
rt = GetComponent<RectTransform>();
lr = GetComponent<UILineRenderer>();
}
// Update is called once per frame
void Update()
private void OnEnable()
{
if (transforms == null || transforms.Length < 1)
{
return;
}
//Performance check to only redraw when the child transforms move
if (previousPositions != null && previousPositions.Length == transforms.Length)
CalculateLinePoints();
}
private void Update()
{
if (lr.RelativeSize)
{
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");
lr.RelativeSize = false;
}
if (transforms == null || transforms.Length < 1)
{
return;
}
// Get world position of UILineRenderer
Vector3 lrWorldPos = rt.position;
/*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)
{
bool updateLine = false;
for (int i = 0; i < transforms.Length; i++)
{
if (transforms[i] == null)
@ -47,47 +65,20 @@ namespace UnityEngine.UI.Extensions
if (!updateLine && previousPositions[i] != transforms[i].position)
{
updateLine = true;
break;
}
}
if (!updateLine) return;
}
}
if (!updateLine) return;
// Get the pivot points
Vector2 thisPivot = rt.pivot;
Vector2 canvasPivot = canvas.pivot;
// Set up some arrays of coordinates in various reference systems
Vector3[] worldSpaces = new Vector3[transforms.Length];
Vector3[] canvasSpaces = new Vector3[transforms.Length];
Vector2[] points = new Vector2[transforms.Length];
// Calculate delta from the local position
CalculateLinePoints();
// First, convert the pivot to worldspace
for (int i = 0; i < transforms.Length; i++)
{
if (transforms[i] == null)
{
continue;
}
worldSpaces[i] = transforms[i].TransformPoint(thisPivot);
}
// Then, convert to canvas space
for (int i = 0; i < transforms.Length; i++)
{
canvasSpaces[i] = canvas.InverseTransformPoint(worldSpaces[i]);
}
// Calculate delta from the canvas pivot point
for (int i = 0; i < transforms.Length; i++)
{
points[i] = new Vector2(canvasSpaces[i].x, canvasSpaces[i].y);
}
// And assign the converted points to the line renderer
lr.Points = points;
lr.RelativeSize = false;
lr.drivenExternally = true;
//save previous states
previousLrPos = lrWorldPos;
previousGlobalScale = rt.lossyScale;
previousPositions = new Vector3[transforms.Length];
for (int i = 0; i < transforms.Length; i++)
{
@ -98,5 +89,24 @@ 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;
}
}
}