XMLPrefs.cs 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Xml.Serialization;
  4. using System.IO;
  5. //DO NOT USE EXCEPT FOR DEBUGGING since it is slow
  6. //Based off http://entitycrisis.blogspot.com/2011/01/store-objects-in-playerprefs.html
  7. public class XMLPrefs
  8. {
  9. public static void Save<T>(string name, T instance)
  10. {
  11. AVDebug.LogWarning("DO NOT USE EXCEPT FOR DEBUGGING since it is slow");
  12. XmlSerializer serializer = new XmlSerializer(typeof(T));
  13. using (var ms = new MemoryStream ())
  14. {
  15. serializer.Serialize(ms, instance);
  16. PlayerPrefs.SetString(name, System.Text.ASCIIEncoding.ASCII.GetString(ms.ToArray()));
  17. }
  18. }
  19. public static T Load<T>(string name)
  20. {
  21. AVDebug.LogWarning("DO NOT USE EXCEPT FOR DEBUGGING since it is slow");
  22. if (!PlayerPrefs.HasKey(name))
  23. {
  24. return default(T);
  25. }
  26. XmlSerializer serializer = new XmlSerializer(typeof(T));
  27. T instance;
  28. using (var ms = new MemoryStream (System.Text.ASCIIEncoding.ASCII.GetBytes (PlayerPrefs.GetString (name))))
  29. {
  30. instance = (T)serializer.Deserialize(ms);
  31. }
  32. return instance;
  33. }
  34. }