Updated LineRenderer Example Scene

Added new Circle class (example in LineRenderer scene)

Changed some of the LineRenderer public variables to public properties, else they are not assignable in the editor for events.  Will check other controls as well.
pull/413/head
Simon Jackson 2017-06-25 11:56:46 +01:00
parent ec165a57b9
commit 290f550fc4
10 changed files with 1829 additions and 70 deletions

View File

@ -35,7 +35,7 @@ namespace UnityEngine.UI.Extensions
}
//Need to transform points to worldspace! when set to Relative
if (curveRenderer.relativeSize)
if (curveRenderer.RelativeSize)
{
for (int i = 0; i < points.Length; i++)
{
@ -57,7 +57,7 @@ namespace UnityEngine.UI.Extensions
if (check.changed)
{
Undo.RecordObject(curveRenderer, "Changed Curve Position");
if (curveRenderer.relativeSize)
if (curveRenderer.RelativeSize)
{
curveRenderer.Points[i] = new Vector2((p.x - offsetX) / sizeX, (p.y - offsetY) / sizeY);
}

View File

@ -0,0 +1,79 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI.Extensions;
[RequireComponent(typeof(UILineRenderer))]
public class LineRendererOrbit : MonoBehaviour {
UILineRenderer lr;
Circle circle;
public GameObject OrbitGO;
RectTransform orbitGOrt;
float orbitTime;
[SerializeField]
private float _xAxis = 3;
public float xAxis
{
get { return _xAxis; }
set { _xAxis = value; GenerateOrbit(); }
}
[SerializeField]
private float _yAxis = 3;
public float yAxis
{
get { return _yAxis; }
set { _yAxis = value; GenerateOrbit(); }
}
[SerializeField]
private int _steps = 10;
public int Steps
{
get { return _steps; }
set { _steps = value; GenerateOrbit(); }
}
// Use this for initialization
void Awake () {
lr = GetComponent<UILineRenderer>();
orbitGOrt = OrbitGO.GetComponent<RectTransform>();
GenerateOrbit();
}
// Update is called once per frame
void Update () {
orbitTime = orbitTime > _steps ? orbitTime = 0 : orbitTime + Time.deltaTime;
orbitGOrt.localPosition = circle.Evaluate(orbitTime);
}
void GenerateOrbit()
{
circle = new Circle(xAxis: _xAxis,yAxis: _yAxis, steps: _steps);
List<Vector2> Points = new List<Vector2>();
for (int i = 0; i < _steps; i++)
{
Points.Add(circle.Evaluate(i));
}
Points.Add(circle.Evaluate(0));
lr.Points = Points.ToArray();
}
private void OnValidate()
{
if (lr != null)
{
GenerateOrbit();
}
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 544c2baa26170b34fb34c19cb04b1fb1
timeCreated: 1498381316
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -14,4 +14,9 @@ public class TestAddingPoints : MonoBehaviour {
pointlist.Add(point);
LineRenderer.Points = pointlist.ToArray();
}
public void ClearPoints()
{
LineRenderer.Points = new Vector2[0];
}
}

File diff suppressed because it is too large Load Diff

View File

@ -52,7 +52,7 @@ namespace UnityEngine.UI.Extensions
protected override void OnPopulateMesh(VertexHelper vh)
{
relativeSize = true;
RelativeSize = true;
int ArraySize = (GridRows * 3) + 1;
if(GridRows % 2 == 0)

View File

@ -54,15 +54,50 @@ namespace UnityEngine.UI.Extensions
[SerializeField]
protected Vector2[] m_points;
public float LineThickness = 2;
public bool relativeSize;
[SerializeField]
private float lineThickness = 2;
[SerializeField]
private bool relativeSize;
[SerializeField]
private bool lineList;
[SerializeField]
private bool lineCaps;
[SerializeField]
private int bezierSegmentsPerCurve = 10;
public float LineThickness
{
get { return lineThickness; }
set { lineThickness = value; SetAllDirty(); }
}
public bool RelativeSize
{
get { return relativeSize; }
set { relativeSize = value; SetAllDirty(); }
}
public bool LineList
{
get { return lineList; }
set { lineList = value; SetAllDirty(); }
}
public bool LineCaps
{
get { return lineCaps; }
set { lineCaps = value; SetAllDirty(); }
}
public bool LineList = false;
public bool LineCaps = false;
public JoinType LineJoins = JoinType.Bevel;
public BezierType BezierMode = BezierType.None;
public int BezierSegmentsPerCurve = 10;
public int BezierSegmentsPerCurve
{
get { return bezierSegmentsPerCurve; }
set { bezierSegmentsPerCurve = value; }
}
[HideInInspector]
public bool drivenExternally = false;
@ -116,7 +151,7 @@ namespace UnityEngine.UI.Extensions
BezierPath bezierPath = new BezierPath();
bezierPath.SetControlPoints(pointsToDraw);
bezierPath.SegmentsPerCurve = BezierSegmentsPerCurve;
bezierPath.SegmentsPerCurve = bezierSegmentsPerCurve;
List<Vector2> drawingPoints;
switch (BezierMode)
{
@ -149,7 +184,7 @@ namespace UnityEngine.UI.Extensions
// Generate the quads that make up the wide line
var segments = new List<UIVertex[]>();
if (LineList)
if (lineList)
{
for (var i = 1; i < pointsToDraw.Length; i += 2)
{
@ -158,14 +193,14 @@ namespace UnityEngine.UI.Extensions
start = new Vector2(start.x * sizeX + offsetX, start.y * sizeY + offsetY);
end = new Vector2(end.x * sizeX + offsetX, end.y * sizeY + offsetY);
if (LineCaps)
if (lineCaps)
{
segments.Add(CreateLineCap(start, end, SegmentType.Start));
}
segments.Add(CreateLineSegment(start, end, SegmentType.Middle));
if (LineCaps)
if (lineCaps)
{
segments.Add(CreateLineCap(start, end, SegmentType.End));
}
@ -180,14 +215,14 @@ namespace UnityEngine.UI.Extensions
start = new Vector2(start.x * sizeX + offsetX, start.y * sizeY + offsetY);
end = new Vector2(end.x * sizeX + offsetX, end.y * sizeY + offsetY);
if (LineCaps && i == 1)
if (lineCaps && i == 1)
{
segments.Add(CreateLineCap(start, end, SegmentType.Start));
}
segments.Add(CreateLineSegment(start, end, SegmentType.Middle));
if (LineCaps && i == pointsToDraw.Length - 1)
if (lineCaps && i == pointsToDraw.Length - 1)
{
segments.Add(CreateLineCap(start, end, SegmentType.End));
}
@ -197,7 +232,7 @@ namespace UnityEngine.UI.Extensions
// Add the line segments to the vertex helper, creating any joins as needed
for (var i = 0; i < segments.Count; i++)
{
if (!LineList && i < segments.Count - 1)
if (!lineList && i < segments.Count - 1)
{
var vec1 = segments[i][1].position - segments[i][2].position;
var vec2 = segments[i + 1][2].position - segments[i + 1][1].position;
@ -207,7 +242,7 @@ namespace UnityEngine.UI.Extensions
var sign = Mathf.Sign(Vector3.Cross(vec1.normalized, vec2.normalized).z);
// Calculate the miter point
var miterDistance = LineThickness / (2 * Mathf.Tan(angle / 2));
var miterDistance = lineThickness / (2 * Mathf.Tan(angle / 2));
var miterPointA = segments[i][2].position - vec1.normalized * miterDistance * sign;
var miterPointB = segments[i][3].position + vec1.normalized * miterDistance * sign;
@ -263,12 +298,12 @@ namespace UnityEngine.UI.Extensions
{
if (type == SegmentType.Start)
{
var capStart = start - ((end - start).normalized * LineThickness / 2);
var capStart = start - ((end - start).normalized * lineThickness / 2);
return CreateLineSegment(capStart, start, SegmentType.Start);
}
else if (type == SegmentType.End)
{
var capEnd = end + ((end - start).normalized * LineThickness / 2);
var capEnd = end + ((end - start).normalized * lineThickness / 2);
return CreateLineSegment(end, capEnd, SegmentType.End);
}
@ -278,7 +313,7 @@ namespace UnityEngine.UI.Extensions
private UIVertex[] CreateLineSegment(Vector2 start, Vector2 end, SegmentType type)
{
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;

View File

@ -0,0 +1,82 @@
/// Credit Board To Bits Games
/// Original Sourced from - https://www.youtube.com/watch?v=Or3fA-UjnwU
/// Updated and modified for UI Extensions to be more generic
namespace UnityEngine.UI.Extensions
{
public class Circle
{
[SerializeField]
private float xAxis;
[SerializeField]
private float yAxis;
[SerializeField]
private int steps;
public float X
{
get { return xAxis; }
set { xAxis = value; }
}
public float Y
{
get { return yAxis; }
set { yAxis = value; }
}
public int Steps
{
get { return steps; }
set { steps = value; }
}
public Circle(float radius)
{
this.xAxis = radius;
this.yAxis = radius;
this.steps = 1;
}
public Circle(float radius, int steps)
{
this.xAxis = radius;
this.yAxis = radius;
this.steps = steps;
}
public Circle(float xAxis, float yAxis)
{
this.xAxis = xAxis;
this.yAxis = yAxis;
this.steps = 10;
}
public Circle(float xAxis, float yAxis, int steps)
{
this.xAxis = xAxis;
this.yAxis = yAxis;
this.steps = steps;
}
public Vector2 Evaluate(float t)
{
float increments = 360f / steps;
float angle = Mathf.Deg2Rad * increments * t;
float x = Mathf.Sin(angle) * xAxis;
float y = Mathf.Cos(angle) * yAxis;
return new Vector2(x, y);
}
public void Evaluate(float t, out Vector2 eval)
{
float increments = 360f / steps;
float angle = Mathf.Deg2Rad * increments * t;
eval.x = Mathf.Sin(angle) * xAxis;
eval.y = Mathf.Cos(angle) * yAxis;
}
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: f11f61a6960d26445a760da7dae63cf2
timeCreated: 1498154251
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -58,7 +58,7 @@ namespace UnityEngine.UI.Extensions
// And assign the converted points to the line renderer
lr.Points = points;
lr.relativeSize = false;
lr.RelativeSize = false;
lr.drivenExternally = true;
}
}