2017-06-22 07:54:02 +08:00
|
|
|
|
/// Credit Alastair Aitchison
|
|
|
|
|
/// Sourced from - https://bitbucket.org/UnityUIExtensions/unity-ui-extensions/issues/123/uilinerenderer-issues-with-specifying
|
|
|
|
|
|
|
|
|
|
namespace UnityEngine.UI.Extensions
|
|
|
|
|
{
|
2017-06-26 05:36:09 +08:00
|
|
|
|
[AddComponentMenu("UI/Extensions/UI Line Connector")]
|
2017-06-22 07:54:02 +08:00
|
|
|
|
[RequireComponent(typeof(UILineRenderer))]
|
|
|
|
|
[ExecuteInEditMode]
|
|
|
|
|
public class UILineConnector : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
// The elements between which line segments should be drawn
|
|
|
|
|
public RectTransform[] transforms;
|
2021-05-11 01:38:31 +08:00
|
|
|
|
private Vector3[] previousPositions;
|
2017-06-22 07:54:02 +08:00
|
|
|
|
private RectTransform canvas;
|
|
|
|
|
private RectTransform rt;
|
|
|
|
|
private UILineRenderer lr;
|
|
|
|
|
|
|
|
|
|
private void Awake()
|
|
|
|
|
{
|
2022-04-24 06:25:31 +08:00
|
|
|
|
var canvasParent = GetComponentInParent<RectTransform>().GetParentCanvas();
|
|
|
|
|
if (canvasParent != null)
|
|
|
|
|
{
|
|
|
|
|
canvas = canvasParent.GetComponent<RectTransform>();
|
|
|
|
|
}
|
2017-06-22 07:54:02 +08:00
|
|
|
|
rt = GetComponent<RectTransform>();
|
|
|
|
|
lr = GetComponent<UILineRenderer>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update is called once per frame
|
|
|
|
|
void Update()
|
|
|
|
|
{
|
|
|
|
|
if (transforms == null || transforms.Length < 1)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-08-05 03:36:25 +08:00
|
|
|
|
//Performance check to only redraw when the child transforms move
|
|
|
|
|
if (previousPositions != null && previousPositions.Length == transforms.Length)
|
|
|
|
|
{
|
|
|
|
|
bool updateLine = false;
|
|
|
|
|
for (int i = 0; i < transforms.Length; i++)
|
|
|
|
|
{
|
2021-05-11 01:38:31 +08:00
|
|
|
|
if (!updateLine && previousPositions[i] != transforms[i].position)
|
2017-08-05 03:36:25 +08:00
|
|
|
|
{
|
|
|
|
|
updateLine = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!updateLine) return;
|
|
|
|
|
}
|
2017-06-22 07:54:02 +08:00
|
|
|
|
|
|
|
|
|
// 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];
|
|
|
|
|
|
|
|
|
|
// First, convert the pivot to worldspace
|
|
|
|
|
for (int i = 0; i < transforms.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
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;
|
2017-06-25 18:56:46 +08:00
|
|
|
|
lr.RelativeSize = false;
|
2017-06-22 07:54:02 +08:00
|
|
|
|
lr.drivenExternally = true;
|
2017-08-05 03:36:25 +08:00
|
|
|
|
|
2021-05-11 01:38:31 +08:00
|
|
|
|
previousPositions = new Vector3[transforms.Length];
|
2017-08-05 03:36:25 +08:00
|
|
|
|
for (int i = 0; i < transforms.Length; i++)
|
|
|
|
|
{
|
2021-05-11 01:38:31 +08:00
|
|
|
|
previousPositions[i] = transforms[i].position;
|
2017-08-05 03:36:25 +08:00
|
|
|
|
}
|
2017-06-22 07:54:02 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|