Persistence.cs 962 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using UnityEngine;
  2. using XGame.Framework.Json;
  3. namespace XGame.Framework.Persistence
  4. {
  5. /// <summary>
  6. /// 数据持久化
  7. /// </summary>
  8. public static class Persistence
  9. {
  10. public static bool Write(string key, object val)
  11. {
  12. PlayerPrefs.SetString(key, XJson.ToJson(val));
  13. return true;
  14. }
  15. public static T Read<T>(string key, T defaultValue)
  16. {
  17. if (!PlayerPrefs.HasKey(key))
  18. {
  19. return defaultValue;
  20. }
  21. string str = PlayerPrefs.GetString(key);
  22. var val = XJson.ToObject<T>(str);
  23. return val;
  24. }
  25. public static void Delete(string key)
  26. {
  27. PlayerPrefs.DeleteKey(key);
  28. }
  29. public static void Clear()
  30. {
  31. PlayerPrefs.DeleteAll();
  32. }
  33. public static void Save()
  34. {
  35. PlayerPrefs.Save();
  36. }
  37. }
  38. }