Advertisement
juniorklawa

Save current time

Jun 30th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.21 KB | None | 0 0
  1.      using UnityEngine;
  2.      using System.Collections;
  3.      using System;
  4.      
  5.      public class DataMaster : MonoBehaviour
  6.      {
  7.          DateTime currentDate;
  8.          DateTime oldDate;
  9.          void Start()
  10.          {
  11.              //Store the current time when it starts
  12.              currentDate = System.DateTime.Now;
  13.      
  14.              //Grab the old time from the player prefs as a long
  15.              long temp = Convert.ToInt64(PlayerPrefs.GetString("sysString"));
  16.      
  17.              //Convert the old time from binary to a DataTime variable
  18.              DateTime oldDate = DateTime.FromBinary(temp);
  19.              print("oldDate: " + oldDate);
  20.      
  21.              //Use the Subtract method and store the result as a timespan variable
  22.              TimeSpan difference = currentDate.Subtract(oldDate);
  23.              print("Difference: " + difference);
  24.      
  25.          }
  26.      
  27.          void OnApplicationQuit()
  28.          {
  29.              //Savee the current system time as a string in the player prefs class
  30.              PlayerPrefs.SetString("sysString", System.DateTime.Now.ToBinary().ToString());
  31.      
  32.              print("Saving this date to prefs: " + System.DateTime.Now);
  33.          }
  34.      
  35.      }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement