New properties to improve the resolution for a line, which when combined with the LineList creates a dotted line.

Supports two modes: dashes based on segments or the entire line.
pull/413/head
Simon Jackson 2017-06-12 09:02:46 +01:00
parent 9ce211c5fc
commit e373565536
2 changed files with 77 additions and 1 deletions

View File

@ -131,6 +131,11 @@ namespace UnityEngine.UI.Extensions
pointsToDraw = drawingPoints.ToArray();
}
if (ImproveResolution != ResolutionMode.None)
{
pointsToDraw = IncreaseResolution(pointsToDraw);
}
// 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;
@ -243,7 +248,13 @@ namespace UnityEngine.UI.Extensions
vh.AddUIVertexQuad(segments[i]);
}
}
if (vh.currentVertCount > 64000)
{
Debug.LogError("Max Verticies size is 64000, current mesh vertcies count is [" + vh.currentVertCount + "] - Cannot Draw");
vh.Clear();
return;
}
}
private UIVertex[] CreateLineCap(Vector2 start, Vector2 end, SegmentType type)
{

View File

@ -1,7 +1,15 @@
using System;
using System.Collections.Generic;
namespace UnityEngine.UI.Extensions
{
public enum ResolutionMode
{
None,
PerSegment,
PerLine
}
public class UIPrimitiveBase : MaskableGraphic, ILayoutElement, ICanvasRaycastFilter
{
@ -17,6 +25,14 @@ namespace UnityEngine.UI.Extensions
internal float m_EventAlphaThreshold = 1;
public float eventAlphaThreshold { get { return m_EventAlphaThreshold; } set { m_EventAlphaThreshold = value; } }
[SerializeField]
private ResolutionMode m_improveResolution;
public ResolutionMode ImproveResolution { get { return m_improveResolution; } set { m_improveResolution = value; SetAllDirty(); } }
[SerializeField]
private float m_Resolution;
public float Resoloution { get { return m_Resolution; } set { m_Resolution = value; SetAllDirty(); } }
/// <summary>
@ -70,6 +86,52 @@ namespace UnityEngine.UI.Extensions
return vbo;
}
protected Vector2[] IncreaseResolution(Vector2[] input)
{
var outputList = new List<Vector2>();
switch (ImproveResolution)
{
case ResolutionMode.PerLine:
float totalDistance = 0, increments = 0;
for (int i = 0; i < input.Length - 1; i++)
{
totalDistance += Vector2.Distance(input[i], input[i + 1]);
}
increments = totalDistance / m_Resolution;
var incrementCount = 0;
for (int i = 0; i < input.Length - 1; i++)
{
var p1 = input[i];
outputList.Add(p1);
var p2 = input[i + 1];
var segmentDistance = Vector2.Distance(p1, p2) / increments;
var incrementTime = 1f / segmentDistance;
for (int j=0; j < segmentDistance; j++)
{
outputList.Add(Vector2.Lerp(p1, (Vector2)p2, j * incrementTime));
incrementCount++;
}
outputList.Add(p2);
}
break;
case ResolutionMode.PerSegment:
for (int i = 0; i < input.Length - 1; i++)
{
var p1 = input[i];
outputList.Add(p1);
var p2 = input[i + 1];
increments = 1f / m_Resolution;
for (Single j = 1; j < m_Resolution; j++)
{
outputList.Add(Vector2.Lerp(p1, (Vector2)p2, increments * j));
}
outputList.Add(p2);
}
break;
}
return outputList.ToArray();
}
#region ILayoutElement Interface
@ -111,6 +173,7 @@ namespace UnityEngine.UI.Extensions
#region ICanvasRaycastFilter Interface
public virtual bool IsRaycastLocationValid(Vector2 screenPoint, Camera eventCamera)
{
// add test for line check
if (m_EventAlphaThreshold >= 1)
return true;
@ -129,6 +192,8 @@ namespace UnityEngine.UI.Extensions
local = MapCoordinate(local, rect);
//test local coord with Mesh
// Normalize local coordinates.
Rect spriteRect = sprite.textureRect;
Vector2 normalized = new Vector2(local.x / spriteRect.width, local.y / spriteRect.height);