• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
6
Question by Sun · Mar 05, 2010 at 08:46 AM · filereadtxt

Help me about reading .TXT File

Hi there i have one TXT hile and i wanna use it as one word with one number. Text file...

 Character / 0934
 US / 0424
 Seoul / 0426
 etc...

and i wanna read one line such as "seoul, 0426"

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fattie · Nov 26, 2015 at 06:08 PM 0
Share

FTR the easiest way by far to do this is just use Unity's http://docs.unity3d.com/$$anonymous$$anual/class-TextAsset.html text asset class, as FIreDude explains.

4 Replies

· Add your reply
  • Sort: 
avatar image
22
Best Answer

Answer by Eric5h5 · Mar 05, 2010 at 01:28 PM

Use the System.IO classes, such as StreamReader:

 import System.IO;
 var fileName = "foo.txt";
 
 function Start () {
     var sr = new StreamReader(Application.dataPath + "/" + fileName);
     var fileContents = sr.ReadToEnd();
     sr.Close();
 
     var lines = fileContents.Split("\n"[0]);
     for (line in lines) {
         print (line);
     }
 }

This reads in the contents of the named text file and splits into separate strings on the newline char.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eric5h5 · Mar 08, 2010 at 05:35 AM 0
Share

The "var lines = fileContents.Split("\n"[0]);" line results in a string array, so "lines[134]" would be the 135th line. You can use Split again to split the line into separate elements.

avatar image Eric5h5 · Mar 08, 2010 at 03:50 PM 0
Share

"lines" is a string array...you can't Split a string array, only individual strings. So you'd use "var lineElements = lines[x].Split("/"[0]);", where x is the line number you want to split.

avatar image bernardfrancois · Mar 09, 2011 at 01:18 PM 0
Share

For iPhone and Android, you need to use the correct file path.

I just made a blog post explaining how to obtain a valid file path in a platform-independent way (working on both PC, $$anonymous$$ac, iOS and Android):

http://www.previewlabs.com/file-io-in-unity3d/

avatar image SmartyP · Feb 05, 2015 at 08:55 PM 0
Share

Normally in .Net you should use System.IO's Path.Combine() to combine folder and filenames versus directly working with '\' and '/' symbols.

ex: string fullPath = Path.Combine(Application.dataPath, filename);

avatar image
16

Answer by fireDude67 · Dec 30, 2010 at 05:16 AM

You can use the Unity Text Asset class, and then use a regex to split it.

Simply do this...

 public TextAsset dictionaryTextFile;

and then literally drop your text file in to the project. It would be called something.txt. In the example it is called yourTextFIle.txt...

alt text

To convert a TextAsset to one long string, is completely trivial.

You just go .text. That's all there is to it.

That's exactly the reason we all use Unity, rather than actually bothering programming.

You then just convert that one long string to a List<> of strings.

So, in your code, simply do this:

 public TextAsset dictionaryTextFile;
 private string theWholeFileAsOneLongString;
 private List<string> eachLine;
 
 void Start()
     {
     theWholeFileAsOneLongString = dictionaryTextFile.text;
     
     eachLine = new List<string>();
     eachLine.AddRange(
                 theWholeFileAsOneLongString.Split("\n"[0]) );
     
     // you're done.
     
     Debug.Log(eachLine[4]);
     Debug.Log(eachLine[10]);
     Debug.Log(eachLine[101]);
     Debug.Log(eachLine[0]);
     int kWords = eachLine.Count;
     Debug.Log(eachLine[kWords-1]);
     }

AddRange and .Split("\n"[0]) are just methods to easily convert a long string which is separated by newlines, to, a List<> of words.

Fortunately it's that easy.


a.png (47.5 kB)
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Alex1002 · May 23, 2016 at 02:57 PM 0
Share

How to write file?

avatar image CrowaL · May 30, 2018 at 04:58 AM 0
Share

@fireDude67 Thank you good sir! After 8 years, this still works great!

avatar image
0

Answer by StephanK · Mar 05, 2010 at 01:09 PM

You can use the standard c# file api.
If you want to run it in the webplayer or you are using javascript you could use the WWW class to do this.

var pathToFile = "path/to/example.txt"; var url = "file://" + pathToFile; yiel download = new WWW(url);

text = download.data;

pathToFile needs to be a complete path. You could also use a path relative to your unity project. Then you'd have to do this to run in the editor:

var url = "file://" + Application.dataPath + "/../" + pathToFile;
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eric5h5 · Mar 05, 2010 at 01:32 PM 1
Share

There is no "C# file api". There is only the .NET file API, which works perfectly fine in Javascript or Boo. Also, "file://" doesn't work in the webplayer.

avatar image StephanK · Mar 05, 2010 at 09:55 PM 0
Share

I meant .NET, sorry for being vague. As I don't use javascript or Visual Basic C# and .NET are pretty much the same to me. However you are right, C# is not .NET. "file://" won't work in the web player use "http://" ins$$anonymous$$d. Would StreamReader and Application.dataPath work in a WebPlayer context?

avatar image Eric5h5 · Mar 08, 2010 at 05:37 AM 1
Share

StreamReader doesn't work in the web player, and dataPath returns the absolute URL of the unity file in that case.

avatar image
0

Answer by DeveloperJake · Dec 01, 2021 at 05:18 PM

How exactly would we open the file?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Pangamini · Dec 01, 2021 at 05:33 PM 0
Share

I'd say that this is an entirely .net / C# question

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How do I read read from and write to a text file? 2 Answers

How to read text file to array for my generateLetter script? 0 Answers

Why is reading some text files with JS so darn slow?! (title changed to reflect answers) 1 Answer

Dat File Location 1 Answer

How to read particular line from *.txt file? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges