Skip to content

Instantly share code, notes, and snippets.

@sergebat
Created September 22, 2017 17:34
Show Gist options
  • Select an option

  • Save sergebat/719f662510cc07e81751cfa1fcc90974 to your computer and use it in GitHub Desktop.

Select an option

Save sergebat/719f662510cc07e81751cfa1fcc90974 to your computer and use it in GitHub Desktop.
Unity PlayerPrefs "extension" to save/retrieve dates/times
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