Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by elliotching · Mar 18, 2018 at 05:15 PM · uigraphics

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

139 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make CanvasRenderer.SetMesh appear in the same frame? 1 Answer

How to prevent mouse lag/latency for custom cursor? 0 Answers

Best technique to create the UI menus like in zombieville usa 2 on mobile platforms? 0 Answers

Trying to add Image for graphic rebuild while we are already inside a graphic rebuild loop. This is not supported. 3 Answers

Rendering problem on Android 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges