From ec165a57b9f8f600c989484e2ffb9b29cb710229 Mon Sep 17 00:00:00 2001 From: Simon Jackson Date: Thu, 22 Jun 2017 00:54:02 +0100 Subject: [PATCH] Added Line Renderer Connector from #123 (resolves #123) Added new Extension method for discovering a parent canvas for a non-graphic --- Editor/BezierLineRendererEditor.cs | 2 +- Scripts/Primitives/UILineRenderer.cs | 3 + Scripts/Utilities/UIExtensionMethods.cs | 26 ++++++++ Scripts/Utilities/UIExtensionMethods.cs.meta | 12 ++++ Scripts/Utilities/UILineConnector.cs | 65 ++++++++++++++++++++ Scripts/Utilities/UILineConnector.cs.meta | 12 ++++ 6 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 Scripts/Utilities/UIExtensionMethods.cs create mode 100644 Scripts/Utilities/UIExtensionMethods.cs.meta create mode 100644 Scripts/Utilities/UILineConnector.cs create mode 100644 Scripts/Utilities/UILineConnector.cs.meta diff --git a/Editor/BezierLineRendererEditor.cs b/Editor/BezierLineRendererEditor.cs index 1a25109..5184ac1 100644 --- a/Editor/BezierLineRendererEditor.cs +++ b/Editor/BezierLineRendererEditor.cs @@ -12,7 +12,7 @@ namespace UnityEngine.UI.Extensions { UILineRenderer curveRenderer = target as UILineRenderer; - if (!curveRenderer || curveRenderer.Points == null || curveRenderer.Points.Length < 2) + if (!curveRenderer || curveRenderer.drivenExternally || curveRenderer.Points == null || curveRenderer.Points.Length < 2) { return; } diff --git a/Scripts/Primitives/UILineRenderer.cs b/Scripts/Primitives/UILineRenderer.cs index d0860f0..001a2ca 100644 --- a/Scripts/Primitives/UILineRenderer.cs +++ b/Scripts/Primitives/UILineRenderer.cs @@ -64,6 +64,9 @@ namespace UnityEngine.UI.Extensions public BezierType BezierMode = BezierType.None; public int BezierSegmentsPerCurve = 10; + [HideInInspector] + public bool drivenExternally = false; + /// /// UV rectangle used by the texture. /// diff --git a/Scripts/Utilities/UIExtensionMethods.cs b/Scripts/Utilities/UIExtensionMethods.cs new file mode 100644 index 0000000..bb49049 --- /dev/null +++ b/Scripts/Utilities/UIExtensionMethods.cs @@ -0,0 +1,26 @@ +/// Credit Simon (darkside) Jackson +/// Sourced from - My head +namespace UnityEngine.UI.Extensions +{ + public static class UIExtensionMethods + { + public static Canvas GetParentCanvas(this RectTransform rt) + { + RectTransform parent = rt; + Canvas parentCanvas = rt.GetComponent(); + + int SearchIndex = 0; + while (parentCanvas == null || SearchIndex > 50) + { + parentCanvas = rt.GetComponentInParent(); + if (parentCanvas == null) + { + parent = parent.parent.GetComponent(); + SearchIndex++; + } + } + return parentCanvas; + } + + } +} diff --git a/Scripts/Utilities/UIExtensionMethods.cs.meta b/Scripts/Utilities/UIExtensionMethods.cs.meta new file mode 100644 index 0000000..64d0b56 --- /dev/null +++ b/Scripts/Utilities/UIExtensionMethods.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 08fd87e2db0639a4caf7a053f227a470 +timeCreated: 1498086098 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Scripts/Utilities/UILineConnector.cs b/Scripts/Utilities/UILineConnector.cs new file mode 100644 index 0000000..3038d31 --- /dev/null +++ b/Scripts/Utilities/UILineConnector.cs @@ -0,0 +1,65 @@ +/// Credit Alastair Aitchison +/// Sourced from - https://bitbucket.org/UnityUIExtensions/unity-ui-extensions/issues/123/uilinerenderer-issues-with-specifying + +namespace UnityEngine.UI.Extensions +{ + [RequireComponent(typeof(UILineRenderer))] + [ExecuteInEditMode] + public class UILineConnector : MonoBehaviour + { + + // The elements between which line segments should be drawn + public RectTransform[] transforms; + private RectTransform canvas; + private RectTransform rt; + private UILineRenderer lr; + + private void Awake() + { + canvas = GetComponentInParent().GetParentCanvas().GetComponent(); + rt = GetComponent(); + lr = GetComponent(); + } + + // Update is called once per frame + void Update() + { + if (transforms == null || transforms.Length < 1) + { + 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]; + + // 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; + lr.relativeSize = false; + lr.drivenExternally = true; + } + } +} \ No newline at end of file diff --git a/Scripts/Utilities/UILineConnector.cs.meta b/Scripts/Utilities/UILineConnector.cs.meta new file mode 100644 index 0000000..e6089ea --- /dev/null +++ b/Scripts/Utilities/UILineConnector.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 8b52ee3a4491f3742bb35fa3eec798d3 +timeCreated: 1498085244 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: