- Home /
Pairing and GUITexture with a Timer?
Hello! I am working on a project that currently uses a timer that counts down from any number as a GUIText object.
While that is all fine and dandy (and it works), I was wondering how I would achieve this with a GUITexture so I can have something a little more visual. It would be a gauge that sits at the top of the screen, and as time passes, the gauge will go down until its gone, and when its gone, the game will end.
The only issue I can think of is, the way the timer works in my game, time is added and taken away over the course of the game, does this mean in order for this to work, I'll need a cap on the time allowed? If so, how will that affect the solution to my problem
(I am relatively new to code, but know basics of C# and less Java)
Thanks in advance!!
Answer by VesuvianPrime · Sep 10, 2014 at 12:12 PM
There are a lot of different ways you can accomplish this. There are packages like NGUI that have builtin functionality for "filled" sprites that are perfect for health bars.
If you want to roll your own, the method I have adopted in the past:
Read up on creating procedural meshes: http://blog.nobel-joergensen.com/2010/12/25/procedural-generated-mesh-in-unity/
Create a simple mesh plane.
Assign your material to the plane, make sure the UVs are correct.
Write a method so you can set a float value between 0.0f and 1.0f to resize the mesh plane.
When the float value changes, adjust your UVs accordingly. This will make sure any texturing does not become squashed, but instead is sliced.
Your last question is related to point 4: you need a way to convert your time into a 0.0f to 1.0f range. If values can exceed 1.0f (100% fill) I'm wondering if a texture representation is a good idea?