Home > Software design >  streamWriter does not apply changes until I restart my game
streamWriter does not apply changes until I restart my game

Time:04-29

I'm making a game in Unity with an editor level that writes the level data in a .txt

My problem is that changes to the file are not applying correctly because I only can see them when I restart the game.

When the player creates a new level i use this:

TextAsset txtFile = Resources.Load<TextAsset>("Levels");
StreamWriter streamWriter = new StreamWriter(new FileStream("Assets/Resources/"   levelsManager.filename   ".txt", FileMode.Truncate, FileAccess.Write));

And at the end of the method I close the StreamWriter.

    streamWriter.Flush();
    streamWriter.Close();
    streamWriter.Dispose();

Moreover if the user creates multiple levels, only the last one is saved.

Changing the filemode to apend does not work because after restarting the game and creating a level it also creates again the stored levels.

¿Is there a way of refreshing the document so I don't have to restart the game everytime I create a level?

Thank you in advance.

CodePudding user response:

You just need to refresh the assets in Unity using AssetDatabase.Refresh.


However

When the player creates a new level i use this

Note that none of this will work in a built application!

After building your app the Resources are read-only. Anyway note from Best practices -> Resources

Don't use it!

You probably rather would want to store the default file in StreamingAssets and use Application.streamingassetsPath and then later in a build use Application.persistentDataPath instead and fallback to streamingAssetsPath for reading only (again StreamingAssets is read-only after building)

For example like

public string ReadlevelsFile()
{
    try
    {
#if UNITY_EDITOR
        // In the editor always use "Assets/StreamingAssets"
        var filePath = Path.Combine(Application.streamingAssetsPath, "Levels.txt");
#else
        // In a built application use the persistet data
        var filePath = Path.Combine(Application.persistentDataPath, "Levels.txt");
        
        // but for reading use the streaming assets on as fallback
        if (!File.Exists(filePath))
        {
            filePath = Path.Combine(Application.streamingAssetsPath, "Levels.txt");
        }
#endif
        return File.ReadAllText(filePath);
    }
    catch (Exception e)
    {
        Debug.LogException(e);
        return "";
    }
}

public void WriteLevelsFile(string content)
{
    try
    {
#if UNITY_EDITOR
        // in the editor always use "Assets/StreamingAssets"
        var filePath = Path.Combine(Application.streamingAssetsPath, "Levels.txt");
        // mke sure to create that directory if not exists yet
        if(!Directory.Exists(Application.streamingAssetsPath))
        {
            Directory.CreateDirectory(Application.streamingAssetsPath);
        }
#else
        // in a built application always use the persistent data for writing
        var filePath = Path.Combine(Application.persistentDataPath, "Levels.txt");
#endif
        
        File.WriteAllText(filePath, content);

#if UNITY_EDITOR
        // in Unity need to refresh the data base
        UnityEditor.AssetDatabase.Refresh();
#endif
    }
    catch(Exception e)
    {
        Debug.LogException(e);
    }
}
  • Related