• 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
70
Question by qJake · Apr 09, 2010 at 10:17 AM · guitimerprogress-barcircular

Creating a Circular Progressbar / Timer

I'd like to know how to make a circular progressbar or timer-type effect. Basically, given a value [0..1], it will "fill in" a portion of a circle on the GUI that circularly represents the value. So if I give it 0.33, it should fill in 1/3 of the circle, and so on. These types of GUI elements are useful for timers and that sort of thing.

I really can't think of a way to do this off the top of my head, so if anyone can either post some code or point me in the right direction, that would be awesome. :)

Comment
Add comment · Show 3
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
avatar image nikhil38 · Feb 23, 2014 at 05:59 AM 0
Share

hers' my workaround . http://codeskool.blogspot.in/p/unity-hacks.html

avatar image robertbu nikhil38 · Feb 23, 2014 at 06:15 AM 0
Share

Similar answer using GUI.DrawTexture:

http://answers.unity3d.com/questions/394850/make-a-circular-progress-bar-that-changes-state-up.html

avatar image Noob_Vulcan · Aug 09, 2015 at 10:09 AM 2
Share

In Unity 5 You need Image component with "Image type" parameter set to "Filled". Choose "Fill method" Radial 360. Last thing that you need to change parameter "Fill Amount" by script.

for script you can refer http://www.unityrealm.com/how-to-make-a-circular-progress-bar-in-unity-ui/

6 Replies

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

Answer by Eric5h5 · Apr 09, 2010 at 05:38 PM

The easiest way is to use an alpha gradient texture:

innergradient

Which is used as a mask in an alpha test (transparent cutout) shader. You change the _Cutoff property to indicate which percentage should be used, ranging from 0..1. This code would make the bar empty when the mouse pointer is on the right side of the screen, going up to full as you move the mouse pointer to the left side (see demo):

function Update () { 
   renderer.material.SetFloat("_Cutoff", Mathf.InverseLerp(0, Screen.width, Input.mousePosition.x)); 
}

In the demo, there are three textures; one for the regular graphics (using normal alpha for nice anti-aliasing), one for the green health bar (using alpha cutoff), and a solid red behind the first two that shows through where there's no green. The non-alpha part of the health bar texture is just a block of solid green, although it doesn't have to be...you can make it look like whatever you want of course.

alt text

A unitypackage of this is available here.

Comment
Add comment · Show 40 · 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
avatar image Ashkan_gc · May 03, 2010 at 04:26 PM 0
Share

great answer eric! do you use shaders much? do you know shader program$$anonymous$$g?

avatar image Eric5h5 · May 05, 2010 at 08:48 PM 0
Share

A little, but you don't need to know any shader program$$anonymous$$g for this, just use a transparent cutout shader.

avatar image Wolfram · Aug 04, 2010 at 05:19 PM 0
Share

Just use material instances to be able to change the cutoff value for each button individually.

avatar image Eric5h5 · Aug 04, 2010 at 06:33 PM 0
Share

Indeed, material instances is far simpler than extra cameras/rendertextures.

avatar image Eric5h5 · Aug 05, 2010 at 04:59 AM 0
Share

@$$anonymous$$olix: Oh, OnGUI, sorry. OnGUI isn't really suited for this, so I wouldn't use it.

Show more comments
avatar image
28

Answer by gilley033 · Dec 16, 2014 at 08:55 PM

FYI this can be done very easily with the new UI. All you need is a sprite with a circular texture (I've provided one). Add an Image object to your scene, and the provided sprite as the Source Image, and set the Image Type to Filled.

You can access the fill amount via scripting by getting a reference to the Image component (make sure to add import or add a using statement for UnityEngine.UI).

Enjoy!alt text


circulartimer.png (96.3 kB)
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
avatar image
9

Answer by Noob_Vulcan · Aug 09, 2015 at 10:09 AM

2019 ...

This is now built in to Unity. Image -> "Filled" -> Radial 360.

Just change "Fill Amount" ( .fillAmount in script).

alt text

alt text

Use any image you wish.

[1]: http://www.unityrealm.com/how-to-make-a-circular-progress-bar-in-unity-ui


screenshot-1.png (84.4 kB)
screenshot.png (68.0 kB)
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
avatar image
2

Answer by plkost · Jun 19, 2014 at 01:49 PM

A more mathematical approach to this problem is:

Render lines that correspond to the part of your circle you want each time.

Here's what I 've done using the GL class:

 using UnityEngine; 
 using System.Collections; 
 
 public class timer : MonoBehaviour
     { 
     //assign a material for your lines 
     public Material lineMat; 
     public float time; 
     private float _timer; 
     private float length=0.5f; 
     private Vector3 timerpos; 
     float x,y,delta; 
 
     void Start()
         { 
         //Initialize timerPos here 
         delta = 0.02f; 
         CreateMat (); 
         } 
 
     void Update()
         { 
         if (time - _timer > 0) 
         _timer += Time.deltaTime; 
         } 
 
     void OnPostRender()
         { 
         if (time - _timer > 0)
             { 
             for (float angle = 0; angle <(_timer/time) * 2*Mathf.PI; angle+=delta)
                 { 
                 x = length * Mathf.Cos (-angle + Mathf.PI / 2 ); 
                 y = length * Mathf.Sin (-angle + Mathf.PI / 2 ); 
                 lineMaterial.SetPass(0); 
                 GL.Begin (1); 
                 GL.Color (Color.white); 
                 GL.Vertex3 (timerpos.x, timerpos.y, 0); 
                 GL.Vertex3 (timerpos.x + x,timerpos.y + y, 0); 
                 GL.End (); 
                 } 
             } 
         } 
     } 
 // end



For anti-clockwise

 x = length * Mathf.Cos (angle + Mathf.PI / 2 ); 
 y = length * Mathf.Sin (angle + Mathf.PI / 2 ); 


The Mathf.PI / 2 is added so that the timer starts from 12 'o clock

P.S I am not sure if this code is so fast or not but it gets the work done

Comment
Add comment · Show 1 · 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
avatar image Fattie · Jan 10, 2019 at 03:29 PM 0
Share

i fixed the formatting on this great answer!

avatar image
0

Answer by ThomasKang · Oct 08, 2020 at 10:03 PM

Unity has since added the Sprite Mask component which works very similarly to the alpha cutout shader @Eric5h5 posted. https://docs.unity3d.com/Manual/class-SpriteMask.html

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
  • 1
  • 2
  • ›

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

23 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

Related Questions

Jaggedness on circular spinner 2 Answers

Fill a Circular Progress Bar Based on Value 1 Answer

GUI Progress ? 2 Answers

Timer progress bar 0 Answers

Circular Progress Bar/Meter - how to generate smooth alpha channel for cutout? 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