Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- using UnityEngine.UI;
- public class SaveLoad : MonoBehaviour
- {
- public FadeInOutSaveGameText fadeInOutSaveGame;
- public float timeToStartSaving;
- public float savingFadeInOutTime;
- public int resWidth = 1920;
- public int resHeight = 1080;
- [Tooltip("If true will save manualy only by the left control key.")]
- public bool saveManual = false;
- private List<GameObject> objectsToSave;
- private string saveString;
- private void Awake()
- {
- if (objectsToSave == null)
- {
- objectsToSave = new List<GameObject>();
- }
- //objectsToSave = GameObject.FindGameObjectsWithTag("My Unique ID").ToList();
- var objectsWithGenerateGuid = GameObject.FindObjectsOfType<GenerateGuid>().ToList();
- if (objectsWithGenerateGuid.Count > 0 && objectsToSave.Count == 0)
- {
- for (int i = 0; i < objectsWithGenerateGuid.Count; i++)
- {
- // 29ad9dc9-80c2-498a-bda1-bce883df7971
- // 29ad9dc9-80c2-498a-bda1-bce883df7971
- objectsToSave.Add(objectsWithGenerateGuid[i].gameObject);
- }
- }
- Debug.Log("Start");
- for (int i = 0; i < objectsToSave.Count; i++)
- {
- Debug.Log($"{i}");
- Debug.Log($"{objectsToSave[i].name}");
- }
- Debug.Log("End Init");
- // Here to find all gameobjects with the GenerateGuid script attached to them as gameobjects
- // To be saved instead finding them by tag.
- // And change now all this gameobjects tag to be untagged !
- }
- public void Save()
- {
- SaveGame saveGame = new SaveGame();
- saveGame.saveObjects = new List<SaveObject>();
- for (int i = 0; i < objectsToSave.Count; i++)
- {
- SaveObject saveObject = new SaveObject();
- saveObject.transformSaver = new TransformSaver();
- // Need to change everywhere need from GetInstanceID and gameObjectInstanceID to
- // The new List objectsToSave to get from each object to save the component script
- // GenerateGuid get from it the string guid and convert the string to int.
- Debug.Log($"{i}");
- Debug.Log($"{objectsToSave[i].name}");
- saveObject.gameObjectUniqueID = objectsToSave[i].GetComponent<GenerateGuid>().uniqueGuidID;
- var x = objectsToSave[i].GetComponents<Component>();
- var stateQueryComponent = x.Where(component => component is IStateQuery).ToList();
- List<KeyToValue> componentsState = new List<KeyToValue>();
- foreach (var z in stateQueryComponent)
- {
- var w = z as IStateQuery;
- componentsState.Add(new KeyToValue(w.UniqueId.ToString(), w.GetState()));
- }
- saveObject.transformSaver.position = objectsToSave[i].transform.position;
- saveObject.transformSaver.rotation = objectsToSave[i].transform.rotation;
- saveObject.transformSaver.scaling = objectsToSave[i].transform.localScale;
- saveObject.componentsState = componentsState;
- saveGame.saveObjects.Add(saveObject);
- }
- string json = JsonUtility.ToJson(saveGame);
- //StartCoroutine(fadeInOutSaveGame.CanvasAlphaChangeOverTime(3f,true));
- SaveSystem.Save(json, resWidth, resHeight);
- //StartCoroutine(fadeInOutSaveGame.CanvasAlphaChangeOverTime(3f, false));
- }
- public void Load(string fileName)
- {
- if (objectsToSave.Count > 0)
- {
- // Why objectsToSave here is empty ?
- Dictionary<string, GameObject> uniqueIdToObject = objectsToSave
- .ToDictionary(o => o.GetComponent<GenerateGuid>().uniqueGuidID, o => o);
- saveString = SaveSystem.Load(fileName);
- if (saveString != null)
- {
- SaveGame saveGame = JsonUtility.FromJson<SaveGame>(saveString);
- if (saveGame != null)
- {
- foreach (var saveObject in saveGame.saveObjects)
- {
- List<KeyToValue> loadedComponents = saveObject.componentsState;
- var objectToSetState = uniqueIdToObject[saveObject.gameObjectUniqueID];
- objectToSetState.transform.position = saveObject.transformSaver.position;
- objectToSetState.transform.rotation = saveObject.transformSaver.rotation;
- objectToSetState.transform.localScale = saveObject.transformSaver.scaling;
- var y = objectToSetState.GetComponents<Component>();
- var z = y.Where(component => component is IStateQuery).ToList();
- Dictionary<string, IStateQuery> zz = z.ToDictionary(sq => (sq as IStateQuery).UniqueId.ToString(), sq => sq as IStateQuery);
- foreach (KeyToValue keyvalue in loadedComponents)
- {
- zz[keyvalue.Key].SetState(keyvalue.Value);
- }
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment