[HOWTO] How To: Include Summary Info In Saved Games
Posted: Mon Sep 23, 2019 10:43 am
Someone asked how to include summary info in saved games.
There are generally two approaches. The first is to maintain a separate file (or files) containing summary information about each saved game. The second is to put each saved game's summary info in the saved game itself. In this post, I'll describe how to do the second approach.
Write a new Saver script to hold the summary info. It might look something like below. (I'm going to abbreviate it a bit.)
Add this script to the Dialogue Manager, and give it a unique key such as "GameSummary". Now, when you save a game, it will save sceneName, totalPlayTime, etc., under the key "GameSummary".
If you need to save image data such as a screenshot of the current gameplay screen, add a byte[] array to Data. Use ImageConversion.EncodeToJPG (or PNG, etc.) to convert the screenshot to the byte[] array. To turn it back into an image, use ImageConversion.LoadImage. If you need to save a player character portrait, you may find it easier to save the name or index of the portrait and look up the image by this name or index instead of serializing the entire image.
When showing your load game menu, call SaveSystem.HasSavedGameInSlot(#).
If it's false, show the slot as "No Save".
If it's true, then call SaveSystem.storer.RetrieveSavedGameData(#). This will return a SavedGameData object. Get the game summary:
Then you can show summary.sceneName, summary.totalPlayTime, etc., in your load game menu.
---
Added - How To Save Screenshot:
Use ImageConversion.LoadImage() to convert the byte[] array back to a texture.
There are generally two approaches. The first is to maintain a separate file (or files) containing summary information about each saved game. The second is to put each saved game's summary info in the saved game itself. In this post, I'll describe how to do the second approach.
Write a new Saver script to hold the summary info. It might look something like below. (I'm going to abbreviate it a bit.)
Code: Select all
public class GameSummarySaver : Saver
{
[Serializable]
public class Data
{
public string sceneName;
public string totalPlayTime;
public int characterLevel;
}
public override string RecordData()
{
var data = new Data();
data.sceneName = SceneManager.GetActiveScene().name;
// ... etc. for everything in Data ...
return SaveSystem.Serialize(data);
}
public override void ApplyData(string s)
{
// Do nothing. We only record the data for the save/load menu.
// There's no need to re-apply it when loading a saved game.
}
}
If you need to save image data such as a screenshot of the current gameplay screen, add a byte[] array to Data. Use ImageConversion.EncodeToJPG (or PNG, etc.) to convert the screenshot to the byte[] array. To turn it back into an image, use ImageConversion.LoadImage. If you need to save a player character portrait, you may find it easier to save the name or index of the portrait and look up the image by this name or index instead of serializing the entire image.
When showing your load game menu, call SaveSystem.HasSavedGameInSlot(#).
If it's false, show the slot as "No Save".
If it's true, then call SaveSystem.storer.RetrieveSavedGameData(#). This will return a SavedGameData object. Get the game summary:
Code: Select all
var savedGameData = SaveSystem.storer.RetrieveSavedGameData(#);
var s = savedGameData.GetData("GameSummary");
var summary = SaveSystem.Deserialize<GameSummarySaver.Data>(s);
---
Added - How To Save Screenshot:
Code: Select all
public byte[] GetScreenshotBytes()
{
var screenshotTexture = TakeScreenshot();
return ImageConversion.EncodeToJPG(screenshotTexture);
}
public Texture2D TakeScreenshot()
{
var screenshotTexture = new Texture2D(Screen.width, Screen.height);
screenshotTexture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
screenshotTexture.Apply ();
return screenshotTexture;
}