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.
This is script is how I’m saving in the game with the K key for saving and L key for loading :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SaveTest : MonoBehaviour
{
public SaveLoad saveLoad;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(Input.GetKeyDown(KeyCode.K))
{
StartCoroutine(saveLoad.SaveWithTime());
}
if(Input.GetKeyDown(KeyCode.L))
{
saveLoad.Load(null, null);
}
}
}
Then the SaveLoad script at the bottom :
public IEnumerator SaveWithTime()
{
yield return new WaitForSeconds(timeToStartSaving);
Save(null,null);
StartCoroutine(fadeInOutSaveGame.OverAllTime(savingFadeInOutTime));
}
public IEnumerator SaveWithTime(string FolderToSave, string FileName)
{
yield return new WaitForSeconds(timeToStartSaving);
Save(FolderToSave, FileName);
StartCoroutine(fadeInOutSaveGame.OverAllTime(savingFadeInOutTime));
}
The fading script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FadeInOutSaveGameText : MonoBehaviour
{
public Canvas canvas;
public float fadingSpeed;
private bool stopFading = false;
private const float THRESHOLD = 0.01F;
// Start is called before the first frame update
void Start()
{
}
IEnumerator CanvasAlphaChangeOverTime(Canvas canvas, float duration)
{
float alphaColor = canvas.GetComponent<CanvasGroup>().alpha;
while (true)
{
alphaColor = (Mathf.Sin(Time.time * duration) + 1.0f) / 2.0f;
canvas.GetComponent<CanvasGroup>().alpha = alphaColor;
// only break, if current alpha value is close to 0 or 1
if (stopFading && Mathf.Abs(alphaColor) <= THRESHOLD)//if (stopFading && (Mathf.Abs(alphaColor) <= THRESHOLD || Mathf.Abs(alphaColor - 1) <= THRESHOLD))
{
break;
}
yield return null;
}
}
public IEnumerator OverAllTime(float time)
{
stopFading = false;
StartCoroutine(CanvasAlphaChangeOverTime(canvas, fadingSpeed));
yield return new WaitForSeconds(time);
stopFading = true;
}
}
And last the saving to the hard disk :
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’s a bit long but all the scripts are connected and I didn’t added the whole SaveLoad script only it’s bottom.
I don’t understand why it’s writing the saved game file to the hard disk only when leaving the editor window and coming back and when the game is not running already.
It should make the whole saving and writing the file in runtime.
I added the completed SaveLoad script to pastebin.com if it’s needed :
https://pastebin.com/3wraQhCU