daily pastebin goal
49%
SHARE
TWEET

Untitled

a guest Feb 13th, 2018 47 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. public class ProgressBar : MonoBehaviour {
  7.  
  8.     private float progress;
  9.     private float maxAmount;
  10.  
  11.     public Image progressImage;
  12.     public bool useTimer;
  13.     public Color maxProgressColour;
  14.     public Color minProgressColour;
  15.  
  16.     private Color currentColour;
  17.     private float timeDuration = 100;    //In seconds
  18.     private WaitForSeconds oneSecond = new WaitForSeconds(1);
  19.  
  20.     // Use this for initialization
  21.     void Start () {
  22.         progress = maxAmount;
  23.         ChangeColour ();
  24.     }
  25.    
  26.     public void DecrementProgress(float p) {
  27.         if (progress > 0) {
  28.             progress -= p;
  29.             progressImage.fillAmount = (progress / maxAmount);
  30.             ChangeColour ();
  31.         } else {
  32.             ProgressComplete ();
  33.         }
  34.     }
  35.  
  36.     public void SetMaxAmount(float m) {
  37.         maxAmount = m;
  38.         progress = m;
  39.         progressImage.fillAmount = 1;
  40.     }
  41.  
  42.     public void StartTimer() {
  43.         if (useTimer) {
  44.             float increment = maxAmount / timeDuration;
  45.             StartCoroutine(StartTimer(increment));
  46.         }
  47.     }
  48.  
  49.     IEnumerator StartTimer(float increment) {
  50.         while (true) {
  51.             if(progress > 0) {
  52.                 yield return oneSecond;
  53.                 progress -= increment;
  54.             } else {
  55.                 ProgressComplete ();
  56.                 break;
  57.             }
  58.         }
  59.     }
  60.  
  61.     void ChangeColour(){
  62.         progressImage.color = Color.Lerp (minProgressColour, maxProgressColour, (float)progress / maxAmount);
  63.     }
  64.  
  65.     void ProgressComplete(){
  66.         SendMessageUpwards ("OnProgressComplete");
  67.     }
  68. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top