- Home /
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.
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.
'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,
$$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.
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
Follow this Question
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