Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.IO;
  5.  
  6. public class GameManager : MonoBehaviour
  7. {
  8. public Transform Player;
  9. // Start is called before the first frame update
  10. void Start()
  11. {
  12.  
  13. }
  14.  
  15. // Update is called once per frame
  16. void Update()
  17. {
  18.  
  19. }
  20.  
  21. public string tempJson = "";
  22. public void SaveAction() {
  23. PlayerData data = new PlayerData();
  24. data.position = Player.position;
  25. data.life = 75;
  26. string dataString = JsonUtility.ToJson(data);
  27. // tempJson = dataString;
  28. GameManager.Save(dataString);
  29. Debug.Log("Save Action");
  30. Debug.Log("Data string: " + dataString);
  31. }
  32.  
  33. public void LoadAction() {
  34. string dataString = GameManager.Load();
  35. PlayerData data = JsonUtility.FromJson<PlayerData>(dataString);
  36. Player.position = data.position;
  37. }
  38.  
  39. public static void Save(string jsonString, string filename = "save.json", string storePath = null) {
  40. if (storePath == null) {
  41. storePath = Application.dataPath + "/saves";
  42. }
  43. if (!Directory.Exists(storePath)) {
  44. Directory.CreateDirectory(storePath);
  45. }
  46. string fullPath = storePath + "/" + filename;
  47. File.WriteAllText(fullPath, jsonString);
  48. }
  49.  
  50. public static string Load(string filename = "save.json", string storePath = null) {
  51. if (storePath == null) {
  52. storePath = Application.dataPath + "/saves";
  53. }
  54. string fullPath = storePath + "/" + filename;
  55. string loaded = null;
  56. if (File.Exists(fullPath)) {
  57. loaded = File.ReadAllText(fullPath);
  58. }
  59. return loaded;
  60. }
  61. }
  62.  
  63. public class PlayerData {
  64. public Vector3 position;
  65. public float life;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement