2015-09-20 08:21:29 +08:00
|
|
|
/// Credit zge
|
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
|
|
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
namespace UnityEngine.UI.Extensions
|
2015-09-20 08:21:29 +08:00
|
|
|
{
|
|
|
|
[AddComponentMenu("UI/Extensions/Primatives/UI Circle")]
|
2015-09-20 07:24:17 +08:00
|
|
|
public class UICircle : Graphic
|
|
|
|
{
|
|
|
|
[SerializeField]
|
|
|
|
Texture m_Texture;
|
|
|
|
[Range(0, 100)]
|
|
|
|
public int fillPercent = 100;
|
|
|
|
public bool fill = true;
|
|
|
|
public float thickness = 5;
|
|
|
|
[Range(0, 360)]
|
|
|
|
public int segments = 360;
|
2015-09-20 08:21:29 +08:00
|
|
|
|
2015-09-20 07:24:17 +08:00
|
|
|
public override Texture mainTexture
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return m_Texture == null ? s_WhiteTexture : m_Texture;
|
|
|
|
}
|
|
|
|
}
|
2015-09-20 08:21:29 +08:00
|
|
|
|
2015-09-20 07:24:17 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Texture to be used.
|
|
|
|
/// </summary>
|
|
|
|
public Texture texture
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return m_Texture;
|
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (m_Texture == value)
|
|
|
|
return;
|
2015-09-20 08:21:29 +08:00
|
|
|
|
2015-09-20 07:24:17 +08:00
|
|
|
m_Texture = value;
|
|
|
|
SetVerticesDirty();
|
|
|
|
SetMaterialDirty();
|
|
|
|
}
|
|
|
|
}
|
2015-09-20 08:21:29 +08:00
|
|
|
|
2015-09-20 07:24:17 +08:00
|
|
|
protected void SetVbo(List<UIVertex> vbo, Vector2[] vertices, Vector2[] uvs)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < vertices.Length; i++)
|
|
|
|
{
|
|
|
|
var vert = UIVertex.simpleVert;
|
|
|
|
vert.color = color;
|
|
|
|
vert.position = vertices[i];
|
|
|
|
vert.uv0 = uvs[i];
|
|
|
|
vbo.Add(vert);
|
|
|
|
}
|
|
|
|
}
|
2015-09-20 08:21:29 +08:00
|
|
|
|
2015-09-20 07:24:17 +08:00
|
|
|
protected override void OnFillVBO(List<UIVertex> vbo)
|
|
|
|
{
|
|
|
|
float outer = -rectTransform.pivot.x * rectTransform.rect.width;
|
|
|
|
float inner = -rectTransform.pivot.x * rectTransform.rect.width + this.thickness;
|
2015-09-20 08:21:29 +08:00
|
|
|
|
2015-09-20 07:24:17 +08:00
|
|
|
vbo.Clear();
|
2015-09-20 08:21:29 +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;
|
2015-09-20 08:21:29 +08:00
|
|
|
|
2015-09-20 07:24:17 +08:00
|
|
|
float f = (this.fillPercent / 100f);
|
|
|
|
float degrees = 360f / segments;
|
|
|
|
int fa = (int)((segments + 1) * f);
|
2015-09-20 08:21:29 +08:00
|
|
|
|
|
|
|
|
2015-09-20 07:24:17 +08:00
|
|
|
for (int i = 0; i < fa; i++)
|
|
|
|
{
|
|
|
|
float rad = Mathf.Deg2Rad * (i * degrees);
|
|
|
|
float c = Mathf.Cos(rad);
|
|
|
|
float s = Mathf.Sin(rad);
|
2015-09-20 08:21:29 +08:00
|
|
|
|
2015-09-20 07:24:17 +08:00
|
|
|
uv0 = new Vector2(0, 1);
|
|
|
|
uv1 = new Vector2(1, 1);
|
|
|
|
uv2 = new Vector2(1, 0);
|
|
|
|
uv3 = new Vector2(0, 0);
|
2015-09-20 08:21:29 +08:00
|
|
|
|
2015-09-20 07:24:17 +08:00
|
|
|
pos0 = prevX;
|
|
|
|
pos1 = new Vector2(outer * c, outer * s);
|
2015-09-20 08:21:29 +08:00
|
|
|
|
2015-09-20 07:24:17 +08:00
|
|
|
if (fill)
|
|
|
|
{
|
|
|
|
pos2 = Vector2.zero;
|
|
|
|
pos3 = Vector2.zero;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pos2 = new Vector2(inner * c, inner * s);
|
|
|
|
pos3 = prevY;
|
|
|
|
}
|
2015-09-20 08:21:29 +08:00
|
|
|
|
2015-09-20 07:24:17 +08:00
|
|
|
prevX = pos1;
|
|
|
|
prevY = pos2;
|
2015-09-20 08:21:29 +08:00
|
|
|
|
2015-09-20 07:24:17 +08:00
|
|
|
SetVbo(vbo, new[] { pos0, pos1, pos2, pos3 }, new[] { uv0, uv1, uv2, uv3 });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|