- Home /
Save and Load Game
Hi guys.I want to ask something about save and load game. In my game there are two level and I did scene that you can choose level. And I want to if you don't complete the first level , you can't choose second level. And you complete the first level the game save outomaticly and you can choose second level another time in scene. How can I do? Thank you in advance. (If you can, please javascript)
Are you talking about saving a lot of data, like what objects the player has collected etc., or just to save the fact that the player has completed the level?
Time and score is not a lot. An array of all objects in the game, along with their state - that is a lot.
The reason for me asking was that if you just want to maintain some data elements between game sessions, like score, time, highest level achieved and so on - the easiest way to do it is to use PlayerPrefs.
Okey but how can I use PlayerPrefs ? For now I want to do only level control. Namely if you complete first level , you can play second level after open the game(you can choose 2nd level in scene of level)if you don't , you can't choose
I have added an answer below. For starters, I would recommend you create a new empty script, and play with PlayerPrefs a little to understand how it works. Then you should be able to easily see how (and if) it fits your need.
Answer by tw1st3d · Oct 06, 2012 at 10:37 PM
You will need to learn about a specific function, known as UTF8Encoding();
This isn't exactly what you want, but this IS a load/save script.
import System;
import System.Collections;
import System.Xml;
import System.Xml.Serialization;
import System.IO;
import System.Text;
class TempData
{
var x : float;
var y : float;
var z : float;
var name : String;
}
class UsernameData
{
public var _iUser : TempData = new TempData();
function UsernameData() { }
}
private var _Save : Rect;
private var _Load : Rect;
private var _SaveMSG : Rect;
private var _LoadMSG : Rect;
private var _FileLocation : String;
private var _FileName : String = "SaveData.xml";
var _Player : GameObject;
var _PlayerName : String = "Joe Schmoe";
private var myData : UsernameData;
private var _data : String;
private var VPosition : Vector3;
function Awake () {
_FileLocation=Application.dataPath;
myData=new UsernameData();
}
function UTF8ByteArrayToString(characters : byte[] )
{
var encoding : UTF8Encoding = new UTF8Encoding();
var constructedString : String = encoding.GetString(characters);
return (constructedString);
}
function StringToUTF8ByteArray(pXmlString : String)
{
var encoding : UTF8Encoding = new UTF8Encoding();
var byteArray : byte[] = encoding.GetBytes(pXmlString);
return byteArray;
}
function SerializeObject(pObject : Object)
{
var XmlizedString : String = null;
var memoryStream : MemoryStream = new MemoryStream();
var xs : XmlSerializer = new XmlSerializer(typeof(UsernameData));
var xmlTextWriter : XmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
xs.Serialize(xmlTextWriter, pObject);
memoryStream = xmlTextWriter.BaseStream;
XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
return XmlizedString;
}
function DeserializeObject(pXmlizedString : String)
{
var xs : XmlSerializer = new XmlSerializer(typeof(UsernameData));
var memoryStream : MemoryStream = new MemoryStream(StringToUTF8ByteArray(pXmlizedString));
var xmlTextWriter : XmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
return xs.Deserialize(memoryStream);
}
function CreateXML()
{
var writer : StreamWriter;
var t : FileInfo = new FileInfo(_FileLocation+"/"+ _FileName);
if(!t.Exists)
{
writer = t.CreateText();
}
else
{
t.Delete();
writer = t.CreateText();
}
writer.Write(_data);
writer.Close();
Debug.Log("File written.");
}
function LoadXML()
{
var r : StreamReader = File.OpenText(_FileLocation+"/"+ _FileName);
var _info : String = r.ReadToEnd();
r.Close();
_data=_info;
Debug.Log("File Read");
}
Best of luck.
Answer by DannyB · Oct 07, 2012 at 12:12 PM
Based on your specific use case and the comments to the question, I would like to suggest PlayerPrefs.
When the user completes the level, you save the highest unlocked level, like this:
PlayerPrefs.SetInt("HighestLevel", 2);
When you load the game, you check what is the highest unlocked level, like this:
HighestLevel = PlayerPrefs.GetInt("HighestLevel", 1);
The second argument is optional, to say what is the default value.
Finally, in your screen where the player can choose a level to play, you check against this HighestLevel variable to see which buttons you need to display.
In the same way of course, you can store the score instead of (or in addition to-) the highest level.
Lastly, here is a quick link to the PlayerPrefs documentation
Let me know if something is not clear.
Your answer
![](https://koobas.hobune.stream/wayback/20220613081954im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Load last checkpoint from main menu 2 Answers
Save game and load 2 Answers
Save/ Load game script in main menu 1 Answer
The name "Game Foundation" does not exist in the current context 1 Answer
saving the game in position 1 Answer