unity read text file and code example
Example 1: unity read from text file
using UnityEngine;
using UnityEditor;
using System.IO;
public class HandleTextFile
{
[MenuItem("Tools/Write file")]
static void WriteString()
{
string path = "Assets/Resources/test.txt";
StreamWriter writer = new StreamWriter(path, true);
writer.WriteLine("Test");
writer.Close();
AssetDatabase.ImportAsset(path);
TextAsset asset = Resources.Load("test");
Debug.Log(asset.text);
}
[MenuItem("Tools/Read file")]
static void ReadString()
{
string path = "Assets/Resources/test.txt";
StreamReader reader = new StreamReader(path);
Debug.Log(reader.ReadToEnd());
reader.Close();
}
}
Example 2: unity read text file
TextAsset textFile;
string text;
string[] lines;
textFile = Resources.Load(fileName) as TextAsset;
text = textFile.ToString();
lines = text.Split('\n');