How Do I Read And Write Data From A Text File? (Unity)

How Do I Read And Write Data From A Text File? (Unity)


  • I want to read and write data from a text file that can be in a specific directory or among my other Assets.


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 the 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);

        //Re-import the file to update the reference in the editor
        TextAsset asset = Resources.Load("test");

        //Print the text from the file

    [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); 



Was this article helpful?

Related Articles