using UnityEngine;
namespace Crosstales.Common.Util
{
/// Wrapper for the PlayerPrefs.
public static class CTPlayerPrefs
{
//TODO add getter and setter: Vector2 - 4, Quaternion
/*
#if UNITY_EDITOR
private static readonly SerializableDictionary content = new SerializableDictionary();
private static readonly string fileName = $"{Application.persistentDataPath}/crosstales.cfg";
static CTPlayerPrefs()
{
if (System.IO.File.Exists(fileName))
content = XmlHelper.DeserializeFromFile>(fileName);
if (content == null)
content = new SerializableDictionary();
}
#endif
*/
/// Exists the key?
/// Key for the PlayerPrefs.
/// Value for the key.
public static bool HasKey(string key)
{
if (string.IsNullOrEmpty(key))
throw new System.ArgumentNullException(nameof(key));
//#if UNITY_EDITOR
// return content.ContainsKey(key);
//#else
return PlayerPrefs.HasKey(key);
//#endif
}
/// Deletes all keys.
public static void DeleteAll()
{
//#if (UNITY_WSA || UNITY_WEBGL) && !UNITY_EDITOR
PlayerPrefs.DeleteAll();
//#else
// content.Clear();
//#endif
}
/// Delete the key.
/// Key to delete in the PlayerPrefs.
public static void DeleteKey(string key)
{
if (string.IsNullOrEmpty(key))
throw new System.ArgumentNullException(nameof(key));
//#if (UNITY_WSA || UNITY_WEBGL) && !UNITY_EDITOR
PlayerPrefs.DeleteKey(key);
//#else
// content.Remove(key);
//#endif
}
/// Saves all modifications.
public static void Save()
{
//#if (UNITY_WSA || UNITY_WEBGL) && !UNITY_EDITOR
PlayerPrefs.Save();
/*
#else
if (content != null && content.Count > 0)
{
XmlHelper.SerializeToFile(content, fileName);
}
#endif
*/
}
#region Getter
/// Allows to get a string from a key.
/// Key for the PlayerPrefs.
/// Value for the key.
public static string GetString(string key)
{
if (string.IsNullOrEmpty(key))
throw new System.ArgumentNullException(nameof(key));
//#if (UNITY_WSA || UNITY_WEBGL) && !UNITY_EDITOR
return PlayerPrefs.GetString(key);
//#else
// return content[key];
//#endif
}
/// Allows to get a float from a key.
/// Key for the PlayerPrefs.
/// Value for the key.
public static float GetFloat(string key)
{
if (string.IsNullOrEmpty(key))
throw new System.ArgumentNullException(nameof(key));
//#if (UNITY_WSA || UNITY_WEBGL) && !UNITY_EDITOR
return PlayerPrefs.GetFloat(key);
//#else
// float.TryParse(GetString(key), out float result);
// return result;
//#endif
}
/// Allows to get an int from a key.
/// Key for the PlayerPrefs.
/// Value for the key.
public static int GetInt(string key)
{
if (string.IsNullOrEmpty(key))
throw new System.ArgumentNullException(nameof(key));
//#if (UNITY_WSA || UNITY_WEBGL) && !UNITY_EDITOR
return PlayerPrefs.GetInt(key);
//#else
// int.TryParse(GetString(key), out int result);
// return result;
//#endif
}
/// Allows to get a bool from a key.
/// Key for the PlayerPrefs.
/// Value for the key.
public static bool GetBool(string key)
{
return "true".CTEquals(GetString(key));
}
/// Allows to get a DateTime from a key.
/// Key for the PlayerPrefs.
/// Value for the key.
public static System.DateTime GetDate(string key)
{
System.DateTime.TryParseExact(GetString(key), "yyyyMMddHHmmsss", null, System.Globalization.DateTimeStyles.None, out System.DateTime result);
return result;
}
#endregion
#region Setter
/// Allows to set a string for a key.
/// Key for the PlayerPrefs.
/// Value for the PlayerPrefs.
public static void SetString(string key, string value)
{
if (string.IsNullOrEmpty(key))
throw new System.ArgumentNullException(nameof(key));
//#if (UNITY_WSA || UNITY_WEBGL) && !UNITY_EDITOR
PlayerPrefs.SetString(key, value);
/*
#else
if (content.ContainsKey(key))
{
content[key] = value;
}
else
{
content.Add(key, value);
}
#endif
*/
}
/// Allows to set a float for a key.
/// Key for the PlayerPrefs.
/// Value for the PlayerPrefs.
public static void SetFloat(string key, float value)
{
//#if (UNITY_WSA || UNITY_WEBGL) && !UNITY_EDITOR
if (string.IsNullOrEmpty(key))
throw new System.ArgumentNullException(nameof(key));
PlayerPrefs.SetFloat(key, value);
//#else
// SetString(key, value.ToString(System.Globalization.CultureInfo.InvariantCulture));
//#endif
}
/// Allows to set an int for a key.
/// Key for the PlayerPrefs.
/// Value for the PlayerPrefs.
public static void SetInt(string key, int value)
{
//#if (UNITY_WSA || UNITY_WEBGL) && !UNITY_EDITOR
if (string.IsNullOrEmpty(key))
throw new System.ArgumentNullException(nameof(key));
PlayerPrefs.SetInt(key, value);
//#else
// SetString(key, value.ToString());
//#endif
}
/// Allows to set a bool for a key.
/// Key for the PlayerPrefs.
/// Value for the PlayerPrefs.
public static void SetBool(string key, bool value)
{
SetString(key, value ? "true" : "false");
}
/// Allows to set a DateTime for a key.
/// Key for the PlayerPrefs.
/// Value for the PlayerPrefs.
public static void SetDate(string key, System.DateTime value)
{
if (value == null)
throw new System.ArgumentNullException(nameof(value));
SetString(key, value.ToString("yyyyMMddHHmmsss"));
}
#endregion
}
}
// © 2015-2020 crosstales LLC (https://www.crosstales.com)