- Home /
Text file in resource folder only gets updated when i quit.
I have a text file in the resource folder that holds some values that variables in my game use (ex: health, mana). I want the user to be able to write to that file from one scene, and load them in another. Its kind of working. Except when I press save the file gets changed but i need to quit unity or stop the play mode in order for these changes to commit. So if i change mana for example from 100 to 200, id have to exit and when i re-enter play mode its 200. I need that change to happen so that the next scene would read these variables. Also if im in play mode, and i change a value, when i open the text file in the editor it asks me to reload, and when i do the changes actually commit and im still in play mode, but i need that to happen in game alone. Would appreciate some help.
Answer by AlucardJay · Mar 21, 2018 at 04:50 AM
if im in play mode, and i change a value, when i open the text file in the editor it asks me to reload, and when i do the changes actually commit and im still in play mode
So I assume you are closing the file writer ok if the text file is being updated. You can refresh the project window view with :
UnityEditor.AssetDatabase.Refresh();
https://docs.unity3d.com/ScriptReference/AssetDatabase.Refresh.html
You can also just refresh the text file with https://docs.unity3d.com/ScriptReference/AssetDatabase.ImportAsset.html
This is just for the editor (as you want to see the text file change in the editor), you may have to encapsulate the command in a platform dependant directive to avoid build complications, eg:
#if UNITY_EDITOR
UnityEditor.AssetDatabase.Refresh();
#endif
https://docs.unity3d.com/Manual/PlatformDependentCompilation.html
Yes that works but only in the editor, i want it to work in the build. AssetDatabase.Refresh(); only works in the editor. Im looking for a build solution.
You want it to work in the build? Well that's not possible. Assets are generally readonly in a build. There actually isn't a resources "folder" when you create a build. How do you currently "save" / change the file? At runtime you want to either store a file in the persistent data path or use other ways to store your data. One way is using PlayerPrefs. PlayerPref(erence)s are specifically meant to store preferences of your game.
PlayerPrefs where exactly what i needed, thanks! it worked
Aah, I see now. I didn't factor in the Resources folder, was only focused on seeing the changes reflected in the editor. It is exactly as Bunny83 stated, here are the scripting reference links :
https://docs.unity3d.com/ScriptReference/Application-persistentDataPath.html
Your answer
Follow this Question
Related Questions
Loading an asset into the game post-build 0 Answers
Problem with path in FileStream 2 Answers
Unautherorized Access Excepetion on iOS8 1 Answer
Can not load csv file 0 Answers
File Browser that works on ipad + samsung + desktop 0 Answers