Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class ProgressBar : MonoBehaviour {
- private float progress;
- private float maxAmount;
- public Image progressImage;
- public bool useTimer;
- public Color maxProgressColour;
- public Color minProgressColour;
- private Color currentColour;
- private float timeDuration = 100; //In seconds
- private WaitForSeconds oneSecond = new WaitForSeconds(1);
- // Use this for initialization
- void Start () {
- progress = maxAmount;
- ChangeColour ();
- }
- public void DecrementProgress(float p) {
- if (progress > 0) {
- progress -= p;
- progressImage.fillAmount = (progress / maxAmount);
- ChangeColour ();
- } else {
- ProgressComplete ();
- }
- }
- public void SetMaxAmount(float m) {
- maxAmount = m;
- progress = m;
- progressImage.fillAmount = 1;
- }
- public void StartTimer() {
- if (useTimer) {
- float increment = maxAmount / timeDuration;
- StartCoroutine(StartTimer(increment));
- }
- }
- IEnumerator StartTimer(float increment) {
- while (true) {
- if(progress > 0) {
- yield return oneSecond;
- progress -= increment;
- } else {
- ProgressComplete ();
- break;
- }
- }
- }
- void ChangeColour(){
- progressImage.color = Color.Lerp (minProgressColour, maxProgressColour, (float)progress / maxAmount);
- }
- void ProgressComplete(){
- SendMessageUpwards ("OnProgressComplete");
- }
- }
Add Comment
Please, Sign In to add comment