0
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ The solution is when saving the file to the hard disk to use : AssetDatabase.Refresh(); \$\endgroup\$
    – Daniel Lip
    Commented Feb 8, 2021 at 4:26
  • 1
    \$\begingroup\$ Putting your savegames in Application.dataPath (the directory with your game assets) is a bad idea! On a lot of target platforms, that directory won't be writable. Use Application.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\$
    – Philipp
    Commented Mar 10, 2021 at 9:55

1 Answer 1

1
\$\begingroup\$

The solution is to refresh the Assets data base when writing the file to the hard disk :

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
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);

        AssetDatabase.Refresh();
    }

    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);

        AssetDatabase.Refresh();
    }

    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;
    }
}

This line did the trick :

AssetDatabase.Refresh();
\$\endgroup\$
1
  • \$\begingroup\$ Have you tried building the game after making this change? \$\endgroup\$
    – Philipp
    Commented Mar 10, 2021 at 9:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .