Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

streamWriter does not apply changes until I restart my game

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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 Application.streamingassetsPath and then later in a build use Application.persistentDataPath instead and fallback to streamingAssetsPath for reading only.

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);
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading