How to do it...

To create an animated progress bar, follow these steps:

  1. Import the progress bar image to Unity and set its type to Sprite (2d and UI).
  2. Open a scene and create a new Image by using the Game ObjectUI |Image command.Canvas and Image game objects will be created.
  3. Select the Image game object and change its name to ProgressBar.
  4. Find the Image component in the ProgressBar game object's Inspector.
  5. Set the Image Type property to Filled, the Fill Method to Horizontal, and the Fill Amount to 0, as shown in the following screenshot:
  1. By changing the Fill Amount, value you can animate a progress bar. You can do it with an Animation View, but it makes more sense to use a script here.
  2. Create a new script and call it ProgressBars.cs (you can also use the same script provided with the example Unity project; you can find it in the Scripts directory of this recipe). Make sure to include the UnityEngine.UI namespace in the script. Write the following line in the Update() function:
        image.fillAmount += Time.deltaTime * fillSpeed; 
  1. The image object is a reference to the Image component on the same game object. We assign it in the Start() function by calling the line:
        image = GetComponent<Image>(); 
  1. The fillSpeed variable is a public float variable that describes the fill amount increment per second.
  2. In this simple script, we increase the Fill Amount of the Image component in time.
  3. Assign the script to the ProgressBar game object and play the game to see the effect.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.93.245