Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 staticVoidMan · Oct 01, 2014 at 06:01 AM · jsonresourcestextasset

How to write a TextAsset to disk

I have implemented a JSON structure that stores game related data.
This JSON file is stored in the Resources folder and I read it via TextAsset in this manner:

 TextAsset jsonText = (TextAsset)Resources.Load(jsonFileName, typeof(TextAsset));

Now, during the lifetime of the game, this JSON structure may need to be modified and in that case would need to be stored on disk to ensure game play progress is not hampered.

On a device, how would I get the proper path of this TextAsset without using UnityEditor.AssetDatabase.GetAssetPath(jsonText) and then write this TextAsset back to disk?

PS: This needs to work on a device.

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 robertbu · Oct 01, 2014 at 06:09 AM

You cannot modify a TextAsset in a build. The file structure does not exist. You'll have to figure out some other method of saving the file. If your build platform supports it, you can use Application.persistentDataPath. I've never written anything to it, but you might have write access to the streaming assets folder. If so, that might be a nice place to put the file since you would not need to special case things. I don't know how big your file is, but PlayerPrefs can store reasonably large files as strings. The size varies by platform, but it is 1mb on Windows.

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 staticVoidMan · Oct 01, 2014 at 06:26 AM 0
Share

'file structure does not exist' hm... that makes sense. Anyways, I don't think PlayerPrefs would be a neat solution for me because I have a structure of 10 levels with each level having 10 missions.
Levels and $$anonymous$$issions are locked and each mission has a rating of upto 3 in order to unlock further levels.
You know... all that common jazz seen in every other game.

The structure is:

 [
   {
     "levelLocked": false,
     "levelRatingGathered": 0,
     "requiredRatingToUnlock": 0,
     "mission": [
       {
         "missionLocked": false,
         "missionRating": 0
       },
       ... (x numberOf$$anonymous$$issionsPerLevel)
     ]
   },
   ... (x numberOfLevels)
 ]

So, what would one do in this case? A rough idea will also do.

In the meantime, I'll look into the strea$$anonymous$$g assets folder and storing files as string in PlayerPrefs.

Thanks,

avatar image robertbu · Oct 01, 2014 at 07:16 AM 0
Share

$$anonymous$$y thought for PlayerPrefs was to have the entire file as a single string in the PlayerPrefs. You would use your JSON to organize the data in the single file/string.

The Strea$$anonymous$$gAssets is nice because it exists both in the editor and at runtime as a file in a file system. Assu$$anonymous$$g you have write privileges on your target device(s) (something I've never tested), it would provide an alternate to putting the file in Resources, though you will have to use .NET I/O methods to pull the data in.

Note if you are targeting a Web build, PlayerPrefs may be your only choice.

avatar image staticVoidMan · Oct 01, 2014 at 07:22 AM 0
Share

Oh. use PlayerPrefs.SetString and feed it the entire JSON string. Quick and easy. I think I'll just do that for now until I find a better way.

Thanks Robert,

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Read unicode characters from TextAsset? 0 Answers

Any Way to Find a Resources folder file in a build. 0 Answers

How to load a textAsset from a txt in the resources folder 1 Answer

Why does unity expect a file when I try to put text on a mesh? 1 Answer

Where do JSON files go? are they read after build/installation (apk)? 0 Answers


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