- I want to read and write data from a text file that can be in a specific directory or among my other Assets.
using UnityEngine;
using UnityEditor;
using System.IO;
public class HandleTextFile
{
[MenuItem("Tools/Write file")]
static void WriteString()
{
string path = "Assets/Resources/test.txt";
//Write some text to the test.txt file
StreamWriter writer = new StreamWriter(path, true);
writer.WriteLine("Test");
writer.Close();
//Re-import the file to update the reference in the editor
AssetDatabase.ImportAsset(path);
TextAsset asset = Resources.Load("test");
//Print the text from the file
Debug.Log(asset.text);
}
[MenuItem("Tools/Read file")]
static void ReadString()
{
string path = "Assets/Resources/test.txt";
//Read the text from directly from the test.txt file
StreamReader reader = new StreamReader(path);
Debug.Log(reader.ReadToEnd());
reader.Close();
}
}
using UnityEngine;
using System.IO;
public class RuntimeText: MonoBehaviour
{
public static void WriteString()
{
string path = Application.persistentDataPath + "/test.txt";
//Write some text to the test.txt file
StreamWriter writer = new StreamWriter(path, true);
writer.WriteLine("Test");
writer.Close();
StreamReader reader = new StreamReader(path);
//Print the text from the file
Debug.Log(reader.ReadToEnd());
reader.Close();
}
public static void ReadString()
{
string path = Application.persistentDataPath + "/test.txt";
//Read the text from directly from the test.txt file
StreamReader reader = new StreamReader(path);
Debug.Log(reader.ReadToEnd());
reader.Close();
}
}