Advertisement
Diamond32_Tutoriales

DataManager

Mar 30th, 2021
656
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.72 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class DataManager : MonoBehaviour
  6. {
  7.     public Transform ManagerSave;
  8.     public int ObjectAtSave;
  9.  
  10.     void Start()
  11.     {
  12.         StartCoroutine (LoadGame());
  13.     }
  14.  
  15.     public IEnumerator LoadGame()
  16.     {
  17.  
  18.         if (PlayerPrefs.HasKey ("TotalObjectsSaved") == true) {
  19.             ObjectAtSave = PlayerPrefs.GetInt ("TotalObjectsSaved");
  20.  
  21.             for (int i = 0; i < ObjectAtSave; i++)
  22.             {
  23.                 var LoadPrefab = Resources.Load (PlayerPrefs.GetString("DataSaved_" + i));
  24.                 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);
  25.                 yield return new WaitForSeconds (0.1f);
  26.             }
  27.         }
  28.         else
  29.         {
  30.             print ("No se detectaron Objetos guardados");
  31.         }
  32.     }
  33.  
  34.     void Update()
  35.     {
  36.         if (Input.GetKeyDown (KeyCode.O))
  37.         {
  38.             StartCoroutine (SaveGame());
  39.         }
  40.  
  41.         if (Input.GetKey (KeyCode.U))
  42.         {
  43.             PlayerPrefs.DeleteAll();
  44.             print ("Todo lo guardado se borro");
  45.         }
  46.     }
  47.  
  48.     public IEnumerator SaveGame()
  49.     {
  50.         ObjectAtSave = ManagerSave.childCount;
  51.  
  52.         PlayerPrefs.SetInt("TotalObjectsSaved", ObjectAtSave);
  53.  
  54.         for (int i = 0; i < ObjectAtSave; i++)
  55.         {
  56.             PlayerPrefs.SetString ("DataSaved_" + i, ManagerSave.GetChild(i).GetComponent <TypeSaved>().tipo.ToString());
  57.  
  58.             PlayerPrefs.SetFloat("PosSaved_X" + i, ManagerSave.GetChild(i).transform.position.x);
  59.             PlayerPrefs.SetFloat("PosSaved_Y" + i, ManagerSave.GetChild(i).transform.position.y);
  60.             PlayerPrefs.SetFloat("PosSaved_Z" + i, ManagerSave.GetChild(i).transform.position.z);
  61.  
  62.             PlayerPrefs.SetFloat("PosSaved_X" + i, ManagerSave.GetChild(i).transform.position.x);
  63.             PlayerPrefs.SetFloat("PosSaved_Y" + i, ManagerSave.GetChild(i).transform.position.y);
  64.             PlayerPrefs.SetFloat("PosSaved_Z" + i, ManagerSave.GetChild(i).transform.position.z);
  65.  
  66.             PlayerPrefs.SetFloat("RotSaved_X" + i, ManagerSave.GetChild(i).transform.localEulerAngles.x);
  67.             PlayerPrefs.SetFloat("RotSaved_Y" + i, ManagerSave.GetChild(i).transform.localEulerAngles.y);
  68.             PlayerPrefs.SetFloat("RotSaved_Z" + i, ManagerSave.GetChild(i).transform.localEulerAngles.z);
  69.  
  70.         }
  71.         print("Game Saved");
  72.         yield return new WaitForSeconds(0.1f);
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement