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 /
avatar image
0
Question by andyblueAMS001 · Aug 29, 2020 at 07:53 PM · playerprefstimersavingdatetime

Timer that can count Hour difference between two DateTime for android

Hi i need help on a timer that can tell the difference of hours or minutes passed after pause, minimize or quit of the android app, i really need help something that is tested and working, im not looking for something that runs in the background, i am looking something that saves the time in playerprefs on quit or pause and resumes the time after opening the app again, i know its about calculating the difference from the old time it was saved and from the datetime.now time, i also tried a script i found but it is not working when combined on exit and minimize operations. the script works when just minimizing and resuming, but once i exit the app. the timex gives different value like it starts from zero again, it also works when i just quit the app and open but when i mix quit app and minimize it does not go well...

import UnityEngine; import System.Collections; import System.DateTime; import System;

static var currentDate : DateTime; static var oldDate : DateTime; static var timex : double; static var temp : long; static var difference : TimeSpan;

var guiStyle : GUIStyle;

function Start() {

 Screen.SetResolution(1280, 720, true);
 Application.targetFrameRate = 29;


  // THIS IS THE ORIGINAL
  //Store the current time when it starts
  currentDate = System.DateTime.Now;


 //Grab the old time from the playerprefs as a long
 temp = Convert.ToInt64(PlayerPrefs.GetString("timeData"));


 //Convert the old time from binary to a DataTime variable
 oldDate = System.DateTime.FromBinary(temp);

// print("oldDate: " + oldDate);

 //Use the Subtract method and store the result as a timespan variable
 var difference : TimeSpan = currentDate.Subtract(oldDate);

// print("Difference: " + difference.TotalSeconds);

 timex = timex + difference.TotalSeconds;


}

}

//////////////////////////////// ///// ON PASUE /////////////// function OnApplicationPause(status : boolean) { ///////////////////////////////////////////////

 /////// ON MINIMIZE /////////

///////////////////////////// if(status == true) { //Save the current system time as a string in the playerprefs class PlayerPrefs.SetString("timeData", System.DateTime.Now.ToBinary().ToString()); // PlayerPrefs.Save();

 Debug.Log("App Minimized");





 }



 /////// ON APP RESUME /////////

/////////////////////////////// if(status == false) {

   //Store the current time when it starts
  currentDate = DateTime.Now;


 //Grab the old time from the playerprefs as a long
 temp = Convert.ToInt64(PlayerPrefs.GetString("timeData"));


 //Convert the old time from binary to a DataTime variable
 oldDate = System.DateTime.FromBinary(temp);

// print("oldDate: " + oldDate);

 //Use the Subtract method and store the result as a timespan variable
 difference = currentDate.Subtract(oldDate);

// print("Difference: " + difference.TotalSeconds);

 timex = timex + difference.TotalSeconds;




  Debug.Log("App Resumed");

 }

} ///////////////////////////////////////////////

function OnApplicationQuit() { //Save the current system time as a string in the playerprefs class PlayerPrefs.SetString("timeData", System.DateTime.Now.ToBinary().ToString()); // PlayerPrefs.Save();

Debug.Log("Quited");

}

function OnGUI() {

   ////// GUI Styles
   guiStyle.fontSize = 38;
   guiStyle.normal.textColor = Color.white;


   ////// DISPLAY TIME B
   GUI.Label(Rect(20,20,300,40), "Time:  " + timex, guiStyle);




}

Comment
Add comment · Show 1
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
avatar image andyblueAMS001 · Aug 30, 2020 at 09:13 AM 0
Share

The below code works with time saving and loading properly BUT only when the app was closed or removed from recent apps in device, but does not work when user press HO$$anonymous$$E or $$anonymous$$imize the app, i tried to use OnApplicationPause then called Application.Quit(); to simulate like if you leave the app close the app so timers work properly and not freeze like nothing happened, now that problem is OnApplicationPause does not work in my device.. i dunno why, for debugs it work but when i use Application.Quit() on OnApplicationPause does not work.. any help please... anyone..


function Start() {

  //Store the current time when it starts
 currentDate = System.DateTime.Now;

 //Grab the old time from the playerprefs as a long

var temp : long = Convert.ToInt64(PlayerPrefs.GetString("timeData"));

 //Convert the old time from binary to a DataTime variable
 oldDate = System.DateTime.FromBinary(temp);

 //Use the Subtract method and store the result as a timespan variable
 var difference : TimeSpan = currentDate.Subtract(oldDate);

 timex = timex + difference.TotalSeconds;

}

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by andyblueAMS001 · Aug 30, 2020 at 09:18 AM

what i want to do is if the user press home button it will force the app to quit so the timers work properly but i am already getting tired of trying alot of variation but it really seems that it does not accept the Pause state... i cannot force the player to always close the app from recents list everytime he does not want to play the app anymore... that why what i have in mind is to trick the Home button by triggering a quit even by using a Home or minimize, i know Home button is link to OnApplicationPause but what can you do if it does not WORK??

Comment
Add comment · Share
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

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

141 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

Related Questions

PlayerPrefs not saving mission info at all. 0 Answers

[Please Help!]How do I make a player save their position when I pause the game? 3 Answers

Help Saving Max Health With PlayerPrefs 1 Answer

PlayerPrefs v. XML File for Saving Data? 1 Answer

How can a Save a HighScore 2 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