- Home /
The question is answered, right answer was accepted
UnitySerializer Delete Saved Games
I am trying to add a way for the player to delete their saved games. I am using Unity Serializer to create and load the save games but I have read though all the documentation and I can not figure out how to allow the player to delete a saved game. I have reworkd the save game some here is the script I have so far. I would also like to limit the saves they can have down to 5 but not sure that is just a side task though if the player could delete them I would be happy just not sure how to do it.
using UnityEngine;
using System.Collections;
public class GrassPause : MonoBehaviour
{
public GUISkin Box;
public GUISkin Box2;
private string box;
public string gameName = "Your Game";
bool paused;
void Update ()
{
if(Input.GetKeyUp(KeyCode.P))
{
paused = !paused;
if(paused == true){
Time.timeScale = 0.0f;
//if(pausedGUI) pausedGUI.enabled = true;
} else {
Time.timeScale = 1.0f;
// if(pausedGUI) pausedGUI.enabled = false;
}
}
}
void OnGUI ()
{
if (paused)
{
GUILayout.BeginArea(new Rect(400,200, 300, 400));
GUILayout.BeginVertical();
GUILayout.FlexibleSpace();
GUI.skin = Box;
if(GUI.Button(new Rect(55,0,200,55),box))
{
LevelSerializer.SaveGame(gameName);
}
GUILayout.Space(5);
foreach(var sg in LevelSerializer.SavedGames[LevelSerializer.PlayerName])
{
if(GUILayout.Button(sg.Caption))
{
LevelSerializer.LoadNow(sg.Data);
Time.timeScale = 1;
}
}
GUILayout.EndVertical();
GUILayout.FlexibleSpace();
GUILayout.EndArea();
}
}
}
I found a way to add a button to delete all the saved games but I don't think any one would like to delete all most of the time they just want to delete one save game. Any way on doing this?
you could do something like:
foreach(var sg in LevelSerializer.SavedGames[LevelSerializer.PlayerName])
{
if(GUILayout.Button("Delete: " + sg.Caption))
{
sg.Delete();
}
}
if you want to limit the savegames you can just setup a counter and check it before saving
Sinply define a new integer: int _savedGames = 0;
And replace LevelSerializer.SaveGame(name) with:
if(_savedGames <= 5)
{
LevelSerializer.SaveGame(gameName);
_savedGames++;
}
And after the delete Line put
_savedGames--;
Hope it helps
I forgot about it but in your Awake function you can set
LevelSerializer.$$anonymous$$axGames = 5;
Which is much easier :)
And forget about the _savedGames variable (delete the lines you added for it
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Working With GUI 1 Answer
How do I center the gui created by this script? 0 Answers
UnityEngine.Input.GetMouseButton(1)) issue 1 Answer
How To Make GUI Buttons Load/Quit 1 Answer