Hi all, i have a question regarding the game upgrade process.
I’ve been searching for a couple of days now and i cant find any info regarding the updating and upgrading the game.
I understand the upgrade process (upload new version etc.) however i don’t now what will happen to PlayerPrefs during that process and what i need to do to keep the PlayerPrefs saved in the game.
Also i would like to have content that will be downloaded from my server ( in my case new slots game).
Asset bundle can contain only images. I need to upload new game scripts as well.
Any help, pointers to reading material or even script examples will be very much appreciated.
Regards Igor
PlayerPrefs are fragile, easily destroyed- clearing the game’s cache on Android or iOS will do it, as will numerous other things. If you’re saving data in PlayerPrefs that you absolutely have to keep, then you’re putting far too much faith in PlayerPrefs. It’s really good for things like the currently selected resolution, volume levels, text sizes, etc- in other words, preference data, which can just be reset without any huge loss. You should definitely serialize/save and deserialize/load your save data yourself.
AssetBundles can hold assets- not just images, but meshes, sprites, ScriptableObjects, audio files, video files, etc… Scripts are not really a possibility there, but by properly utilizing bundles for the majority of the assets in your game, replacing the “core”, the executable that contains your scripts and game logic, should be a pretty tiny operation.
As for examples and scripts, just check the Unity Learn site. Here are the first three Unity hits with regards to how to use AssetBundles.
Correct, and if you clear app data, pretty much anything you can (easily) write to device will also be gone.
If you do save something valuable in PlayerPrefs, be sure to call PlayerPrefs.Save() immediately afterwards, or else it won’t actually be committed to local storage until the app shuts down gracefully. This means if the app crashes or gets sigkilled, your PlayerPrefs may not make it to permanent storage and then you may become sad.
Ahh, that wasn’t my understanding, but if I was wrong, I apologize. I still say PlayerPrefs are too fragile and inefficient though- I’ve wiped them out dozens of times on accident, and if clearing the cache wasn’t the culprit, then some device-cleaning app was, on PC and on Android. My manually-saved files survive just fine. shrugs
It also promotes various scripts handling their own save and load functions independently, which is only really a boon when prototyping, or in tiny applications where a central management system is overkill and you’ll never need to support multiple users/accounts/save files on a single device. In projects more complex than that, not having complete control of your own save data isn’t really an option IMO.
So, I still don’t recommend putting too much faith in PlayerPrefs- and as the name suggests, using it just for preference data that’s easily replaced when lost makes far more sense to me.
Thank you guys, I found some documents regarding saving using JSON or BinaryFormatter for security. Both methods look OK. Any advise?
I also found that i can basically copy Scrips from web server not as Assets just conversion stream. I can create script that will check and download new scripts online when game starts.
Is it possible to access the new Scripts if they are included in new Game scenes asset bundle if user click download them. I’m not sure if they will be found and loaded by the Scenes automatically.
afaik, adding scripts in the bundle is impossible.
As for Json/BinaryFormatter, I think it depends on if you need readability or not.
I’m also not 100% sure about a changing class (one where you remove a field, for instance) with BinaryFormatter. I’ve read that this will cause errors, but when I tested it (outside of Unity, mind you, but C#), I did not get an error. I do not know if that was a fluke, but if someone knows that could be helpful.
BinaryFormatter will handle the removal, and even addition of new fields just fine.
When adding fields, it just won’t have data to stick in them.
Renaming won’t work for obvious reasons… I mean, it will still deserialize, but the data won’t show up there, because the serializer doesn’t know it was renamed. So it effectively looks like a field was removed and another was added.
Of course you can use many of the attributes and interfaces available to remedy this stuff. Like the ISerializable interface that lets you manually control how the data gets written to and read from the serialized object.
Honestly… I find the .net serialization engine to be heavily extensible, if only convoluted at times.
It can be added to a project with very little overhead/bloat (it’s just the formatter class, reader, writer, an enum, and an exception… I didn’t implement Binder or Surrogate support, but will in due time).
The manner in which you serialize is completely up to you.
BinaryFormatter saves your data in 1s and 0s and comes with .NET, and while it’s pretty slow and filesizes aren’t small, it’s dead-simple and you need no extra assemblies or anything. This is the “standard method” you’ll often see in guides when the serialization is not the focus, I’ve noticed.
JsonUtility comes built into Unity and serializes data into strings, it’s a bit faster and smaller filesizes than BinaryFormatter, and if you don’t encrypt it it can be nearly human-readable if you open the file in a text editor or just print the string to the console. That can be helpful for some debugging. It’s also fantastic for sending data to and from servers, since the strings can be included in GET requests easily.
XmlSerializer also comes in .NET and is extremely human-readable- it’s pretty standard but from what I recall including it in small projects can bloat your build sizes a bit. I’d only use it for debugging (swapping out your normal serialization system for XML just to be able to read the files created easily).
Some other options are ZeroFormatter and MsgPack (and many others), with different pros and cons to each, but you’ll need to find their git repositories and copy them over to your project to use them.
As for compiling scripts from servers and external files after building, it’s technically possible with great great effort, but not at all trivial, completely unsafe, and never worth the effort. It’s also incredibly painful to actually use the scripts in Unity. If you want to load assets externally (text, images, videos, ScriptableObjects, etc), AssetBundles help a lot with that. text and images can technically be loaded from anywhere (I myself use Google Drive for directly sending and receiving spreadsheet data for my localization databases, among other things).
I usually just recommend those 2 because they’re “easy” to talk about, especially when someone is not that familiar and chances are might not be off to find something else
That is a wonderful confirmation, I appreciate that. That is what I saw in my own testing, and added to your response, will squash the other memories of a post (or more) that I saw with conflicting information.
I see talk now and then about speed, and a lot of differing results. Obviously some of it is old information ,and/or older .Net maybe. I remember reading a post recently about a change to BinaryFormatter that increased its performance and speed greatly. (slightly off topic)