- Home /
Long unattended QA with too broad a question
Saving System?
Hello, I am trying to make a saving system for my game. What I need to save is the players position, as well as its child's (which should just only need the players). I also need to be able to save all the objects created in the game. Since this is dynamic, so will the code. This meaning, the users can spawn objects. Therefore I would need the positions and rotations of every spawned object. Is this possible? I don't think playerprefs can handle this much of a saving system. Also, the system needs to be able to reopen this back up when the user uses a load feature, which I would also need.
If anyone can help me with a tutorial or something on the basics of this, I might be able to just get the rest of it on my own, if not I can just make another question on the forums or here asking the part I need help on.
Thanks, Brandon
I have, I don't think they can hold what I am trying to do.
then try it with streamreader adn streamwriter class from c#(check msdn for info)
I think that it is better to use serialisation instead of PlayerPrefs Here is code that I`m using:
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using UnityEngine;
public static class FileManager
{
public static void SetUpData<T>(T Data, string Path)
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream file = File.Open(Path, FileMode.OpenOrCreate);
formatter.Serialize(file, Data);
file.Close();
}
public static T GetData<T>(string Path)
{
if (File.Exists(Path))
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream file = File.Open(Path, FileMode.Open);
T data = (T)formatter.Deserialize(file);
file.Close();
return data;
}
return default(T);
}
}
[System.Serializable]
public class Data
{
//place all that you need to save here as variables
}
An example:
public class ExampleClass : MonoBehaviour
{
public Data data;
public const string FileName = "/PlayerData.data";
private void Start()
{
data = FileManager.GetData<Data>(Application.persistentDataPath + FileName);
}
//do smth
public void Save()
{
FileManager.SetUpData(data,Application.persistentDataPath+FileName);
}
}
[System.Serializable]
public class Data
{
public Vector3 PlayerPosition;
public Vector3 ChildPosition;
public List<SpawnedObject> spawnedObjects = new List<SpawnedObject>();
}
public struct SpawnedObject
{
public Vector3 Position;
public Vector3 Rotation;
}
Answer by FTheCloud · Feb 12, 2012 at 09:13 PM
Flamy's right. You've got to use player prefs. But for what you want to do its going to take a lot of work.
What you need to do is store the object's position and rotation values in the player prefs. Then when you start up your scene again you set the object's position and rotation values according to the ones you previously stored.
It will be a ton of work though if you have a lot of objects in which you want to save their position and rotation in the scene. Player prefs should be able to handle the amount of information.