O'Reilly logo

Unity 3D Game Development by Example by Ryan Henson Creighton

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action - flex those GUI muscles

Let's take a trip down memory lane to the previous chapter, where we became OnGUI ninjas. We'll use the GUI techniques we already know to display the two bars, and shrink the foreground bar as time runs out.

  1. In the DoCountdown function, calculate the percentage of time elapsed by comparing the startTime and the timeRemaining values:
    function DoCountdown()
    {
    timeRemaining = startTime - Time.time;
    percent = timeRemaining/startTime * 100;
    if (timeRemaining < 0)
    {
    timeRemaining = 0;
    isPaused = true;
    TimeIsUp();
    }
    ShowTime();
    }
    
  2. Store the initial width of the clockFG graphic in a variable called clockFGMaxWidth in the Start function:
    function Start()
    {
    startTime = 120.0;
    clockFGMaxWidth = clockFG.width;
    }
    
  3. Create ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required