Sorry for asking this, however I’ve looked at a dozen or more ave/Load examples.
I’m new to Unity, and I have not seen a single example that
A) works and or provides sample script that works without errors.
B) none actually show working example of prompting the user for a NAME of the saved game,
OR ask the player what game to load.
The closest I’ve fount is the script below, I know it is saving the game, however I can’t tell if it is actually loading the name.
How do I get the script to ask the player for saved game name to save, or load?
Thanks!
My guess is that I’m missing something really simple (stupid) that is preventing me from
assigning a name to the saved game, and then recalling it at the load game function.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
[System.Serializable]
public class Game
{
public static Game current;
public CharacterInfo knight;
public CharacterInfo rogue;
public CharacterInfo wizard;
public Game()
{
knight = new CharacterInfo();
rogue = new CharacterInfo();
wizard = new CharacterInfo();
}
public static class SaveLoad
{
public static List<Game> savedGames = new List<Game>();
//it's static so we can call it from anywhere
public static void Save()
{
SaveLoad.savedGames.Add(Game.current);
BinaryFormatter bf = new BinaryFormatter();
//Application.persistentDataPath is a string, so if you wanted you can put that into debug.log if you want to know where save games are located
FileStream file = File.Create(Application.persistentDataPath + savedGames); //you can call it anything you want
bf.Serialize(file, SaveLoad.savedGames);
file.Close();
Debug.Log("Game Saved");
}
public static void Load()
{
if (File.Exists(Application.persistentDataPath + savedGames))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + savedGames, FileMode.Open);
SaveLoad.savedGames = (List<Game>)bf.Deserialize(file);
file.Close();
Debug.Log ("Game Opened");
}
}
}