Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by georgikgeo25 · Jun 30, 2020 at 08:20 PM · onapplicationpause

OnApplicationPause problem Time

I have a daily reward. When the game enters OnApplicationPause the time does not continue. unless I use Application.Quit and enter again Please if you know what I should write down at OnApplicationPause

using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using Unity.Notifications.Android; using UnityEngine.SceneManagement;

public class DailyReward : MonoBehaviour {

 //UI
 public Text timeLabel; //only use if your timer uses a label
 public Button timerButton; //used to disable button when needed
 public Image _progress;
 //TIME ELEMENTS
 public int hours; //to set the hours
 public int minutes; //to set the minutes
 public int seconds; //to set the seconds
 private bool _timerComplete = false;
 private bool _timerIsReady;
 private TimeSpan _startTime;
 private TimeSpan _endTime;
 private TimeSpan _remainingTime;
 //progress filler
 private float _value = 1f;
 //reward to claim
 public int RewardToEarn;
 public GameObject press;
 [SerializeField]





 //startup
 void Start()
 {
     if (PlayerPrefs.GetString("_timer") == "")
     {
         Debug.Log("==> Enableing button");
         enableButton();
     }
     else
     {
         disableButton();
         StartCoroutine("CheckTime");
     }
 }



 //update the time information with what we got some the internet
 private void updateTime()
 {
     if (PlayerPrefs.GetString("_timer") == "Standby")
     {
         PlayerPrefs.SetString("_timer", TimeManager.sharedInstance.getCurrentTimeNow());
         PlayerPrefs.SetInt("_date", TimeManager.sharedInstance.getCurrentDateNow());

     }
     else if (PlayerPrefs.GetString("_timer") != "" && PlayerPrefs.GetString("_timer") != "Standby")
     {

         int _old = PlayerPrefs.GetInt("_date");
         int _now = TimeManager.sharedInstance.getCurrentDateNow();


         //check if a day as passed
         if (_now > _old)
         {//day as passed
             Debug.Log("Day has passed");
             enableButton();

             return;
         }
         else if (_now == _old)
         {//same day

             Debug.Log("Same Day - configuring now");
             _configTimerSettings();
             return;
         }
         else
         {
             Debug.Log("error with date");
             return;
         }
     }
     Debug.Log("Day had passed - configuring now");
     _configTimerSettings();
 }

 //setting up and configureing the values
 //update the time information with what we got some the internet
 private void _configTimerSettings()
 {
     _startTime = TimeSpan.Parse(PlayerPrefs.GetString("_timer"));
     _endTime = TimeSpan.Parse(hours + ":" + minutes + ":" + seconds);
     TimeSpan temp = TimeSpan.Parse(TimeManager.sharedInstance.getCurrentTimeNow());
     TimeSpan diff = temp.Subtract(_startTime);
     _remainingTime = _endTime.Subtract(diff);
     //start timmer where we left off
     setProgressWhereWeLeftOff();

     if (diff >= _endTime)
     {
         _timerComplete = true;
         enableButton();

     }
     else
     {
         _timerComplete = false;
         disableButton();
         _timerIsReady = true;
     }
 }

 //initializing the value of the timer
 private void setProgressWhereWeLeftOff()
 {
     float ah = 1f / (float)_endTime.TotalSeconds;
     float bh = 1f / (float)_remainingTime.TotalSeconds;
     _value = ah / bh;
     _progress.fillAmount = _value;
 }



 //enable button function
 private void enableButton()
 {
     timerButton.interactable = true;
     press.SetActive(true);
 }



 //disable button function
 private void disableButton()
 {
     timerButton.interactable = false;
     press.SetActive(false);


 }


 //use to check the current time before completely any task. use this to validate
 private IEnumerator CheckTime()
 {
     disableButton();

     Debug.Log("==> Checking for new time");
     yield return StartCoroutine(
         TimeManager.sharedInstance.getTime()
     );
     updateTime();

     Debug.Log("==> Time check complete!");


 }


 //trggered on button click
 public void rewardClicked()
 {
     spinText.spinAmount += 30;
     Debug.Log("==> Claim Button Clicked");
     claimReward(RewardToEarn);
     PlayerPrefs.SetString("_timer", "Standby");
     StartCoroutine("CheckTime");
 }



 //update method to make the progress tick
 void Update()
 {
     if (_timerIsReady)
     {
         if (!_timerComplete && PlayerPrefs.GetString("_timer") != "")
         {
             _value -= Time.deltaTime * 1f / (float)_endTime.TotalSeconds;
             _progress.fillAmount = _value;

             //this is called once only
             if (_value <= 0 && !_timerComplete)
             {
                 //SendNotification1();
                 //CreateNotifChanel1()

                 //when the timer hits 0, let do a quick validation to make sure no speed hacks.
                 validateTime();
                 _timerComplete = true;

             }
         }
     }
 }



 //validator
 private void validateTime()
 {

     Debug.Log("==> Validating time to make sure no speed hack!");
     StartCoroutine("CheckTime");
     PlayerPrefs.SetInt("_date", TimeManager.sharedInstance.getCurrentDateNow());

 }




 private void claimReward(int x)
 {
     Debug.Log("YOU EARN " + x + " REWARDS");
 }
 void CreateNotifChanel1()
 {
     var b = new AndroidNotificationChannel()
     {
         Id = "spin",
         Name = "playreminder",
         Importance = Importance.High,
         Description = "Reminds the player to play the game",
     };
     AndroidNotificationCenter.RegisterNotificationChannel(b);
 }


 void SendNotification1()
 {
     var notification = new AndroidNotification();
     notification.Title = "Colect Reward Spin";
     notification.Text = "Come one and collect spin and win";
     notification.FireTime = System.DateTime.Now.AddSeconds(3);
     notification.SmallIcon = "icon_3";
     notification.LargeIcon = "icon_2";
     notification.ShouldAutoCancel = true;


     AndroidNotificationCenter.SendNotification(notification, "spin");
 }
 public void SaveData()
 {
     PlayerPrefs.SetString("_timer", TimeManager.sharedInstance.getCurrentTimeNow());
     PlayerPrefs.Save();
 }

 void OnApplicationPause(bool pauseStatus)
 {
     if (pauseStatus)
     {

         

     }


 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

201 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

[Android] When App closed get time when player was gone? 1 Answer

OnApplicationFocus on Android - render an extra frame on pause? 0 Answers

Can I render an extra frame onapplication pause in android? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges