Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class DataManager : MonoBehaviour
- {
- public Transform ManagerSave;
- public int ObjectAtSave;
- void Start()
- {
- StartCoroutine (LoadGame());
- }
- public IEnumerator LoadGame()
- {
- if (PlayerPrefs.HasKey ("TotalObjectsSaved") == true) {
- ObjectAtSave = PlayerPrefs.GetInt ("TotalObjectsSaved");
- for (int i = 0; i < ObjectAtSave; i++)
- {
- var LoadPrefab = Resources.Load (PlayerPrefs.GetString("DataSaved_" + i));
- var objLoad = Instantiate (LoadPrefab, new Vector3 (PlayerPrefs.GetFloat("PosSaved_X" + i), PlayerPrefs.GetFloat("PosSaved_Y" + i), PlayerPrefs.GetFloat("PosSaved_Z" + i)), Quaternion.Euler(PlayerPrefs.GetFloat("RotSaved_X" + i), PlayerPrefs.GetFloat("RotSaved_Y" + i), PlayerPrefs.GetFloat("RotSaved_Z" + i)), ManagerSave);
- yield return new WaitForSeconds (0.1f);
- }
- }
- else
- {
- print ("No se detectaron Objetos guardados");
- }
- }
- void Update()
- {
- if (Input.GetKeyDown (KeyCode.O))
- {
- StartCoroutine (SaveGame());
- }
- if (Input.GetKey (KeyCode.U))
- {
- PlayerPrefs.DeleteAll();
- print ("Todo lo guardado se borro");
- }
- }
- public IEnumerator SaveGame()
- {
- ObjectAtSave = ManagerSave.childCount;
- PlayerPrefs.SetInt("TotalObjectsSaved", ObjectAtSave);
- for (int i = 0; i < ObjectAtSave; i++)
- {
- PlayerPrefs.SetString ("DataSaved_" + i, ManagerSave.GetChild(i).GetComponent <TypeSaved>().tipo.ToString());
- PlayerPrefs.SetFloat("PosSaved_X" + i, ManagerSave.GetChild(i).transform.position.x);
- PlayerPrefs.SetFloat("PosSaved_Y" + i, ManagerSave.GetChild(i).transform.position.y);
- PlayerPrefs.SetFloat("PosSaved_Z" + i, ManagerSave.GetChild(i).transform.position.z);
- PlayerPrefs.SetFloat("PosSaved_X" + i, ManagerSave.GetChild(i).transform.position.x);
- PlayerPrefs.SetFloat("PosSaved_Y" + i, ManagerSave.GetChild(i).transform.position.y);
- PlayerPrefs.SetFloat("PosSaved_Z" + i, ManagerSave.GetChild(i).transform.position.z);
- PlayerPrefs.SetFloat("RotSaved_X" + i, ManagerSave.GetChild(i).transform.localEulerAngles.x);
- PlayerPrefs.SetFloat("RotSaved_Y" + i, ManagerSave.GetChild(i).transform.localEulerAngles.y);
- PlayerPrefs.SetFloat("RotSaved_Z" + i, ManagerSave.GetChild(i).transform.localEulerAngles.z);
- }
- print("Game Saved");
- yield return new WaitForSeconds(0.1f);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement