Making a progress bar based off a timer that goes down and persists?
Hey I'm trying to make a crafting progress bar that is based on a timer for the craft time remaining. The timer is 120 seconds long and uses the fillAmount (0-1). There are a few problems one of them being that the fillAmount reaches 1 before the timer finishes. Here is the code.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;
public class Craft : MonoBehaviour {
//Problem with loading timer amount is that it is not setting the timer in CheckDate() so it is still being 0 even after CheckDate() so the other fuction is reseting the timer because it thinks it hasent been called.
DateTime currentDate;
DateTime oldDate;
public float tinpaneltimer;
public bool tinpaneltimerOn = false;
public string saveLocation;
public static Craft instance;
public Text CraftSteelButtonText;
public Text CraftElectrumButtonText;
public Text CraftTinPanelButtonText;
public int Iron;
public int Coal;
public int Steel;
public int Silver;
public int GoldOre;
public int Electrum;
public int Diff;
public int Tin;
public int TinPanel;
public int XP;
public int XPCap;
public int Level;
public int XPCapMulitplier;
public Text SteelAmount;
public Text ElectrumAmount;
public Text TinPanelAmount;
public Text SlotSteelText;
public Text SlotElectrumText;
public Text SlotTinPanelText;
public bool CraftSteelOn = false;
public bool CraftElectrumOn = false;
public bool CraftTinPanelOn = false;
public Button CraftSteelButton;
public Image CraftSteelButtonImage;
public Image CraftSteelButtonImageRight;
public Button CraftElectrumButton;
public Image CraftElectrumButtonImage;
public Image CraftElectrumButtonImageRight;
public Button CraftTinPanelButton;
public Image CraftTinPanelButtonImage;
public Image CraftTinPanelButtonImageRight;
public Image Panel1;
public Image Panel2;
public Image Panel3;
public Image LevelBar;
public AudioSource Audio;
void Start()
{
if (tinpaneltimer <= 0) {
tinpaneltimer = 0;
}
CheckDate ();
if (tinpaneltimer != 120) {
CraftTinPanelOn = true;
tinpaneltimerOn = true;
print ("Tinpaneltimer was not at 120 so it had already started which means that timer should be On");
}
if (tinpaneltimer == 0) {
tinpaneltimer = 120;
print ("Timer was 0 so it was reset to 120");
}
//if (tinpaneltimerOn == true) {
//tinpaneltimer -= instance.CheckDate ();
//}
//not sure what this does, could possibly interfere.
}
void Awake()
{
CheckDate ();
instance = this;
saveLocation = "lastSavedDate1";
}
public float CheckDate()
{
currentDate = System.DateTime.Now;
string tempString = PlayerPrefs.GetString (saveLocation, "1");
long templong = Convert.ToInt64 (tempString);
DateTime oldDate = DateTime.FromBinary (templong);
print ("Old Date: " + oldDate);
TimeSpan difference = currentDate.Subtract (oldDate);
TimeSpan dt = TimeSpan.FromSeconds(120) - difference;
print ("Timer should be " + dt);
//Figure out how to subtract int and timespan// //Did that but maybe be something else setting it to 120 when game starts..
print ("Difference: " + difference);
tinpaneltimer = (float)dt.TotalSeconds;
print ("Difference is correct DateTime Checked");
return (float)difference.TotalSeconds;
}
public void SaveDate()
{
print ("Old Date Set");
PlayerPrefs.SetString (saveLocation, System.DateTime.Now.ToBinary ().ToString ());
print ("Saving this date to player prefs: " + System.DateTime.Now);
}
public void ExitButton()
{
Panel1.gameObject.SetActive (false);
Panel2.gameObject.SetActive (false);
Panel3.gameObject.SetActive (false);
LevelBar.gameObject.SetActive (true);
}
public void ClickSlot1()
{
Panel1.gameObject.SetActive (true);
LevelBar.gameObject.SetActive (false);
}
public void ClickSlot2()
{
Panel2.gameObject.SetActive (true);
LevelBar.gameObject.SetActive (false);
}
public void ClickSlot3()
{
Panel3.gameObject.SetActive (true);
LevelBar.gameObject.SetActive (false);
}
void Update()
{
//0.849583 amount of zeros * 6
//Crafts multiple items when timer = 0 because it calls multiple times
if (tinpaneltimer <= 0) {
tinpaneltimer = 0;
}
if (tinpaneltimer == 0) {
CraftTinPanelOn = false;
tinpaneltimerOn = false;
print ("Tinpaneltimer finished completing craft");
WaitForView3 ();
}
if (tinpaneltimerOn == true) {
CheckDate ();
tinpaneltimer -= Time.deltaTime;
print ("TinpaneltiemrOn so checking date everyframe and subtracting 1 from 120 timer every time.deltatime");
}
//Maybe either have a long time for bigger items or just not and have same craft time.
SlotSteelText.text = "" + Steel;
SlotElectrumText.text = "" + Electrum;
SlotTinPanelText.text = "" + TinPanel;
if (CraftSteelOn == true) {
CraftSteelButtonImage.fillAmount += 1f / 2f * Time.deltaTime;
CraftSteelButtonImageRight.fillAmount += 1f / 2f * Time.deltaTime;
}
if (CraftElectrumOn == true) {
CraftElectrumButtonImage.fillAmount += 1f / 2f * Time.deltaTime;
CraftElectrumButtonImageRight.fillAmount += 1f / 2f * Time.deltaTime;
}
if (CraftTinPanelOn == true) {
CraftTinPanelButtonImage.fillAmount += 1f / tinpaneltimer * Time.time;
CraftTinPanelButtonImageRight.fillAmount += 1f / tinpaneltimer * Time.deltaTime;
}
//Store Date.Time when you start the craft and keep checking it until the subtraction is equal to variable
SteelAmount.text = "" + Steel;
ElectrumAmount.text = "" + Electrum;
TinPanelAmount.text = "" + TinPanel;
XP = PlayerPrefs.GetInt ("XP");
XPCap = PlayerPrefs.GetInt ("XPCap");
Level = PlayerPrefs.GetInt ("Level");
XPCapMulitplier = PlayerPrefs.GetInt ("XPCapMultiplier");
Iron = PlayerPrefs.GetInt ("Iron");
Coal = PlayerPrefs.GetInt ("Coal");
Steel = PlayerPrefs.GetInt ("Steel");
Silver = PlayerPrefs.GetInt ("Silver");
GoldOre = PlayerPrefs.GetInt ("GoldOre");
Electrum = PlayerPrefs.GetInt ("Electrum");
TinPanel = PlayerPrefs.GetInt ("TinPanel");
Tin = PlayerPrefs.GetInt ("Tin");
}
public void CraftSteel()
{
if (Iron >= 1 && Coal >= 1) {
Audio.Play ();
CraftSteelOn = true;
XP = XP + 20;
PlayerPrefs.SetInt ("Level", Level);
PlayerPrefs.SetInt ("XPCap", XPCap);
PlayerPrefs.SetInt("XP", XP);
PlayerPrefs.SetInt ("XPCapMultiplier", XPCapMulitplier);
StartC ();
}
if (Iron <= 1 || Coal <= 1) {
CraftSteelButtonText.text = "CANT AFFORD";
}
}
public void CraftElectrum()
{
if (Silver >= 1 && GoldOre >= 1) {
Audio.Play ();
CraftElectrumOn = true;
XP = XP + 20;
PlayerPrefs.SetInt ("Level", Level);
PlayerPrefs.SetInt ("XPCap", XPCap);
PlayerPrefs.SetInt("XP", XP);
PlayerPrefs.SetInt ("XPCapMultiplier", XPCapMulitplier);
StartC2 ();
}
if (Silver <= 1 || GoldOre <= 1) {
CraftElectrumButtonText.text = "CANT AFFORD";
}
}
public void CraftTinPanel()
{
if (Tin >= 4) {
SaveDate ();
tinpaneltimerOn = true;
Audio.Play ();
CraftTinPanelOn = true;
XP = XP + 13;
PlayerPrefs.SetInt ("Level", Level);
PlayerPrefs.SetInt ("XPCap", XPCap);
PlayerPrefs.SetInt("XP", XP);
PlayerPrefs.SetInt ("XPCapMultiplier", XPCapMulitplier);
}
if (Tin <= 4) {
CraftTinPanelButtonText.text = "CANT AFFORD";
}
}
public IEnumerator WaitForView()
{
CraftSteelButtonText.text = "CRAFTED";
CraftSteelButton.interactable = false;
yield return new WaitForSeconds (2f);
PlayerPrefs.SetInt ("Iron", PlayerPrefs.GetInt ("Iron") - 1);
PlayerPrefs.SetInt ("Coal", PlayerPrefs.GetInt ("Coal") - 1);
PlayerPrefs.SetInt ("Steel", PlayerPrefs.GetInt ("Steel") + 1);
CraftSteelOn = false;
CraftSteelButtonImage.fillAmount = 0;
CraftSteelButtonImageRight.fillAmount = 0;
CraftSteelButton.interactable = true;
CraftSteelButtonText.text = "CRAFT";
}
public IEnumerator WaitForView2()
{
CraftElectrumButtonText.text = "CRAFTED";
CraftElectrumButton.interactable = false;
yield return new WaitForSeconds (2f);
PlayerPrefs.SetInt ("Silver", PlayerPrefs.GetInt ("Silver") - 1);
PlayerPrefs.SetInt ("GoldOre", PlayerPrefs.GetInt ("GoldOre") - 1);
PlayerPrefs.SetInt ("Electrum", PlayerPrefs.GetInt ("Electrum") + 1);
CraftElectrumOn = false;
CraftElectrumButtonImage.fillAmount = 0;
CraftElectrumButtonImageRight.fillAmount = 0;
CraftElectrumButton.interactable = true;
CraftElectrumButtonText.text = "CRAFT";
}
public void WaitForView3()
{
CraftTinPanelButtonText.text = "CRAFTED";
CraftTinPanelButton.interactable = false;
PlayerPrefs.SetInt ("Tin", PlayerPrefs.GetInt ("Tin") - 4);
PlayerPrefs.SetInt ("TinPanel", PlayerPrefs.GetInt ("TinPanel") + 1);
tinpaneltimer = 120;
CraftTinPanelOn = false;
tinpaneltimerOn = false;
CraftTinPanelButtonImage.fillAmount = 0;
CraftTinPanelButtonImageRight.fillAmount = 0;
CraftTinPanelButton.interactable = true;
CraftTinPanelButtonText.text = "CRAFT";
}
void StartC()
{
StartCoroutine (WaitForView ());
}
void StartC2()
{
StartCoroutine (WaitForView2 ());
}
}
Don't worry about some of the // notes most are just for me and i wrote a while ago and have already fixed.
Other than the problem of the fillAmount reaching 1 before the CraftTinPanel timer finishes, I also need to figure out a way to save the fillAmount progress in a PlayerPref or something so if you quit the game and come back the progress in the fillAmount doesnt restart. Thanks.
The solution might be to store the fillAmount in a PP Float, I didnt know you could store floats.
Your answer
Follow this Question
Related Questions
PlayerPrefs defaulting to 0 4 Answers
Unity builds with priop playerprefs 0 Answers
Saving an image's state 2 Answers
Network Behaviour 0 Answers
How to make a sensitivity slider? 0 Answers