Advertisement
Guest User

SaveSystem

a guest
Jan 21st, 2018
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. public class SaveSystem : MonoBehaviour
  2. {
  3. #region Instance
  4. public static SaveSystem Instance;
  5.  
  6. public void Awake()
  7. {
  8. if (Instance != null)
  9. Instance = this;
  10. }
  11. #endregion
  12.  
  13. public Dictionary<int, ObjectData> LevelData = new Dictionary<int, ObjectData>();
  14.  
  15. public bool HasObject(int ID)
  16. {
  17. return LevelData.ContainsKey(ID);
  18. }
  19.  
  20. public ObjectData GetObject(int ID)
  21. {
  22. if (HasObject(ID))
  23. return LevelData[ID];
  24. else
  25. return null;
  26. }
  27.  
  28. public void RegisterObject(int ID, ObjectData data)
  29. {
  30. LevelData.Add(ID, data);
  31. }
  32.  
  33. public void UpdateData(int ID, ObjectData newData)
  34. {
  35. LevelData[ID] = newData;
  36. }
  37.  
  38. public void OnDestroy() // when we quit the game save
  39. {
  40. Save();
  41. }
  42.  
  43. public void Save()
  44. {
  45. string json = JsonUtility.ToJson(LevelData);
  46. //save the json in a file
  47. }
  48.  
  49. public void Load(string json)
  50. {
  51. LevelData = JsonUtility.FromJson<Dictionary<int, ObjectData>>(json);
  52. //TODO create all the objects, again
  53. }
  54. }
  55.  
  56. public class ObjectData
  57. {
  58. public Vector3 Position;
  59. public Quaternion Rotation;
  60.  
  61. public bool Destroied;
  62.  
  63. public ObjectData(Vector3 pos, Quaternion rot, bool des)
  64. {
  65. this.Position = pos;
  66. this.Rotation = rot;
  67. this.Destroied = des;
  68. }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement