I want to call this function when a save button is clicked.
public static void Load() {
Load(“savedata.json”);
}
using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.SceneManagement;
public static class SaveSystem {
public static bool HasUnsavedChanges { get; private set; }
public static string Username {
get {
return _data.Username;
}
set {
if (_data.Username != value) {
HasUnsavedChanges = true;
_data.Username = value;
}
}
}
public static Pronouns Pronouns {
get {
return _data.Pronouns;
}
set {
if (_data.Pronouns == null || !_data.Pronouns.Equals(value)) {
HasUnsavedChanges = true;
_data.Pronouns = value;
}
}
}
public static int GradeLevel {
get {
return _data.GradeLevel;
}
set {
if (_data.GradeLevel != value) {
HasUnsavedChanges = true;
_data.GradeLevel = value;
}
}
}
public static CharacterSettings CharacterSettings {
get {
if (_data.CharacterSettings == null) {
_data.CharacterSettings = new CharacterSettings();
_data.CharacterSettings.OnSettingsChanged += OnObjectChanged;
HasUnsavedChanges = true;
}
return _data.CharacterSettings;
}
}
public static SaveData.Language LanguageSelection {
get {
return _data.LanguageSelection;
}
set {
if (_data.LanguageSelection != value) {
HasUnsavedChanges = true;
_data.LanguageSelection = value;
}
}
}
public static SaveData.Theme ThemeSelection => _data.ThemeSelection;
public static string ActiveSceneName => _data.ActiveSceneName;
public static SaveData.ProgressStage Progress => _data.Progress;
public static List<string> NpcsTalkedTo {
get {
var result = new List<string>();
foreach (var dialogue in _data.AllDialogue) {
result.Add(dialogue.NpcName);
}
return result;
}
}
// Create private container for all save data in the save system
private static SaveData _data = new SaveData() {
Username = null,
Pronouns = null,
GradeLevel = -1,
CharacterSettings = null,
ThemeSelection = SaveData.Theme.Night,
LanguageSelection = SaveData.Language.English,
ActiveSceneName = SceneManager.GetActiveScene().name,
Progress = SaveData.ProgressStage.None,
AllDialogue = new List<Dialogue>()
};
/// <summary>
/// Subscribe to the activeSceneChanged event on static construction.
/// </summary>
static SaveSystem() {
SceneManager.activeSceneChanged += OnActiveSceneChanged;
}
/// <summary>
/// Saves all data contained within the stored SaveData class.
/// </summary>
public static void Save() {
Save("savedata.json");
}
/// <summary>
/// Saves all data contained within the stored SaveData class.
/// </summary>
/// <param name="filename">The name of the file to load. Must end in ".json" or ".txt".</param>
public static void Save(string filename) {
if (HasUnsavedChanges) {
string filepath = Path.Combine(Application.persistentDataPath, filename);
// Serialize the data into a JSON and store it
string json = JsonUtility.ToJson(_data);
File.WriteAllText(filepath, json);
HasUnsavedChanges = false;
}
}
/// <summary>
/// Loads all data from a existing file into the SaveData class and automatically changes the
/// scene in Unity to the saved scene in the file. This method does not check whether there are
/// unsaved changes before loading a file.
/// </summary>
public static void Load() {
Load("savedata.json");
}
/// <summary>
/// Loads all data from a existing file into the SaveData class and automatically changes the
/// scene in Unity to the saved scene in the file. This method does not check whether there are
/// unsaved changes before loading a file.
/// </summary>
/// <param name="filename">The name of the file to load. Must end in ".json" or ".txt".</param>
public static void Load(string filename) {
string filepath = Path.Combine(Application.persistentDataPath, filename);
// Deserialize the json file and store it back into SaveData
string json = File.ReadAllText(filepath);
_data = JsonUtility.FromJson<SaveData>(json);
// Load the saved active scene if not loaded already
if (SceneManager.GetActiveScene().name != _data.ActiveSceneName) {
SceneManager.LoadScene(_data.ActiveSceneName);
}
HasUnsavedChanges = false;
}
/// <summary>
/// Toggles the day/night selection between the two display modes.
/// </summary>
public static void ToggleDayNightSelection() {
switch (_data.ThemeSelection) {
case SaveData.Theme.Day:
_data.ThemeSelection = SaveData.Theme.Night;
HasUnsavedChanges = true;
break;
case SaveData.Theme.Night:
_data.ThemeSelection = SaveData.Theme.Day;
HasUnsavedChanges = true;
break;
default:
Debug.LogError("DayNightSelection does not exist");
break;
}
}
/// <summary>
/// Used to get the dialogue of a specific NPC.
/// </summary>
/// <param name="npcName">The name of the NPC the player has talked to.</param>
/// <returns>All dialogue of the NPC.</returns>
public static Dialogue GetDialogueOf(string npcName) {
Dialogue result = null;
// Search for the dialogue to see if it exists
int iterator = 0;
while (result == null && iterator < _data.AllDialogue.Count) {
if (_data.AllDialogue[iterator].NpcName == npcName) {
result = _data.AllDialogue[iterator];
}
iterator++;
}
// Create a new dialogue if none exists
if (result == null) {
result = new Dialogue(npcName);
result.DialogueChanged += OnObjectChanged;
_data.AllDialogue.Add(result);
}
return result;
}
/// <summary>
/// Changes the stage of the story the player is currently in to the next one.
/// </summary>
public static void GoToNextProgressStage() {
switch (_data.Progress) {
case SaveData.ProgressStage.None:
_data.Progress = SaveData.ProgressStage.OfferedToJoinHackerHero;
HasUnsavedChanges = true;
break;
case SaveData.ProgressStage.OfferedToJoinHackerHero:
_data.Progress = SaveData.ProgressStage.JoinedHackerHero;
HasUnsavedChanges = true;
break;
case SaveData.ProgressStage.JoinedHackerHero:
_data.Progress = SaveData.ProgressStage.CompletedIntro;
HasUnsavedChanges = true;
break;
case SaveData.ProgressStage.CompletedIntro:
Debug.LogError("Player is already at the final stage: " + _data.Progress.ToString());
break;
default:
Debug.LogError("Cannot go to next stage. Current stage is: " + _data.Progress.ToString());
break;
}
}
private static void OnObjectChanged(object sender, EventArgs e) {
HasUnsavedChanges = true;
}
private static void OnActiveSceneChanged(Scene current, Scene next) {
_data.ActiveSceneName = next.name;
HasUnsavedChanges = true;
}
}