Created
September 22, 2017 17:34
-
-
Save sergebat/719f662510cc07e81751cfa1fcc90974 to your computer and use it in GitHub Desktop.
Unity PlayerPrefs "extension" to save/retrieve dates/times
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using UnityEngine; | |
| public class PlayerPrefsDateTime { | |
| public static DateTime GetDateTime(string key, DateTime defaultValue = default(DateTime)) { | |
| if (PlayerPrefs.HasKey(key)) { | |
| string stringValue = PlayerPrefs.GetString(key); | |
| long longValue = Convert.ToInt64(stringValue); | |
| return DateTime.FromBinary(longValue); | |
| } | |
| else { | |
| return defaultValue; | |
| } | |
| } | |
| public static void SetDateTime(string key, DateTime value) { | |
| long longValue = value.ToBinary(); | |
| string stringValue = longValue.ToString(); | |
| PlayerPrefs.SetString(key, stringValue); | |
| } | |
| public static TimeSpan GetTimeSpan(string key, TimeSpan defaultValue = default(TimeSpan)) { | |
| if (PlayerPrefs.HasKey(key)) { | |
| string stringValue = PlayerPrefs.GetString(key); | |
| long longValue = Convert.ToInt64(stringValue); | |
| return TimeSpan.FromTicks(longValue); | |
| } | |
| else { | |
| return defaultValue; | |
| } | |
| } | |
| public static void SetTimeSpan(string key, TimeSpan value) { | |
| long longValue = value.Ticks; | |
| string stringValue = longValue.ToString(); | |
| PlayerPrefs.SetString(key, stringValue); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment