It's not saving in runtime for some reason. Only when I stop the game and only when leaving the editor window then when coming back to the editor window I see it's writing the saved game file on the hard drive.
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public static class SaveSystem
{
private static readonly string SAVE_FOLDER = Application.dataPath + "/Saved Games";
private static string content;
public static void Init()
{
if (!Directory.Exists(SAVE_FOLDER))
{
Directory.CreateDirectory(SAVE_FOLDER);
}
}
public static void Save(string saveString)
{
string fileName = Path.Combine(SAVE_FOLDER, "savegame.txt");
File.WriteAllText(fileName, saveString);
}
public static void Save(string SaveString, string FolderToSave, string FileName)
{
if (!Directory.Exists(FolderToSave))
{
Directory.CreateDirectory(FolderToSave);
}
string fileName = Path.Combine(FolderToSave, FileName);
File.WriteAllText(fileName, SaveString);
}
public static string Load()
{
content = "";
string fileName = Path.Combine(SAVE_FOLDER, "savegame.txt");
if (File.Exists(fileName))
content = File.ReadAllText(fileName);
return content;
}
public static string Load(string FolderToLoadFrom, string FileName)
{
content = "";
string fileName = Path.Combine(FolderToLoadFrom, FileName);
if (File.Exists(fileName))
content = File.ReadAllText(fileName);
return content;
}
}
It should make the whole saving and writing the file in runtime.
Application.dataPath
(the directory with your game assets) is a bad idea! On a lot of target platforms, that directory won't be writable. UseApplication.persistentDataPath
instead. It gives you the directory where you are supposed to store savegames according to what's the best practice on the user's operating system. \$\endgroup\$