Merged in tswalker/unity-ui-extensions (pull request #20)

Major changes to UICircle with some member modifications and additions to the component behaviour.
pull/413/head
Troy Walker 2017-10-19 12:15:59 +00:00 committed by Simon Jackson
commit 731961fe22
10 changed files with 418 additions and 113 deletions

View File

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: d85457b4b0625c6448836b062a4b33a2
folderAsset: yes
timeCreated: 1507492783
licenseType: Pro
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,69 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI.Extensions;
public class ChangeColor : MonoBehaviour
{
public GameObject TargetUICircle;
private Color baseColor;
private Color progressColor;
private float r, g, b = 0;
private float factor = 1536.145f;
private void Awake()
{
baseColor = TargetUICircle.GetComponent<UICircle>().color;
progressColor = TargetUICircle.GetComponent<UICircle>().ProgressColor;
}
public void UpdateBaseColor(float value)
{
baseColor = SetFixedColor(value, baseColor.a);
TargetUICircle.GetComponent<UICircle>().color = baseColor;
}
public void UpdateProgressColor(float value)
{
progressColor = SetFixedColor(value, progressColor.a);
TargetUICircle.GetComponent<UICircle>().SetProgressColor(progressColor);
}
private Color SetFixedColor(float value,float alpha)
{
if (value <= 0.166f)
{
g = 0;
r = 255f;
b = 255f - (255f - (value * factor));
}else if(value <= 0.332f)
{
g = 0;
r = 255f - (255f - ((0.332f - value)*factor));
b = 255f;
}else if(value <= 0.498f)
{
g = 255f - (255f - ((0.498f - value) * factor));
r = 0f;
b = 255f;
}else if(value <= 0.664f)
{
g = 255f;
r = 0f;
b = 255f - (255f - ((0.664f - value) * factor));
}else if(value <= 0.83f)
{
g = 255f;
r = 255f - (255f - ((0.83f - value) * factor));
b = 0;
}else
{
g = 255f - (255f - ((1 - value) * factor));
r = 255f;
b = 0;
}
return new Color(r, g, b, alpha);
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 7ae29b862df56504fac8133305afea62
timeCreated: 1507577225
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,30 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.UI.Extensions;
public class ChangeDensity : MonoBehaviour
{
public GameObject MultiColorObject;
public GameObject TextOutputObject;
private UICircle _uiCircleComponent;
private Text _densityOutput;
private void Awake()
{
_uiCircleComponent = MultiColorObject.GetComponent<UICircle>();
_densityOutput = TextOutputObject.GetComponent<Text>();
}
private void OnEnable()
{
_densityOutput.text = _uiCircleComponent.ArcSteps.ToString();
}
public void UpdateDensity(float value)
{
_uiCircleComponent.SetArcSteps((int)value);
_densityOutput.text = value.ToString();
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 970b2e1ba28dfa64e8fc65acb4f7e37c
timeCreated: 1507668540
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

Binary file not shown.

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 79f0a411768319c48801b886b4530fb9
timeCreated: 1507494554
licenseType: Pro
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

Binary file not shown.

View File

@ -0,0 +1,92 @@
fileFormatVersion: 2
guid: 203e0bdef973df44ba779da032273050
timeCreated: 1507498573
licenseType: Pro
TextureImporter:
fileIDToRecycleName: {}
serializedVersion: 4
mipmaps:
mipMapMode: 0
enableMipMap: 0
sRGBTexture: 1
linearTexture: 0
fadeOut: 0
borderMipMap: 0
mipMapFadeDistanceStart: 1
mipMapFadeDistanceEnd: 3
bumpmap:
convertToNormalMap: 0
externalNormalMap: 0
heightScale: 0.25
normalMapFilter: 0
isReadable: 0
grayScaleToAlpha: 0
generateCubemap: 6
cubemapConvolution: 0
seamlessCubemap: 0
textureFormat: 1
maxTextureSize: 2048
textureSettings:
filterMode: -1
aniso: -1
mipBias: -1
wrapMode: 1
nPOTScale: 0
lightmap: 0
compressionQuality: 50
spriteMode: 1
spriteExtrude: 1
spriteMeshType: 1
alignment: 0
spritePivot: {x: 0.5, y: 0.5}
spriteBorder: {x: 0, y: 0, z: 0, w: 0}
spritePixelsToUnits: 100
alphaUsage: 1
alphaIsTransparency: 1
spriteTessellationDetail: -1
textureType: 8
textureShape: 1
maxTextureSizeSet: 0
compressionQualitySet: 0
textureFormatSet: 0
platformSettings:
- buildTarget: DefaultTexturePlatform
maxTextureSize: 2048
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
- buildTarget: Standalone
maxTextureSize: 2048
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
- buildTarget: iPhone
maxTextureSize: 2048
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
- buildTarget: Windows Store Apps
maxTextureSize: 2048
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
spriteSheet:
serializedVersion: 2
sprites: []
outline: []
spritePackingTag:
userData:
assetBundleName:
assetBundleVariant:

View File

@ -2,146 +2,219 @@
/// Sourced from - http://forum.unity3d.com/threads/draw-circles-or-primitives-on-the-new-ui-canvas.272488/#post-2293224 /// Sourced from - http://forum.unity3d.com/threads/draw-circles-or-primitives-on-the-new-ui-canvas.272488/#post-2293224
/// Updated from - https://bitbucket.org/ddreaper/unity-ui-extensions/issues/65/a-better-uicircle /// Updated from - https://bitbucket.org/ddreaper/unity-ui-extensions/issues/65/a-better-uicircle
/// Update 10.9.2017 (tswalker, https://bitbucket.org/tswalker/)
///
/// * Modified component to utilize vertex stream instead of quads
/// * Improved accuracy of geometry fill to prevent edge "sliding" and redundant tris
/// * Added progress capability to allow component to be used as an indicator
/// * Added methods for use during runtime and event system(s) with other components
/// * Change some terminology of members to reflect other component property changes
/// * Added padding capability
/// * Only utilizes UV0 set for sprite/texture mapping (maps UV to geometry 0,1 boundary)
/// * Sample usage in scene "UICircleProgress"
/// Note: moving the pivot around from center to an edge can cause strange things
/// as well as having the RectTransform be smaller than the Thickness and/or Padding.
/// When making an initial layout for the component, it would be best to test multiple
/// aspect ratios and resolutions to ensure consistent behaviour.
using System.Collections.Generic;
namespace UnityEngine.UI.Extensions namespace UnityEngine.UI.Extensions
{ {
[AddComponentMenu("UI/Extensions/Primitives/UI Circle")] [AddComponentMenu("UI/Extensions/Primitives/UI Circle")]
public class UICircle : UIPrimitiveBase public class UICircle : UIPrimitiveBase
{ {
[Tooltip("The circular fill percentage of the primitive, affected by FixedToSegments")] [Tooltip("The Arc Invert property will invert the construction of the Arc.")]
[Range(0, 100)] public bool ArcInvert = true;
[SerializeField]
private int m_fillPercent = 100; [Tooltip("The Arc property is a percentage of the entire circumference of the circle.")]
[Tooltip("Should the primitive fill draw by segments or absolute percentage")] [Range(0, 1)]
public bool FixedToSegments = false; public float Arc = 1;
[Tooltip("Draw the primitive filled or as a line")]
[SerializeField] [Tooltip("The Arc Steps property defines the number of segments that the Arc will be divided into.")]
private bool m_fill = true; [Range(0, 1000)]
[Tooltip("If not filled, the thickness of the primitive line")] public int ArcSteps = 100;
[SerializeField]
private float m_thickness = 5; [Tooltip("The Arc Rotation property permits adjusting the geometry orientation around the Z axis.")]
[Tooltip("The number of segments to draw the primitive, more segments = smoother primitive")]
[Range(0, 360)] [Range(0, 360)]
[SerializeField] public int ArcRotation = 0;
private int m_segments = 360;
[Tooltip("The Progress property allows the primitive to be used as a progression indicator.")]
[Range(0, 1)]
public float Progress = 0;
private float _progress = 0;
public int FillPercent public Color ProgressColor = new Color(255, 255, 255, 255);
{ public bool Fill = true; //solid circle
get { return m_fillPercent; } public float Thickness = 5;
set { m_fillPercent = value; SetAllDirty(); } public int Padding = 0;
}
public bool Fill private List<int> indices = new List<int>(); //ordered list of vertices per tri
{ private List<UIVertex> vertices = new List<UIVertex>();
get { return m_fill; } private Vector2 uvCenter = new Vector2(0.5f, 0.5f);
set { m_fill = value; SetAllDirty(); }
}
public float Thickness
{
get { return m_thickness; }
set { m_thickness = value; SetAllDirty(); }
}
void Update()
{
this.m_thickness = (float)Mathf.Clamp(this.m_thickness, 0, rectTransform.rect.width / 2);
}
public int Segments
{
get { return m_segments; }
set { m_segments = value; SetAllDirty(); }
}
protected override void OnPopulateMesh(VertexHelper vh) protected override void OnPopulateMesh(VertexHelper vh)
{ {
float outer = -rectTransform.pivot.x * rectTransform.rect.width; int _inversion = ArcInvert ? -1 : 1;
float inner = -rectTransform.pivot.x * rectTransform.rect.width + this.m_thickness; float Diameter = (rectTransform.rect.width < rectTransform.rect.height ? rectTransform.rect.width : rectTransform.rect.height) - Padding; //correct for padding and always fit RectTransform
float outerDiameter = -rectTransform.pivot.x * Diameter;
float innerDiameter = -rectTransform.pivot.x * Diameter + Thickness;
vh.Clear(); vh.Clear();
indices.Clear();
vertices.Clear();
Vector2 prevX = Vector2.zero; int i = 0;
Vector2 prevY = Vector2.zero; int j = 1;
Vector2 uv0 = new Vector2(0, 0); int k = 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;
if (FixedToSegments) float stepDegree = (Arc * 360f) / ArcSteps;
_progress = ArcSteps * Progress;
float rad = _inversion * Mathf.Deg2Rad * ArcRotation;
float X = Mathf.Cos(rad);
float Y = Mathf.Sin(rad);
var vertex = UIVertex.simpleVert;
vertex.color = _progress > 0 ? ProgressColor : color;
//initial vertex
vertex.position = new Vector2(outerDiameter * X, outerDiameter * Y);
vertex.uv0 = new Vector2(vertex.position.x / Diameter + 0.5f, vertex.position.y / Diameter + 0.5f);
vertices.Add(vertex);
var iV = new Vector2(innerDiameter * X, innerDiameter * Y);
if (Fill) iV = Vector2.zero; //center vertex to pivot
vertex.position = iV;
vertex.uv0 = Fill ? uvCenter : new Vector2(vertex.position.x / Diameter + 0.5f, vertex.position.y / Diameter + 0.5f);
vertices.Add(vertex);
for (int counter = 1; counter <= ArcSteps; counter++)
{ {
float f = (this.m_fillPercent / 100f); rad = _inversion * Mathf.Deg2Rad * (counter * stepDegree + ArcRotation);
float degrees = 360f / m_segments; X = Mathf.Cos(rad);
int fa = (int)((m_segments + 1) * f); Y = Mathf.Sin(rad);
vertex.color = counter > _progress ? color : ProgressColor;
vertex.position = new Vector2(outerDiameter * X, outerDiameter * Y);
vertex.uv0 = new Vector2(vertex.position.x / Diameter + 0.5f, vertex.position.y / Diameter + 0.5f);
vertices.Add(vertex);
for (int i = 0; i < fa; i++) //add additional vertex if required and generate indices for tris in clockwise order
if (!Fill)
{ {
float rad = Mathf.Deg2Rad * (i * degrees); vertex.position = new Vector2(innerDiameter * X, innerDiameter * Y);
float c = Mathf.Cos(rad); vertex.uv0 = new Vector2(vertex.position.x / Diameter + 0.5f, vertex.position.y / Diameter + 0.5f);
float s = Mathf.Sin(rad); vertices.Add(vertex);
k = j;
uv0 = new Vector2(0, 1); indices.Add(i);
uv1 = new Vector2(1, 1); indices.Add(j + 1);
uv2 = new Vector2(1, 0); indices.Add(j);
uv3 = new Vector2(0, 0); j++;
i = j;
StepThroughPointsAndFill(outer, inner, ref prevX, ref prevY, out pos0, out pos1, out pos2, out pos3, c, s); j++;
indices.Add(i);
vh.AddUIVertexQuad(SetVbo(new[] { pos0, pos1, pos2, pos3 }, new[] { uv0, uv1, uv2, uv3 })); indices.Add(j);
} indices.Add(k);
} }
else else
{ {
float tw = rectTransform.rect.width; indices.Add(i);
float th = rectTransform.rect.height; indices.Add(j + 1);
//Fills (solid circle) with progress require an additional vertex to
float angleByStep = (m_fillPercent / 100f * (Mathf.PI * 2f)) / m_segments; // prevent the base circle from becoming a gradient from center to edge
float currentAngle = 0f; if (counter > _progress)
for (int i = 0; i < m_segments + 1; i++)
{ {
indices.Add(ArcSteps + 2);
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;
}
}
}
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);
if (m_fill)
{
pos2 = Vector2.zero;
pos3 = Vector2.zero;
} }
else else
{ {
pos2 = new Vector2(inner * c, inner * s); indices.Add(1);
pos3 = prevY;
} }
prevX = pos1; j++;
prevY = pos2; i = j;
}
} }
//this vertex is added to the end of the list to simplify index ordering on geometry fill
if (Fill)
{
vertex.position = iV;
vertex.color = color;
vertex.uv0 = uvCenter;
vertices.Add(vertex);
}
vh.AddUIVertexStream(vertices, indices);
}
//the following methods may be used during run-time
//to update the properties of the component
public void SetProgress(float progress)
{
Progress = progress;
SetVerticesDirty();
}
public void SetArcSteps(int steps)
{
ArcSteps = steps;
SetVerticesDirty();
}
public void SetInvertArc(bool invert)
{
ArcInvert = invert;
SetVerticesDirty();
}
public void SetArcRotation(int rotation)
{
ArcRotation = rotation;
SetVerticesDirty();
}
public void SetFill(bool fill)
{
Fill = fill;
SetVerticesDirty();
}
public void SetBaseColor(Color color)
{
this.color = color;
SetVerticesDirty();
}
public void UpdateBaseAlpha(float value)
{
var _color = this.color;
_color.a = value;
this.color = _color;
SetVerticesDirty();
}
public void SetProgressColor(Color color)
{
ProgressColor = color;
SetVerticesDirty();
}
public void UpdateProgressAlpha(float value)
{
ProgressColor.a = value;
SetVerticesDirty();
}
public void SetPadding(int padding)
{
Padding = padding;
SetVerticesDirty();
}
public void SetThickness(int thickness)
{
Thickness = thickness;
SetVerticesDirty();
}
} }
} }