Symptoms:
- I want to read and write data from a text file that can be in a specific directory or among my other Assets.
Cause:
-
Resolution:
If you don’t want to read the file from a directory, you can assign the asset directly from the Editor using an exposed property of type TextAsset (as you can see in Figure 1) and get the text of the file using the TextAsset.text property.
Figure 1. Text file assigned to a TextAsset
Another way to read and write data from a text file that’s in a specific directory is to use the StreamWriter and StreamReader classes from the System.IO namespace to access characters from a byte stream and then load the file as a TextAsset in Unity.
The following example Editor script prints the data from a file (if it exists) or writes text to a file in the specified path.
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();
}
}
You can run the script by going to Tools > Read/Write File
If you intend to read/write files at runtime, you will need to make some changes. Here is an example that should work on all platforms:
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();
}
}
More Information:
For more information, consult the following documentation:
Comments
9 comments
This was exactly what I was looking for to create a Log System. Thanks!
Does system.io streamwritting work on all platforms, mobile & webplayer for example?
The [MenuItem("Tools/XXXX file")] part is misleading because is not needed to solve the problem and what is worse it's use (which is not straightforward) it's not explained: not even a screenshot nor anything
If you are developing for Hololens or mixed reality, you'll need to pass a stream in. This works fine for working in the editor, but when you try to build a solution under Universal Windows Platform, you'll get an error. Here's what I did:
try
{
using (StreamWriter sw = new StreamWriter(new FileStream("Assets/" + FileName + ".txt", FileMode.OpenOrCreate, FileAccess.Write)))
{
string line = "";
//logic logic logic
sw.Write(line);
}
}
catch (Exception e)
{
}
perfect! I needed TextAsset.text for Quest to read from Assets/myFile.html and used StreamWriter for Quest to write to /sdcard/Android/data/myPkgId/files, and a PC player can access the sdcard via adb
is there a way to format the code?
Very useful article thank you
Sensacional! Sou do brazil and I'm still using the translator.. I have a small company where I deploy the crm bitrix, I'm not a programmer I just deploy crm training processes and if anyone is interested in exchanging ideas about deploying unity solutions because there is a lot of demand for AI bot apps and always wanted to work in the corporate system but together with free open communities (of course, always complying with the rule and agreement)... any Brazilians? congratulations for the project and for everyone in the community.
အဖွဲ့အစည်းတစ်ခုတွင်၊ အဖွဲ့ဝင်များသည် ပိုင်ရှင်၊ မန်နေဂျာ သို့မဟုတ် သုံးစွဲသူများအပေါ်မူတည်၍ မတူညီသောခွင့်ပြုချက်များရှိသည်။ သင့်အခန်းကဏ္ဍပေါ် မူတည်၍ သင့်တွင် အဖွဲ့အစည်းသို့ ဝင်ရောက်ခွင့် အမျိုးမျိုးရှိသည်။
Please sign in to leave a comment.