Tanoda
UICircle.cs
Go to the documentation of this file.
1
4
19
20using System.Collections.Generic;
21
23{
24 [AddComponentMenu("UI/Extensions/Primitives/UI Circle")]
26 {
27 [Tooltip("The Arc Invert property will invert the construction of the Arc.")]
28 public bool ArcInvert = true;
29
30 [Tooltip("The Arc property is a percentage of the entire circumference of the circle.")]
31 [Range(0, 1)]
32 public float Arc = 1;
33
34 [Tooltip("The Arc Steps property defines the number of segments that the Arc will be divided into.")]
35 [Range(0, 1000)]
36 public int ArcSteps = 100;
37
38 [Tooltip("The Arc Rotation property permits adjusting the geometry orientation around the Z axis.")]
39 [Range(0, 360)]
40 public int ArcRotation = 0;
41
42 [Tooltip("The Progress property allows the primitive to be used as a progression indicator.")]
43 [Range(0, 1)]
44 public float Progress = 0;
45 private float _progress = 0;
46
47 public Color ProgressColor = new Color(255, 255, 255, 255);
48 public bool Fill = true; //solid circle
49 public float Thickness = 5;
50 public int Padding = 0;
51
52 private List<int> indices = new List<int>(); //ordered list of vertices per tri
53 private List<UIVertex> vertices = new List<UIVertex>();
54 private Vector2 uvCenter = new Vector2(0.5f, 0.5f);
55
56 protected override void OnPopulateMesh(VertexHelper vh)
57 {
58 int _inversion = ArcInvert ? -1 : 1;
59 float Diameter = (rectTransform.rect.width < rectTransform.rect.height ? rectTransform.rect.width : rectTransform.rect.height) - Padding; //correct for padding and always fit RectTransform
60 float outerDiameter = -rectTransform.pivot.x * Diameter;
61 float innerDiameter = -rectTransform.pivot.x * Diameter + Thickness;
62
63 vh.Clear();
64 indices.Clear();
65 vertices.Clear();
66
67 int i = 0;
68 int j = 1;
69 int k = 0;
70
71 float stepDegree = (Arc * 360f) / ArcSteps;
72 _progress = ArcSteps * Progress;
73 float rad = _inversion * Mathf.Deg2Rad * ArcRotation;
74 float X = Mathf.Cos(rad);
75 float Y = Mathf.Sin(rad);
76
77 var vertex = UIVertex.simpleVert;
78 vertex.color = _progress > 0 ? ProgressColor : color;
79
80 //initial vertex
81 vertex.position = new Vector2(outerDiameter * X, outerDiameter * Y);
82 vertex.uv0 = new Vector2(vertex.position.x / Diameter + 0.5f, vertex.position.y / Diameter + 0.5f);
83 vertices.Add(vertex);
84
85 var iV = new Vector2(innerDiameter * X, innerDiameter * Y);
86 if (Fill) iV = Vector2.zero; //center vertex to pivot
87 vertex.position = iV;
88 vertex.uv0 = Fill ? uvCenter : new Vector2(vertex.position.x / Diameter + 0.5f, vertex.position.y / Diameter + 0.5f);
89 vertices.Add(vertex);
90
91 for (int counter = 1; counter <= ArcSteps; counter++)
92 {
93 rad = _inversion * Mathf.Deg2Rad * (counter * stepDegree + ArcRotation);
94 X = Mathf.Cos(rad);
95 Y = Mathf.Sin(rad);
96
97 vertex.color = counter > _progress ? color : ProgressColor;
98 vertex.position = new Vector2(outerDiameter * X, outerDiameter * Y);
99 vertex.uv0 = new Vector2(vertex.position.x / Diameter + 0.5f, vertex.position.y / Diameter + 0.5f);
100 vertices.Add(vertex);
101
102 //add additional vertex if required and generate indices for tris in clockwise order
103 if (!Fill)
104 {
105 vertex.position = new Vector2(innerDiameter * X, innerDiameter * Y);
106 vertex.uv0 = new Vector2(vertex.position.x / Diameter + 0.5f, vertex.position.y / Diameter + 0.5f);
107 vertices.Add(vertex);
108 k = j;
109 indices.Add(i);
110 indices.Add(j + 1);
111 indices.Add(j);
112 j++;
113 i = j;
114 j++;
115 indices.Add(i);
116 indices.Add(j);
117 indices.Add(k);
118 }
119 else
120 {
121 indices.Add(i);
122 indices.Add(j + 1);
123 //Fills (solid circle) with progress require an additional vertex to
124 // prevent the base circle from becoming a gradient from center to edge
125 if (counter > _progress)
126 {
127 indices.Add(ArcSteps + 2);
128 }
129 else
130 {
131 indices.Add(1);
132 }
133
134 j++;
135 i = j;
136 }
137 }
138
139 //this vertex is added to the end of the list to simplify index ordering on geometry fill
140 if (Fill)
141 {
142 vertex.position = iV;
143 vertex.color = color;
144 vertex.uv0 = uvCenter;
145 vertices.Add(vertex);
146 }
147 vh.AddUIVertexStream(vertices, indices);
148 }
149
150 //the following methods may be used during run-time
151 //to update the properties of the component
152 public void SetProgress(float progress)
153 {
154 Progress = progress;
155 SetVerticesDirty();
156 }
157
158 public void SetArcSteps(int steps)
159 {
160 ArcSteps = steps;
161 SetVerticesDirty();
162 }
163
164 public void SetInvertArc(bool invert)
165 {
166 ArcInvert = invert;
167 SetVerticesDirty();
168 }
169
170 public void SetArcRotation(int rotation)
171 {
172 ArcRotation = rotation;
173 SetVerticesDirty();
174 }
175
176 public void SetFill(bool fill)
177 {
178 Fill = fill;
179 SetVerticesDirty();
180 }
181
182 public void SetBaseColor(Color color)
183 {
184 this.color = color;
185 SetVerticesDirty();
186 }
187
188 public void UpdateBaseAlpha(float value)
189 {
190 var _color = this.color;
191 _color.a = value;
192 this.color = _color;
193 SetVerticesDirty();
194 }
195
196 public void SetProgressColor(Color color)
197 {
198 ProgressColor = color;
199 SetVerticesDirty();
200 }
201
202 public void UpdateProgressAlpha(float value)
203 {
204 ProgressColor.a = value;
205 SetVerticesDirty();
206 }
207
208 public void SetPadding(int padding)
209 {
210 Padding = padding;
211 SetVerticesDirty();
212 }
213
214 public void SetThickness(int thickness)
215 {
216 Thickness = thickness;
217 SetVerticesDirty();
218 }
219 }
220}
UnityEngine.Color Color
Definition: TestScript.cs:32
void SetArcRotation(int rotation)
Definition: UICircle.cs:170
void UpdateBaseAlpha(float value)
Definition: UICircle.cs:188
void SetPadding(int padding)
Definition: UICircle.cs:208
void SetThickness(int thickness)
Definition: UICircle.cs:214
void SetBaseColor(Color color)
Definition: UICircle.cs:182
void SetProgress(float progress)
Definition: UICircle.cs:152
override void OnPopulateMesh(VertexHelper vh)
Definition: UICircle.cs:56
void SetProgressColor(Color color)
Definition: UICircle.cs:196
void UpdateProgressAlpha(float value)
Definition: UICircle.cs:202
void SetInvertArc(bool invert)
Definition: UICircle.cs:164
Credit Erdener Gonenc - @PixelEnvision.