- Home /
Redraw canvas graphic on Update()
using UnityEngine;
using UnityEngine.UI;
public class DrawLoad : Graphic
{
protected override void OnPopulateMesh(VertexHelper vh)
{
//==========================
float r = rectTransform.rect.height / 2.0f;
//float widthFactor = (rectTransform.rect.width / 2.0f) - r;
float y = r;
float x = 0.0f;
int t = 20; // t = total vertices
float a = 360.0f / (t-1); // angle delta/differences between each vertex
float aC = 0.0f; // incrementing angles from 0 --> 360
int maxi = t - 1;
Vector2[] vtR = new Vector2[t];
UIVertex[] uivR = new UIVertex[t];
vh.Clear();
for (int i = 0; i <= maxi; i++)
{
y = r * Mathf.Cos(Mathf.Deg2Rad * aC);
x = r * Mathf.Sin(Mathf.Deg2Rad * aC);
vtR[i] = new Vector2(x, y);
aC += a;
uivR[i] = UIVertex.simpleVert;
uivR[i].position = vtR[i];//0,0
uivR[i].color = Color.green;
vh.AddVert(uivR[i]);
}
// ======== add center point ========
Vector2 center = new Vector2(0.0f, 0.0f);
UIVertex c = UIVertex.simpleVert;
c.position = center;//0,0
c.color = Color.green;
vh.AddVert(c);
for (int i = 0; i <= maxi; i++)
{
vh.AddTriangle(t, i, i + 1);// use indexes, don't use count, start from 0
}
for (int i = 0; i < uivR.Length; i++)
{
uivR[i].color = Color.green;
vh.SetUIVertex(uivR[i],i);
}
}
}
Hi, I wish to change the color dynamically of each assigned triangles. I have these code modified from Unity documentation and I tried the SetUIVertex method but it doesn't seems to work. This is actually a circle made up from every piece of triangles. What is the proper way to redraw the vertices. I want to make it in MonoBehavior.Update(). Any helps would be greatly appreciated. Thanks.
Answer by elliotching · Mar 20, 2018 at 02:16 PM
Found the solution:
The shorst answer is using SetAllDirty()
will do.
A better alternative:
is using others' source code/utility to draw shape and update circle (unfortunately).
> Go to bitbucket.org/UnityUIExtensions and download the .unitypackage
> Go to your Unity project and Import the package
> Don't need to import everything but import only
1. unity-ui-extensions > Scripts > Primitives > UICircle.cs
2. unity-ui-extensions > Scripts > Primitives > UIPrimitiveBase.cs
3. unity-ui-extensions > Scripts > Utility > SetPropertyUtility.cs
> Create an UI image under canvas
> Remove the Image Component
> Assign "UICircle.cs" into Image object
> In you game Update()
script, assign values like GameObject.Find("Image").GetComponent<UICircle>().FIllPercent -= 1
> will dynamically change the circle draw percentage.
Your answer
![](https://koobas.hobune.stream/wayback/20220612154844im_/https://answers.unity.com/themes/thub/images/avi.jpg)