For saving/loading you should look into Player Preferences, specifically, PlayerPrefs.SetString/GetString.
docs.unity3d.com/Documentation/ScriptReference/PlayerPrefs.html
docs.unity3d.com/Documentation/ScriptReference/PlayerPrefs.SetString.html
docs.unity3d.com/Documentation/ScriptReference/PlayerPrefs.GetString.html
Sorry, the hyperlink button isn't working on my browser.
I tested it and if I understand correctly this C# code should work:
using UnityEngine;
using System.Collections;
public class SaveLoad : MonoBehaviour {
public string[] loadedData;
public int convertBack;
// Update is called once per frame
void Update ()
{
// Easy way to test it press "S" in game to save and "L" to load
if(Input.GetKeyDown("s"))
SaveCubos();
if(Input.GetKeyDown("l"))
LoadCubos();
}
public void SaveCubos()
{
GameObject[] cubos = GameObject.FindGameObjectsWithTag("Cubos"); // If you create a new tag "Cubos" and tag all of the cubos with it
string saveData = "";
foreach(GameObject specificCubo in cubos)
{
EspacioCreacion cuboScript = specificCubo.GetComponent();
saveData = saveData + cuboScript.cubo.ToString() + ","; // The additional comma is for splitting the string later
}
Debug.Log(saveData); // To display what information we actually saved
PlayerPrefs.SetString("SaveData", saveData); // Actually save the information to PlayerPrefs
}
public void LoadCubos()
{
string loadData = PlayerPrefs.GetString("SaveData"); // Retrieve the information we saved
loadedData = loadData.Split(','); // Divides the information up based on the commas "," into a string array
// To convert the information back into integers or floats you could use:
convertBack = int.Parse(loadedData[0]);
}
}
I guess it matters whether you want to assign the data back to a specific cube. If so you will probably need to save more information that associates the "cubos" with each specific cube. You will also need to tag each object with the "Cubos" tag.
I hope that answers your question.
Thanks, God bless!
Howey
↧