x


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. :)

more ▼

asked Apr 09 '10 at 10:17 AM

qJake gravatar image

qJake
11.6k 43 78 161

it's incredibly simple to do this using Unity's built-in animation system. just spin one part on top of the other! have done this dozens of times in production games .. unless you mean something different???

May 28 '12 at 08:45 PM Fattie
(comments are locked)
10|3000 characters needed characters left

5 answers: sort voted first

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.

more ▼

answered Apr 09 '10 at 05:38 PM

Eric5h5 gravatar image

Eric5h5
80.1k 41 132 519

Absolutely the information I needed. Thank you!

Apr 10 '10 at 03:01 AM qJake

great answer eric! do you use shaders much? do you know shader programming?

May 03 '10 at 04:26 PM Ashkan_gc

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

May 05 '10 at 08:48 PM Eric5h5

To make this work on many buttons in OnGUI, would you need to set up a camera and poly on separate layers for each button, and render each to a RenderTexture? If you had, say, 50 of them, would that kill performance? (50 extra cameras, 50 extra render textures)

Aug 04 '10 at 02:25 PM Molix

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

Aug 04 '10 at 05:19 PM Wolfram
(comments are locked)
10|3000 characters needed characters left

That's a kind of involved process, because you can't just scale a circle. However, the Lerpz tutorial explains it. Basically, how it works is you have, say, 6 images arranged in a circle. Based on your variable, you would set the transparency of an image to be either 0 or 1, thus causing it to fade away, and make it look like a part of the circle has disappeared. For a background, you could just put a normal circle just underneath it.

more ▼

answered Apr 09 '10 at 12:02 PM

e.bonneville gravatar image

e.bonneville
5.7k 100 116 165

(comments are locked)
10|3000 characters needed characters left

You could do it with an animation.

The animation would contain all the stages from 'full' to 'empty' for the circle timer. Then just link the position of the animation to the timer variable.

(I don't know alot about Unity so this is just experience from general game building, you would have to work out how to animate and refere to animations in Unity)

more ▼

answered Apr 09 '10 at 01:49 PM

Sydan gravatar image

Sydan
241 13 14 19

You can't animate pic animations in Unity. You have to use script. I can see where you might think that, coming from a general game building background, however, because that's a common thing to do.

Apr 09 '10 at 01:59 PM e.bonneville

Ah. Ok. How would you have an animated image then? Would have you have manually change the picture via scritp?

Apr 09 '10 at 02:34 PM Sydan

The best approach for that is either a MovieTexture, or you can tile all your animated images together into one large texture, and use the textureOffset and textureScale to selectively display only one of these tiles.

Aug 04 '10 at 05:22 PM Wolfram

It can be animated if you use a 3D mesh. Then just put it on the GUI camera layer.

Jan 31 '11 at 01:04 PM Rblain

Huh? Of course obviously you drop it on a mesh (like anything you want to "see"!! heh)

If you prefer use one of the many 2D sprite systems available on the Asset Store, they all work fine (or again just drop it on a plane mesh). Then animate your heart out using the wonderful animation system.

Your answer is the correct one Sydan!

May 28 '12 at 08:48 PM Fattie
(comments are locked)
10|3000 characters needed characters left
more ▼

answered Sep 14 '11 at 07:36 PM

Neodrop gravatar image

Neodrop
46

(comments are locked)
10|3000 characters needed characters left

If you are using NGUI, you can do this with a progress bar component by setting the Foreground sprite to use radial fill.

more ▼

answered May 06 at 09:47 PM

TimK.Disney gravatar image

TimK.Disney
1

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3673
x347
x68
x22

asked: Apr 09 '10 at 10:17 AM

Seen: 25507 times

Last Updated: May 06 at 09:47 PM