2016-05-17 20:26:50 +08:00
|
|
|
/// Credit zge, jeremie sellam
|
2015-09-20 07:24:17 +08:00
|
|
|
/// Sourced from - http://forum.unity3d.com/threads/draw-circles-or-primitives-on-the-new-ui-canvas.272488/#post-2293224
|
2016-05-17 20:26:50 +08:00
|
|
|
/// Updated from - https://bitbucket.org/ddreaper/unity-ui-extensions/issues/65/a-better-uicircle
|
2015-09-20 07:24:17 +08:00
|
|
|
|
|
|
|
namespace UnityEngine.UI.Extensions
|
2015-09-20 08:21:29 +08:00
|
|
|
{
|
2015-09-20 08:49:52 +08:00
|
|
|
[AddComponentMenu("UI/Extensions/Primitives/UI Circle")]
|
2016-05-25 07:56:18 +08:00
|
|
|
public class UICircle : UIPrimitiveBase
|
2015-09-20 07:24:17 +08:00
|
|
|
{
|
2016-05-17 20:26:50 +08:00
|
|
|
[Tooltip("The circular fill percentage of the primitive, affected by FixedToSegments")]
|
2015-09-20 07:24:17 +08:00
|
|
|
[Range(0, 100)]
|
2017-02-11 00:45:50 +08:00
|
|
|
[SerializeField]
|
|
|
|
private int m_fillPercent = 100;
|
2016-05-17 20:26:50 +08:00
|
|
|
[Tooltip("Should the primitive fill draw by segments or absolute percentage")]
|
|
|
|
public bool FixedToSegments = false;
|
|
|
|
[Tooltip("Draw the primitive filled or as a line")]
|
2017-02-11 00:45:50 +08:00
|
|
|
[SerializeField]
|
|
|
|
private bool m_fill = true;
|
2016-05-17 20:26:50 +08:00
|
|
|
[Tooltip("If not filled, the thickness of the primitive line")]
|
2017-02-11 00:45:50 +08:00
|
|
|
[SerializeField]
|
|
|
|
private float m_thickness = 5;
|
2016-05-17 20:26:50 +08:00
|
|
|
[Tooltip("The number of segments to draw the primitive, more segments = smoother primitive")]
|
2015-09-20 07:24:17 +08:00
|
|
|
[Range(0, 360)]
|
2017-02-11 00:45:50 +08:00
|
|
|
[SerializeField]
|
|
|
|
private int m_segments = 360;
|
|
|
|
|
|
|
|
|
|
|
|
public int FillPercent
|
|
|
|
{
|
|
|
|
get { return m_fillPercent; }
|
|
|
|
set { m_fillPercent = value; SetAllDirty(); }
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Fill
|
|
|
|
{
|
|
|
|
get { return m_fill; }
|
|
|
|
set { m_fill = value; SetAllDirty(); }
|
|
|
|
}
|
|
|
|
|
|
|
|
public float Thickness
|
|
|
|
{
|
|
|
|
get { return m_thickness; }
|
|
|
|
set { m_thickness = value; SetAllDirty(); }
|
|
|
|
}
|
|
|
|
|
2016-05-25 07:50:10 +08:00
|
|
|
|
2015-09-20 08:49:52 +08:00
|
|
|
void Update()
|
|
|
|
{
|
2017-02-11 00:45:50 +08:00
|
|
|
this.m_thickness = (float)Mathf.Clamp(this.m_thickness, 0, rectTransform.rect.width / 2);
|
2015-09-20 08:49:52 +08:00
|
|
|
}
|
2017-02-11 00:45:50 +08:00
|
|
|
|
|
|
|
public int Segments
|
|
|
|
{
|
|
|
|
get { return m_segments; }
|
|
|
|
set { m_segments = value; SetAllDirty(); }
|
|
|
|
}
|
|
|
|
|
2015-10-11 19:05:53 +08:00
|
|
|
protected override void OnPopulateMesh(VertexHelper vh)
|
2015-09-20 07:24:17 +08:00
|
|
|
{
|
|
|
|
float outer = -rectTransform.pivot.x * rectTransform.rect.width;
|
2017-02-11 00:45:50 +08:00
|
|
|
float inner = -rectTransform.pivot.x * rectTransform.rect.width + this.m_thickness;
|
2015-09-20 08:49:52 +08:00
|
|
|
|
2015-10-11 19:05:53 +08:00
|
|
|
vh.Clear();
|
2015-09-20 08:49:52 +08:00
|
|
|
|
2015-09-20 07:24:17 +08:00
|
|
|
Vector2 prevX = Vector2.zero;
|
|
|
|
Vector2 prevY = Vector2.zero;
|
|
|
|
Vector2 uv0 = new Vector2(0, 0);
|
|
|
|
Vector2 uv1 = new Vector2(0, 1);
|
|
|
|
Vector2 uv2 = new Vector2(1, 1);
|
|
|
|
Vector2 uv3 = new Vector2(1, 0);
|
|
|
|
Vector2 pos0;
|
|
|
|
Vector2 pos1;
|
|
|
|
Vector2 pos2;
|
|
|
|
Vector2 pos3;
|
2016-05-17 20:26:50 +08:00
|
|
|
|
|
|
|
if (FixedToSegments)
|
2015-09-20 07:24:17 +08:00
|
|
|
{
|
2017-02-11 00:45:50 +08:00
|
|
|
float f = (this.m_fillPercent / 100f);
|
|
|
|
float degrees = 360f / m_segments;
|
|
|
|
int fa = (int)((m_segments + 1) * f);
|
2016-05-17 20:26:50 +08:00
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < fa; i++)
|
2015-09-20 07:24:17 +08:00
|
|
|
{
|
2016-05-17 20:26:50 +08:00
|
|
|
float rad = Mathf.Deg2Rad * (i * degrees);
|
|
|
|
float c = Mathf.Cos(rad);
|
|
|
|
float s = Mathf.Sin(rad);
|
|
|
|
|
|
|
|
uv0 = new Vector2(0, 1);
|
|
|
|
uv1 = new Vector2(1, 1);
|
|
|
|
uv2 = new Vector2(1, 0);
|
|
|
|
uv3 = new Vector2(0, 0);
|
|
|
|
|
|
|
|
StepThroughPointsAndFill(outer, inner, ref prevX, ref prevY, out pos0, out pos1, out pos2, out pos3, c, s);
|
|
|
|
|
|
|
|
vh.AddUIVertexQuad(SetVbo(new[] { pos0, pos1, pos2, pos3 }, new[] { uv0, uv1, uv2, uv3 }));
|
2015-09-20 07:24:17 +08:00
|
|
|
}
|
2016-05-17 20:26:50 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
float tw = rectTransform.rect.width;
|
|
|
|
float th = rectTransform.rect.height;
|
|
|
|
|
2017-02-11 00:45:50 +08:00
|
|
|
float angleByStep = (m_fillPercent / 100f * (Mathf.PI * 2f)) / m_segments;
|
2016-05-17 20:26:50 +08:00
|
|
|
float currentAngle = 0f;
|
2017-02-11 00:45:50 +08:00
|
|
|
for (int i = 0; i < m_segments + 1; i++)
|
2015-09-20 07:24:17 +08:00
|
|
|
{
|
2016-05-17 20:26:50 +08:00
|
|
|
|
|
|
|
float c = Mathf.Cos(currentAngle);
|
|
|
|
float s = Mathf.Sin(currentAngle);
|
|
|
|
|
|
|
|
StepThroughPointsAndFill(outer, inner, ref prevX, ref prevY, out pos0, out pos1, out pos2, out pos3, c, s);
|
|
|
|
|
|
|
|
uv0 = new Vector2(pos0.x / tw + 0.5f, pos0.y / th + 0.5f);
|
|
|
|
uv1 = new Vector2(pos1.x / tw + 0.5f, pos1.y / th + 0.5f);
|
|
|
|
uv2 = new Vector2(pos2.x / tw + 0.5f, pos2.y / th + 0.5f);
|
|
|
|
uv3 = new Vector2(pos3.x / tw + 0.5f, pos3.y / th + 0.5f);
|
|
|
|
|
|
|
|
vh.AddUIVertexQuad(SetVbo(new[] { pos0, pos1, pos2, pos3 }, new[] { uv0, uv1, uv2, uv3 }));
|
|
|
|
|
|
|
|
currentAngle += angleByStep;
|
2015-09-20 07:24:17 +08:00
|
|
|
}
|
2015-09-20 08:49:52 +08:00
|
|
|
}
|
2015-09-20 07:24:17 +08:00
|
|
|
}
|
2016-05-17 20:26:50 +08:00
|
|
|
|
|
|
|
private void StepThroughPointsAndFill(float outer, float inner, ref Vector2 prevX, ref Vector2 prevY, out Vector2 pos0, out Vector2 pos1, out Vector2 pos2, out Vector2 pos3, float c, float s)
|
|
|
|
{
|
|
|
|
pos0 = prevX;
|
|
|
|
pos1 = new Vector2(outer * c, outer * s);
|
|
|
|
|
2017-02-11 00:45:50 +08:00
|
|
|
if (m_fill)
|
2016-05-17 20:26:50 +08:00
|
|
|
{
|
|
|
|
pos2 = Vector2.zero;
|
|
|
|
pos3 = Vector2.zero;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pos2 = new Vector2(inner * c, inner * s);
|
|
|
|
pos3 = prevY;
|
|
|
|
}
|
|
|
|
|
|
|
|
prevX = pos1;
|
|
|
|
prevY = pos2;
|
|
|
|
}
|
2016-05-25 07:50:10 +08:00
|
|
|
|
2015-09-20 07:24:17 +08:00
|
|
|
}
|
|
|
|
}
|