Skip to content

Instantly share code, notes, and snippets.

@MarcelvanDuijnDev
Created January 8, 2022 14:18
Show Gist options
  • Save MarcelvanDuijnDev/1551835de8b07b9c3541673261eea478 to your computer and use it in GitHub Desktop.
Save MarcelvanDuijnDev/1551835de8b07b9c3541673261eea478 to your computer and use it in GitHub Desktop.
Unity Read/Write Text(txt) files
using UnityEngine;
using System.IO;
public class ReadWrite_TextFile : MonoBehaviour
{
[SerializeField] private string _Path = "";
[SerializeField] private string _FileName = "ExampleTextFile";
[Header("Example")]
[SerializeField] private string _Message = "Test Message";
void Start()
{
if (_Path == "")
{
_Path = "Assets/" + _FileName;
}
WriteTextFile();
ReadTextFile();
}
public void ReadTextFile()
{
StreamReader reader = new StreamReader(_Path + ".txt");
Debug.Log("Read Result: " + reader.ReadToEnd());
reader.Close();
}
public void WriteTextFile()
{
StreamWriter writer = new StreamWriter(_Path + ".txt", true);
writer.WriteLine(_Message);
writer.Close();
Debug.Log("Write Complete");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment