Chocolade19721309

Untitled

Jun 4th, 2024
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.33 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using UnityEngine;
  7. using UnityEngine.SceneManagement;
  8. using UnityEngine.UI;
  9.  
  10. public class SaveLoad : MonoBehaviour
  11. {
  12. public FadeInOutSaveGameText fadeInOutSaveGame;
  13. public float timeToStartSaving;
  14. public float savingFadeInOutTime;
  15. public int resWidth = 1920;
  16. public int resHeight = 1080;
  17.  
  18. [Tooltip("If true will save manualy only by the left control key.")]
  19. public bool saveManual = false;
  20.  
  21. private List<GameObject> objectsToSave;
  22. private string saveString;
  23.  
  24. private void Awake()
  25. {
  26. if (objectsToSave == null)
  27. {
  28. objectsToSave = new List<GameObject>();
  29. }
  30.  
  31. //objectsToSave = GameObject.FindGameObjectsWithTag("My Unique ID").ToList();
  32. var objectsWithGenerateGuid = GameObject.FindObjectsOfType<GenerateGuid>().ToList();
  33. if (objectsWithGenerateGuid.Count > 0 && objectsToSave.Count == 0)
  34. {
  35. for (int i = 0; i < objectsWithGenerateGuid.Count; i++)
  36. {
  37.  
  38. // 29ad9dc9-80c2-498a-bda1-bce883df7971
  39.  
  40. // 29ad9dc9-80c2-498a-bda1-bce883df7971
  41.  
  42. objectsToSave.Add(objectsWithGenerateGuid[i].gameObject);
  43. }
  44. }
  45.  
  46. Debug.Log("Start");
  47.  
  48. for (int i = 0; i < objectsToSave.Count; i++)
  49. {
  50. Debug.Log($"{i}");
  51. Debug.Log($"{objectsToSave[i].name}");
  52. }
  53.  
  54. Debug.Log("End Init");
  55.  
  56. // Here to find all gameobjects with the GenerateGuid script attached to them as gameobjects
  57. // To be saved instead finding them by tag.
  58. // And change now all this gameobjects tag to be untagged !
  59. }
  60.  
  61. public void Save()
  62. {
  63. SaveGame saveGame = new SaveGame();
  64. saveGame.saveObjects = new List<SaveObject>();
  65. for (int i = 0; i < objectsToSave.Count; i++)
  66. {
  67. SaveObject saveObject = new SaveObject();
  68. saveObject.transformSaver = new TransformSaver();
  69. // Need to change everywhere need from GetInstanceID and gameObjectInstanceID to
  70. // The new List objectsToSave to get from each object to save the component script
  71. // GenerateGuid get from it the string guid and convert the string to int.
  72. Debug.Log($"{i}");
  73. Debug.Log($"{objectsToSave[i].name}");
  74. saveObject.gameObjectUniqueID = objectsToSave[i].GetComponent<GenerateGuid>().uniqueGuidID;
  75. var x = objectsToSave[i].GetComponents<Component>();
  76. var stateQueryComponent = x.Where(component => component is IStateQuery).ToList();
  77. List<KeyToValue> componentsState = new List<KeyToValue>();
  78. foreach (var z in stateQueryComponent)
  79. {
  80. var w = z as IStateQuery;
  81. componentsState.Add(new KeyToValue(w.UniqueId.ToString(), w.GetState()));
  82. }
  83.  
  84. saveObject.transformSaver.position = objectsToSave[i].transform.position;
  85. saveObject.transformSaver.rotation = objectsToSave[i].transform.rotation;
  86. saveObject.transformSaver.scaling = objectsToSave[i].transform.localScale;
  87.  
  88. saveObject.componentsState = componentsState;
  89. saveGame.saveObjects.Add(saveObject);
  90. }
  91.  
  92. string json = JsonUtility.ToJson(saveGame);
  93. //StartCoroutine(fadeInOutSaveGame.CanvasAlphaChangeOverTime(3f,true));
  94.  
  95. SaveSystem.Save(json, resWidth, resHeight);
  96. //StartCoroutine(fadeInOutSaveGame.CanvasAlphaChangeOverTime(3f, false));
  97. }
  98.  
  99. public void Load(string fileName)
  100. {
  101. if (objectsToSave.Count > 0)
  102. {
  103. // Why objectsToSave here is empty ?
  104. Dictionary<string, GameObject> uniqueIdToObject = objectsToSave
  105. .ToDictionary(o => o.GetComponent<GenerateGuid>().uniqueGuidID, o => o);
  106.  
  107. saveString = SaveSystem.Load(fileName);
  108.  
  109. if (saveString != null)
  110. {
  111. SaveGame saveGame = JsonUtility.FromJson<SaveGame>(saveString);
  112.  
  113. if (saveGame != null)
  114. {
  115. foreach (var saveObject in saveGame.saveObjects)
  116. {
  117. List<KeyToValue> loadedComponents = saveObject.componentsState;
  118. var objectToSetState = uniqueIdToObject[saveObject.gameObjectUniqueID];
  119.  
  120. objectToSetState.transform.position = saveObject.transformSaver.position;
  121. objectToSetState.transform.rotation = saveObject.transformSaver.rotation;
  122. objectToSetState.transform.localScale = saveObject.transformSaver.scaling;
  123.  
  124. var y = objectToSetState.GetComponents<Component>();
  125. var z = y.Where(component => component is IStateQuery).ToList();
  126. Dictionary<string, IStateQuery> zz = z.ToDictionary(sq => (sq as IStateQuery).UniqueId.ToString(), sq => sq as IStateQuery);
  127.  
  128. foreach (KeyToValue keyvalue in loadedComponents)
  129. {
  130. zz[keyvalue.Key].SetState(keyvalue.Value);
  131. }
  132. }
  133. }
  134. }
  135. }
  136. }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment