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 polodealer · Sep 17, 2017 at 10:53 AM · filestextasset

How can you write over the contents of a text file when using a StreamWriter that takes a MemoryStream as a parameter?

I want to load a text file that is in my Resources folder, then use a StreamWriter to write to the text file.

I load the text file with :

TextAsset textFile = Resources.Load(path);

Then do the StreamWriter by converting the TextAsset to a MemoryStream :

using(StreamWriter writer = new StreamWriter(new MemoryStream(textFile.bytes))) { ... }

The code executes, but the contents of the text file are not wrote over like I want it to be. If I use a StreamWriter and just pass in the path to the text file, with a second parameter of false, this works as I intend it to and writes over the contents of the text file. I however want to load the text file from the Resources folder because using StreamWriter with a path doesn't work when I build on Android.

Is there a way to use a StreamWriter like this where you pass in a MemoryStream but can still write over the contents of the file?

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

1 Reply

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

Answer by Bunny83 · Sep 17, 2017 at 12:57 PM

Assets can't be changed at runtime. TextAssets are like any other assets in a build project. They are readonly. There is no way to permanentally alter an asset at runtime as all assets are packed into the asset database.

If you want to be able to store data in a built game you have to use an actual file path.

The StreamWriter does wotk with a path on Android as long as you pass a valid path. Android is very strict with permissions. You can only write to certain locations. If you want to save anything on android you should use the persistent data path. There you can store files and even create subfolders.

You can not store anything at Application.dataPath as this just points to the APK file. The actual apk file is located in a protected folder of the OS.

If you want to ship a text file with your app which you want to edit during runtime you usually try to load the file from the persistent data path first and if it fails load it from the TextAsset. When you want to save the file you create or overwrite the file in the persistent data path.

Besides that your code can never overwrite the file where the "bytes" come from. When you read the bytes array you just get back an array with all the content of the file. At this point the array has no connection to the file or asset. A MemoryStream just works on the array you give it as buffer.

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 polodealer · Sep 17, 2017 at 05:22 PM 0
Share

Thank you for your explanation. I now understand better how the file system works with Android and how StreamWriter and $$anonymous$$emoryStream works.

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

69 People are following this question.

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

Related Questions

Custom soundtracks? 2 Answers

iOS and Android reading/writing files 1 Answer

How to reference a file outside a project ? 1 Answer

Unity not importing blender animations at all 7 Answers

Trouble with writing text to a .ini 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