This repository has been archived on 2025-04-11. You can view files and clone it, but cannot push or open issues or pull requests.
project-undercover/Project Undercover/Assets/Scripts/UI/ProgressPanelController.cs

77 lines
1.8 KiB
C#
Raw Permalink Normal View History

2017-10-03 04:15:13 -05:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ProgressPanelController : MonoBehaviour {
public Text progressText;
public GameObject progressBar, mainPanel;
private float maxWidth;
[Range(0, 1)]
private float progress = 0.0f;
private HSBColor startColor, endColor;
private static ProgressPanelController activePanel;
void Start() {
maxWidth = progressBar.GetComponent<RectTransform>().sizeDelta.x;
startColor = HSBColor.FromColor(Color.red);
endColor = HSBColor.FromColor(Color.blue);
ActivePanel = this;
Hide();
}
public void Reveal(string progressText)
{
this.progressText.text = progressText;
mainPanel.SetActive(true);
Progress = 0.0f;
}
public void Hide()
{
mainPanel.SetActive(false);
Progress = 0.0f;
2017-10-03 04:15:13 -05:00
}
public float Progress
{
get
{
return progress;
}
set
{
progress = Mathf.Clamp01(value);
var rectTrans = progressBar.GetComponent<RectTransform>();
rectTrans.sizeDelta = new Vector2(progress * maxWidth, rectTrans.sizeDelta.y);
var bar = progressBar.GetComponent<Image>();
bar.color = HSBColor.Lerp(startColor, endColor, progress).ToColor();
}
}
2017-10-05 13:53:29 -05:00
public static ProgressPanelController ActivePanel
{
2017-10-03 04:15:13 -05:00
get
{
if (activePanel)
return activePanel;
Debug.LogError("No panel in scene");
return null;
}
set
{
if (!activePanel)
activePanel = value;
else
{
Debug.LogError("More than one progress panel in the scene");
}
}
}
}