Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
    
youtube.com/watch?v=6c1fTHkYzTQ That is to read text in c#… which will help you :) –  Savlon Oct 13 '14 at 7:54
    
Is the text file an Asset (part of your Unity project) or located on the file system? –  Kelly Thomas Oct 13 '14 at 9:18
    
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); } } ` –  user1509674 Oct 13 '14 at 10:27

4 Answers 4

up vote 1 down vote accepted

C# Version.

using System.IO;

void readTextFile(string file_path)
{
   StreamReader inp_stm = new StreamReader(file_path);

   while(!inp_stm.EndOfStream)
   {
       string inp_ln = stm.ReadLine( );
       // Do Something with the input. 
   }

   inp_stm.Close( );  
}
share|improve this answer

There's class named TextAssets which is used for text file read. http://docs.unity3d.com/Manual/class-TextAsset.html here you can find the supported file format.

so if u want to read read the text file, 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;
share|improve this answer
    
Also worth mentioning that the TextAsset in question should probably be placed within the Assets/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. –  McAden Oct 26 '14 at 6:34

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.

share|improve this answer

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 sourse=new StreamReader(Application.dataPath+"/" + filename);
    var fileContents=sourse.ReadToEnd();
    sourse.Close();
    var lines=fileContents.Split("\n"[0]);
    for(line in lines) {
        print(line);
    }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.