Can anybody help me out with giving the steps needed for reading data from the text file in unity and how can the script be added.
-
\$\begingroup\$ youtube.com/watch?v=6c1fTHkYzTQ That is to read text in c#… which will help you :) \$\endgroup\$– SavlonOct 13, 2014 at 7:54
-
\$\begingroup\$ Is the text file an Asset (part of your Unity project) or located on the file system? \$\endgroup\$– Kelly ThomasOct 13, 2014 at 9:18
-
\$\begingroup\$ its located on the filesystem.I have placed my file in E drive and used the following code ` import System.IO; var filename="data.txt"; function Start () { var sourse=new StreamReader(Application.dataPath+"/" + filename); var fileContents=sourse.ReadToEnd(); sourse.Close(); var lines=fileContents.Split("\n"[0]); for(line in lines) { print(line); } } ` \$\endgroup\$– user1509674Oct 13, 2014 at 10:27
-
\$\begingroup\$ VTC as not game specific. IO is generic programming, this should be on Stack Overflow, not GameDev. \$\endgroup\$– GnemlockSep 25, 2016 at 22:16
-
\$\begingroup\$ The accepted answer isn't Unity specific, but the highest voted answer (which imo should have been accepted) is Unity specific. \$\endgroup\$– jhockingDec 6, 2017 at 20:10
5 Answers
C# Version.
using System.IO;
void readTextFile(string file_path)
{
StreamReader inp_stm = new StreamReader(file_path);
while(!inp_stm.EndOfStream)
{
string inp_ln = inp_stm.ReadLine( );
// Do Something with the input.
}
inp_stm.Close( );
}
EDIT: (Fixed an error on line 9; changed "stm.ReadLine();" to "inp_stm.ReadLine();")
There's a class named TextAssets which is used for text file reading. http://docs.unity3d.com/Manual/class-TextAsset.html Here you can find the supported file format.
So if you want to read the text file, the script would be like this:
class YourClassName : MonoBehaviour{
public TextAsset textFile; // drop your file here in inspector
void Start(){
string text = textFile.text; //this is the content as string
byte[] byteText = textFile.bytes; //this is the content as byte array
}
}
or you can read the text as resource like this:
TextAsset text = Resources.Load("YourFilePath") as TextAsset;
-
8\$\begingroup\$ Also worth mentioning that the
TextAsset
in question should probably be placed within theAssets/Resources
folder. This is the most correct answer as all the other answers seem to ignore the fact that this is within Unity. They are correct ways of reading a file in C# but ignore things like cross-platform deployment and paths. \$\endgroup\$– McAdenOct 26, 2014 at 6:34 -
1\$\begingroup\$ If you use TextAsset you prob drag reference to a MonoBehaviour and use the yext property. In which case it does not need to be in Respurces \$\endgroup\$ Mar 7, 2017 at 15:49
-
1\$\begingroup\$ Yes but... from Unity Manual : "The Text Asset is a very specialized use case. It is extremely useful for getting text from different text files into your game while you are building it. You can write up a simple .txt file and bring the text into your game very easily. It is not intended for text file generation at runtime. For that you will need to use traditional Input/Output programming techniques to read and write external files." \$\endgroup\$ Oct 31, 2021 at 17:39
You can do this in the same way you would in .NET
string word = File.ReadAllText(txtFilePath);
This code snippet can be used in any location you wish then.
This code is working fine for me to read the content in the text file
import System.IO;
var filename = "data.txt";
function Start () {
var source = new StreamReader(Application.dataPath + "/" + filename);
var fileContents = source.ReadToEnd();
source.Close();
var lines = fileContents.Split("\n"[0]);
for(line in lines) {
print(line);
}
}
We can use TextAsset
data type to access text file content in Unity3D.
Try the following code,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Global : MonoBehaviour
{
public TextAsset textFile;
// Start is called before the first frame update
void Start()
{
getFileContents();
}
// Update is called once per frame
void Update()
{
}
void getFileContents() {
string data = textFile.text.Split('\n');
for(line in data) {
Debug.Log(line);
}
}
}
-
\$\begingroup\$ Why is your answer needed while the most voted answer already covers the TexAsset class? \$\endgroup\$– Vaillancourt ♦May 30 at 18:47