Created
January 8, 2022 14:18
-
-
Save MarcelvanDuijnDev/1551835de8b07b9c3541673261eea478 to your computer and use it in GitHub Desktop.
Unity Read/Write Text(txt) files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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