I have an existing json file, and when the game starts, if there is no file named blabla, I save it to the Persistent Data path. and it saves nicely (I think I need to make a mechanism to check it this way every time). Anyway, my real question is this. The player did some things and I will save them in the json file, i.e. update the file (I plan to do this right before the player exits the game). But there are some things I don’t know. If I change the data I get from the persistent data path and serialize it again and save it to the persistent data path again, will it try to replace all the data from start to finish? If so, I guess I’ll have to update it in a different way, because wouldn’t it be difficult to process all the data this way? I’m new here and I can’t quite understand some things. Can you help me? (My English may be bad, sorry :))
Yes it will write all the data which is what you want it to do. It is fast and accurate and the alternative would tend to be worse. If 15 settings changed out of say 20 (for instance) would you prefer to check which items changed and then write them individually? Nope.
Yes, and you would know this by actually beginning to work on it.
You will discover that most JSON use involves a full object → string conversion.
This obviously implies that nothing about the original string is involved.
Therefore, yes, everything must be replaced each time you serialize.
Start with that. When you have an ACTUAL problem rather than a fantasy problem, then begin to optimize.
For all performance and optimization issues, ALWAYS start by using the Profiler window:
Window → Analysis → Profiler
Generally optimization is:
- avoid doing the thing that is slow
- do it fewer times and store its result
- do the slow thing less frequently over time
- do the slow thing when nobody cares much (eg, during level loading)
- find a faster way to do the thing (hardest)
DO NOT OPTIMIZE “JUST BECAUSE…” If you don’t have a problem, DO NOT OPTIMIZE!
If you DO have a problem, there is only ONE way to find out: measuring with the profiler.
Failure to use the profiler first means you’re just guessing, making a mess of your code for no good reason.
Not only that but performance on platform A will likely be completely different than platform B. Test on the platform(s) that you care about, and test to the extent that it is worth your effort, and no more.
https://discussions.unity.com/t/841163/2
Remember that you are gathering information at this stage. You cannot FIX until you FIND.
Remember that optimized code is ALWAYS harder to work with and more brittle, making subsequent feature development difficult or impossible, or incurring massive technical debt on future development.
Don’t forget about the Frame Debugger window either, available right near the Profiler in the menu system.
Notes on optimizing UnityEngine.UI setups:
https://discussions.unity.com/t/846847/2
At a minimum you want to clearly understand what performance issues you are having:
- running too slowly?
- loading too slowly?
- using too much runtime memory?
- final bundle too large?
- too much network traffic?
- something else?
If you are unable to engage the profiler, then your next solution is gross guessing changes, such as “reimport all textures as 32x32 tiny textures” or “replace some complex 3D objects with cubes/capsules” to try and figure out what is bogging you down.
Each experiment you do may give you intel about what is causing the performance issue that you identified. More importantly let you eliminate candidates for optimization. For instance if you swap out your biggest textures with 32x32 stamps and you STILL have a problem, you may be able to eliminate textures as an issue and move onto something else.
This sort of speculative optimization assumes you’re properly using source control so it takes one click to revert to the way your project was before if there is no improvement, while carefully making notes about what you have tried and more importantly what results it has had.
“Software does not run in a magic fairy aether powered by the fevered dreams of CS PhDs.” - Mike Acton
Apart from the fact that just serializing your data again and overwriting the old data is the most logical thing to do, you actually can not “insert” data in a text file, at all, not a single character as it requires you to rewrite EVERY single character that follows. That’s why in most cases it just makes no sense to even try to “patch” individual characters in a text file. Things like that might make sense in certain binary formats that reserve buffer for each entity, like databases do in order to speed up certain operations.
So when you have a text file with this content:
Hello World!
This is the second line.
Third line.
Forth line.
The End!
and you just add a single space in the first line, EVERYTHING would be shifted by one byte.
Here’s a hexeditor view of that file before:
And another one after adding a single space in the first line:
Though the content of the file just looks like this:
Hello World!
This is the second line.
Third line.
Forth line.
The End!
You “think” you just edited the “first line” in that file and everything else stayed the same. It didn’t. There are no “lines” in files. There are line break characters in files which indicate that a new line should start. On windows a new line is indicated with a carriage return (/r
or 0x0D
) followed by a line feed (\n
or 0x0A
). As you can see in the hex editor, there are several pairs of 0D 0A
in the file. Those are the ends of the lines. when adding a single character at the very first character in the file, literally every other character has to move over one byte.
So there’s not really a way to write out or replace a partial json file.
ps: I just noticed I misspellt “fourth”. Since I made two screenshots I’m not going to fix it
Thank you all for your answers and lastly, is there any resource you can recommend as I am in the new learning phase?
(youtube, book, website etc. everything)
No problem, the translation translated it as fourth for me.:)
It’s best to search for concrete problem statements. If you want introductory material, be sure to read whatever the Unity manual has to offer on the subject and also Unity Learn.
Most important is how to analyze any issues, specifically how to use the debugger to understand what your code is doing and where to find the log files.
Thank youu