Quantcast
Channel: Questions in topic: "playerprefs"
Viewing all articles
Browse latest Browse all 1333

Timer that can count Hour difference between two DateTime for android

$
0
0
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); }

Viewing all articles
Browse latest Browse all 1333

Trending Articles