Header Ads

Energy Recovery Continues When The App Is Closed Unity Script

Energy Recovery Continues When The App Is Closed Unity Script:

CODE:

   public static energyrecoverytime instance;
   public float timeValue =300;
   public float TimeTest;
   public int Energy;
   public Text Energytime;
   public Text EnergyText;

 
   void Awake()
   {
        instance = this;
       Energy = PlayerPrefs.GetInt("Energy");
        EnergyText.text = Energy.ToString();
   }
   void Start()
   {
     if(PlayerPrefs.HasKey("TimeSave"))
     {
      long timestamp= (long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds;
    string value = PlayerPrefs.GetString("Time");
    long oldTimestamp = Convert.ToInt64(value);
   
    long elapsedSeconds = timestamp - oldTimestamp;
    TimeSpan interval = TimeSpan.FromSeconds(elapsedSeconds);
   float days = interval.Days;
   float hours = interval.Hours;
   float minutes = interval.Minutes;
   float seconds = interval.Seconds;
   
float Timesystem = (days *24) +(hours*60) + (minutes*60) + seconds;
    float TimeSaves = PlayerPrefs.GetFloat("TimeSave");
    if(Timesystem >TimeSaves)
    {
      float t = Timesystem - TimeSaves;
      if(t <300)
      {
        if(Energy<5)
       AddEnergy(1);
       TimeTest =t;
      } else if(t <600)
      {
        if(Energy<4)
       AddEnergy(2);
       else
       UpdateEnergy();
       TimeTest =t-300;
      }
      else if(t <900)
      {
        if(Energy<3)
       AddEnergy(3);
       else
       UpdateEnergy();
       TimeTest =t-600;
      }
      else if(t <1200)
      {
        if(Energy<2)
       AddEnergy(4);
       else
       UpdateEnergy();
       TimeTest =t-900;
      }
      else if(t <1500)
      {
        if(Energy<1)
       AddEnergy(5);
       else
       UpdateEnergy();
       TimeTest =t-1200;
      }
    else
      {
        UpdateEnergy();
      }
     
    }
    else
    {
       TimeTest = TimeSaves - Timesystem;

    }

    timeValue =  TimeTest;
            PlayerPrefs.DeleteKey("TimeSave");
           PlayerPrefs.DeleteKey("Time");
     }
   }
   void Update()
    {
      if(Energy<5)
       starttime();
       
    }
    void UpdateEnergy()
    {
            Energy =5;
           PlayerPrefs.SetInt("Energy",Energy);
           EnergyText.text = Energy.ToString();
           timeValue=300;
           TimeTest =0;
    }
  void AddEnergy(int amount)
  {
          Energy += amount;
           PlayerPrefs.SetInt("Energy",Energy);
           EnergyText.text = Energy.ToString();
            if(Energy>5)
           {
            TimeTest =0;
           Energytime.text="";
           timeValue=300;
           }

  }
    public void starttime()
   {
   
            if(timeValue >0)
            timeValue -= Time.deltaTime;
        else
        {
            AddEnergy(1);
            timeValue=300;
            if(Energy>=5)
              Energytime.text="";

        }
      DisplayTime(timeValue);
   }
    void DisplayTime(float timeToDisplay)
    {
        if(timeToDisplay<0)
        {
            timeToDisplay = 0;
        }
        float minutes = Mathf.FloorToInt(timeToDisplay / 60);
        float seconds = Mathf.FloorToInt(timeToDisplay % 60);
        Energytime.text = string.Format("{0:00}:{1:00}",minutes,seconds);
    }
   
  void OnApplicationFocus(bool hasFocus)
    {
        if(hasFocus){
          //your app is NO LONGER in the background
      }else{
            if(noiluutru.instance.thoigianhoitien==true)
            {  
                PlayerPrefs.SetFloat("thoigianquancao",chaythoigiantien.instance.timeValue);
           
     long timestamp= (long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds;
    string value = timestamp.ToString();
    PlayerPrefs.SetString("Time", value);
            }
      }
    }
    public void BTN_time()
    {
        Energy -=1;
        PlayerPrefs.SetInt("Energy",Energy);
        EnergyText.text = Energy.ToString() ;
    }

Video

No comments

Powered by Blogger.