From 1482d7c44057e22e1fd145b6738c5d61093f51c3 Mon Sep 17 00:00:00 2001 From: Simon Jackson Date: Mon, 8 May 2017 00:18:06 +0100 Subject: [PATCH] Resolves #138 Line Renderer now renders based on Pivot and resolves drawing issue with Editor script when using !Relative. However, need to update editor script to work with relative drawing. --- Editor/BezierLineRendererEditor.cs | 3 +- Scripts/Primitives/UILineRenderer.cs | 50 +++++++++++----------------- 2 files changed, 21 insertions(+), 32 deletions(-) diff --git a/Editor/BezierLineRendererEditor.cs b/Editor/BezierLineRendererEditor.cs index 7689f3d..34b73ac 100644 --- a/Editor/BezierLineRendererEditor.cs +++ b/Editor/BezierLineRendererEditor.cs @@ -20,10 +20,11 @@ namespace UnityEngine.UI.Extensions var oldMatrix = Handles.matrix; var transform = curveRenderer.GetComponent(); //Pivot must be 0,0 to edit - transform.pivot = Vector2.zero; + //transform.pivot = Vector2.zero; Handles.matrix = transform.localToWorldMatrix; var points = curveRenderer.Points; + //Need to transform points to worldspace! when set to Relative for (int i = 0; i < points.Length - 1; i += 2) { diff --git a/Scripts/Primitives/UILineRenderer.cs b/Scripts/Primitives/UILineRenderer.cs index bc5ce32..4be0362 100644 --- a/Scripts/Primitives/UILineRenderer.cs +++ b/Scripts/Primitives/UILineRenderer.cs @@ -7,7 +7,8 @@ using System.Collections.Generic; namespace UnityEngine.UI.Extensions { [AddComponentMenu("UI/Extensions/Primitives/UILineRenderer")] - public class UILineRenderer : UIPrimitiveBase + [RequireComponent(typeof(RectTransform))] + public class UILineRenderer : UIPrimitiveBase { private enum SegmentType { @@ -54,8 +55,6 @@ namespace UnityEngine.UI.Extensions protected Vector2[] m_points; public float LineThickness = 2; - public bool UseMargins; - public Vector2 Margin; public bool relativeSize; public bool LineList = false; @@ -103,7 +102,7 @@ namespace UnityEngine.UI.Extensions } } - protected override void OnPopulateMesh(VertexHelper vh) + protected override void OnPopulateMesh(VertexHelper vh) { if (m_points == null) return; @@ -132,25 +131,11 @@ namespace UnityEngine.UI.Extensions pointsToDraw = drawingPoints.ToArray(); } - var sizeX = rectTransform.rect.width; - var sizeY = rectTransform.rect.height; - var offsetX = -rectTransform.pivot.x * rectTransform.rect.width; - var offsetY = -rectTransform.pivot.y * rectTransform.rect.height; - - // don't want to scale based on the size of the rect, so this is switchable now - if (!relativeSize) - { - sizeX = 1; - sizeY = 1; - } - - if (UseMargins) - { - sizeX -= Margin.x; - sizeY -= Margin.y; - offsetX += Margin.x / 2f; - offsetY += Margin.y / 2f; - } + // scale based on the size of the rect or use absolute, this is switchable + var sizeX = !relativeSize ? 1 : rectTransform.rect.width; + var sizeY = !relativeSize ? 1 : rectTransform.rect.height; + var offsetX = -rectTransform.pivot.x * sizeX; + var offsetY = -rectTransform.pivot.y * sizeY; vh.Clear(); @@ -279,18 +264,21 @@ namespace UnityEngine.UI.Extensions private UIVertex[] CreateLineSegment(Vector2 start, Vector2 end, SegmentType type) { - var uvs = middleUvs; - if (type == SegmentType.Start) - uvs = startUvs; - else if (type == SegmentType.End) - uvs = endUvs; - - Vector2 offset = new Vector2(start.y - end.y, end.x - start.x).normalized * LineThickness / 2; + Vector2 offset = new Vector2((start.y - end.y), end.x - start.x).normalized * LineThickness / 2; var v1 = start - offset; var v2 = start + offset; var v3 = end + offset; var v4 = end - offset; - return SetVbo(new[] { v1, v2, v3, v4 }, uvs); + //Return the VDO with the correct uvs + switch (type) + { + case SegmentType.Start: + return SetVbo(new[] { v1, v2, v3, v4 }, startUvs); + case SegmentType.End: + return SetVbo(new[] { v1, v2, v3, v4 }, endUvs); + default: + return SetVbo(new[] { v1, v2, v3, v4 }, middleUvs); + } } } } \ No newline at end of file