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.
release
Simon Jackson 2017-05-08 00:18:06 +01:00
parent 652dd1a646
commit 1482d7c440
2 changed files with 21 additions and 32 deletions

View File

@ -20,10 +20,11 @@ namespace UnityEngine.UI.Extensions
var oldMatrix = Handles.matrix; var oldMatrix = Handles.matrix;
var transform = curveRenderer.GetComponent<RectTransform>(); var transform = curveRenderer.GetComponent<RectTransform>();
//Pivot must be 0,0 to edit //Pivot must be 0,0 to edit
transform.pivot = Vector2.zero; //transform.pivot = Vector2.zero;
Handles.matrix = transform.localToWorldMatrix; Handles.matrix = transform.localToWorldMatrix;
var points = curveRenderer.Points; var points = curveRenderer.Points;
//Need to transform points to worldspace! when set to Relative
for (int i = 0; i < points.Length - 1; i += 2) for (int i = 0; i < points.Length - 1; i += 2)
{ {

View File

@ -7,6 +7,7 @@ using System.Collections.Generic;
namespace UnityEngine.UI.Extensions namespace UnityEngine.UI.Extensions
{ {
[AddComponentMenu("UI/Extensions/Primitives/UILineRenderer")] [AddComponentMenu("UI/Extensions/Primitives/UILineRenderer")]
[RequireComponent(typeof(RectTransform))]
public class UILineRenderer : UIPrimitiveBase public class UILineRenderer : UIPrimitiveBase
{ {
private enum SegmentType private enum SegmentType
@ -54,8 +55,6 @@ namespace UnityEngine.UI.Extensions
protected Vector2[] m_points; protected Vector2[] m_points;
public float LineThickness = 2; public float LineThickness = 2;
public bool UseMargins;
public Vector2 Margin;
public bool relativeSize; public bool relativeSize;
public bool LineList = false; public bool LineList = false;
@ -132,25 +131,11 @@ namespace UnityEngine.UI.Extensions
pointsToDraw = drawingPoints.ToArray(); pointsToDraw = drawingPoints.ToArray();
} }
var sizeX = rectTransform.rect.width; // scale based on the size of the rect or use absolute, this is switchable
var sizeY = rectTransform.rect.height; var sizeX = !relativeSize ? 1 : rectTransform.rect.width;
var offsetX = -rectTransform.pivot.x * rectTransform.rect.width; var sizeY = !relativeSize ? 1 : rectTransform.rect.height;
var offsetY = -rectTransform.pivot.y * rectTransform.rect.height; var offsetX = -rectTransform.pivot.x * sizeX;
var offsetY = -rectTransform.pivot.y * sizeY;
// 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;
}
vh.Clear(); vh.Clear();
@ -279,18 +264,21 @@ namespace UnityEngine.UI.Extensions
private UIVertex[] CreateLineSegment(Vector2 start, Vector2 end, SegmentType type) private UIVertex[] CreateLineSegment(Vector2 start, Vector2 end, SegmentType type)
{ {
var uvs = middleUvs; Vector2 offset = new Vector2((start.y - end.y), end.x - start.x).normalized * LineThickness / 2;
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;
var v1 = start - offset; var v1 = start - offset;
var v2 = start + offset; var v2 = start + offset;
var v3 = end + offset; var v3 = end + offset;
var v4 = 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);
}
} }
} }
} }