Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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
0
Question by Keavon · Jul 10, 2011 at 09:38 PM · textfilereadtxt

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

WIll "System.IO" also work on a Mac?

If somebody could explain exactly how this works, thanks

Comment
Add comment · Show 2
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 Joshua · Jul 10, 2011 at 10:18 PM 1
Share

Unity uses mono. $$anonymous$$ono uses a port of thethe .NET framework. This is made by $$anonymous$$icrosoft, but is platform independent. This framework is gigantic and contains nearly everything that is essential to program$$anonymous$$g.

When it comes to actually do it, check out fireDudes example.

avatar image Fattie · Nov 26, 2015 at 06:41 PM 0
Share

Full easy howto http://answers.unity3d.com/answers/40481/view.html

2 Replies

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

Answer by fireDude67 · Jul 10, 2011 at 10:03 PM

 public TextAsset asset; // Assign that variable through inspector
 private string assetText;

 void Start() {
   assetText = asset.Text; // The assetText variable contains the text
                           // If you're looking to extract data from that,
                           // I would say go with XML. Otherwise, use the 
                           // System.Regex class
 }

Writing to the text file is quite a bit more difficult. However, here is another method that requires a bit of setup and isn't exactly good practice...make sure to add using UnityEngine at the top. The text file you're reading/writing from needs to go into a special directory called "Resources".

 public string FileName; // This contains the name of the file. Don't add the ".txt"
                         // Assign in inspector
 private TextAsset asset; // Gets assigned through code. Reads the file.
 private StreamWriter writer; // This is the writer that writes to the file

 void AppendString(string appendString) {
   asset = Resources.Load(FileName + ".txt") as TextAsset;
   writer = new StreamWriter("Resources/" + FileName + ".txt"); // Does this work?
   writer.WriteLine(appendString);
 }

In Update() use the AppendString() function to write text.

Yes, System.IO works on Mac

Comment
Add comment · Show 8 · 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 Keavon · Jul 10, 2011 at 11:45 PM 0
Share

Wow, this looks very complicated. Though relatively it actually isn't. 8P

Thank you for spending the time to put this togeather. I don't completly understand it, but I will ask you some questions I have later.

One thing is, is it possible to have the plain-text file not end in .txt?

Again, thank you so much!

avatar image Joshua · Jul 10, 2011 at 11:49 PM 0
Share

What you're doing here is reading from the resources folder, but can you even write to that (at run-time)?

@$$anonymous$$eavon yes. Look here:http://msdn.microsoft.com/en-us/library/system.io.file.aspx it can basically be any format you want. I use it for instance sometimes to dynamically write .js scripts.

avatar image fireDude67 · Jul 11, 2011 at 02:17 AM 0
Share

Is TextAsset gonna complain if I give it something other than a .txt file? I don't know. However, the writing part will work.

avatar image Keavon · Jul 11, 2011 at 02:21 AM 0
Share

Okay, so I'm going to try doing this now. $$anonymous$$y first question is, what is this resource folder? What is the directory I should make it in in my project?

avatar image Joshua · Jul 11, 2011 at 02:21 AM 0
Share
 TextAsset won't complain if it's an other text file (like .js)

@$$anonymous$$eavon If you don't know something, first look it up in the docs. ;) http://unity3d.com/support/documentation/ScriptReference/Resources.html

Show more comments
avatar image
1

Answer by Eric5h5 · Jul 11, 2011 at 03:11 AM

http://forum.unity3d.com/threads/5084-Can-I-read-and-write-text-files-using-Javascript

Comment
Add comment · 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

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Help me about reading .TXT File 4 Answers

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

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

Confused about intermediate string reading. 1 Answer

Write and read text files with c# 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