Unity storing game data


















Looking for just one will be sufficient. Otherwise, it will print an error to the Unity console. Finally, if you wish to remove the save data stored in PlayerPrefs , all you need to call is PlayerPrefs. DeleteAll and the work is complete. In the following method, DeleteAll is put to use along with resetting the variables and ending by printing a message to the debug console. To try it out in-game, save your code and return to the Unity editor.

Figure 4: Attaching the SavePrefs script. Begin playing the game and tinkering with the GUI on-screen, changing variables to whatever you wish. When ready, save your game. Then try stopping and replaying the game, this time clicking the Load Your Game button. Provided everything works correctly, you should see the variables immediately change to whatever was saved to PlayerPrefs. Figure 5 shows the game in action.

Figure 5: The project in action, using PlayerPrefs. This could be things like how much in-game currency the player currently possesses or stats in a role-playing game. These are all the data types you can save to PlayerPrefs , so if you wish to save variables of other types, you may be out of luck. Open up the SaveSerial script to begin trying out the next method. Much of the code will be the same as the last script with some minor differences to prove certain points.

All remaining methods will be different. Next, in order to save data, a new class within this script will be created. This class will be made serializable and will consist of the data to be saved. The goal now is to create the remaining methods that will allow you to save, load, and reset save data. You might notice that the three variables in the SaveData class correspond with the three variables in SaveSerial. Saving data will work by passing SaveSerial's variable values into SaveData and then serializing the SaveData class itself.

The BinaryFormatter is used to perform the act of serialization and deserialization. When serializing the data, BinaryFormatter is responsible for converting the information to a stream of 1s and 0s. The BinaryFormatter serializes that data to the file defined in the FileStream.

The file is then closed, and a message is printed to the debug console saying the data was saved. Like before, the LoadGame method is very much the same but in reverse. Your save file by the name of MySaveData. Then the variables found in the save file will be fed into SaveSerial's variables. At the end, a message is printed to the debug console saying the load was performed successfully. If there is no file found at the file path, an error message will display instead. This is extremely similar to the PlayerPrefs method but with a couple of extra steps.

Assuming there is a file to delete, the variables in this script will also be reset to some default values and a message printed to the console. Your answer does explain saving data, but this is NOT secure. It can easilly be tampered with — Immorality.

Immorality nothing is really secure, it depends how far you want to go with secruity of your data, but for something like a game save, I recon even placing a salted checksum of the data as a field would stop most crackers — zambari. PlayerPrefs is inherently insecure as it is not intended for save data. It's intended to store options such as volume level, mute status, fullscreen or windowed, etc.

Things that if the player edits the plain text file while the game isn't running won't cause undesired behavior. Marco Alonso Friz Palavecino Ok, where would the json file be located on the system, and I understand nothing is completely secure but is it much better than Player Prefs?

For add security you can add md5 or sh1 hashcode to check the integrity of your file, and you can store the file in zip format for make more hard to change — Marco Alonso Friz Palavecino. Show 1 more comment. TechnoLover TechnoLover 51 5 5 bronze badges. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown.

The Overflow Blog. Podcast Helping communities build their own LTE networks. The stored data is in simple key-value pairs. PlayerPrefs are relatively easy to implement and require only a few lines of code, but they only support Float, Int and String-type values, making it challenging to serialize large, complex objects. JSON is a human-readable data format. The ability to read and change data is useful if you support modding but detrimental if you want to prevent cheating.

In addition to these concerns, since JSON is a text-based format, it's more expensive for machines to parse. That is, it's slower to read and uses more memory than binary alternatives. So, if you have lots of data, you may want to consider options that aren't text-based. Every use case is different, and it's these kinds of tradeoffs that lead developers to create many other data formats.

JSON is standardized and widely used in many different applications. As a result, all platforms support it strongly, which is helpful when building cross-platform games.

JSON was developed as a communication protocol for web browsers, making it inherently good for sending data over a network. A ScriptableObject is essentially a data container that can store data independently from the class instances. You can only use the data you set up during the development for a single game session. In order to create a scriptable object write a class that inherits from ScriptableObject.

If you add the CreateAssetMenu attribute line 3 you will be able to create your objects directly inside the editor. In addition, there is a known issue with BinaryFormatter on iOS that prevents file manipulation on native mobile device.

Luckily, there is an easy solution to this and it only requires you to add a single line of code in either Awake or Start function:. Firstly, create a serializable class that will hold all the data your game needs. Please note the [Serializable] attribute above the class declaration. Secondly, write the static Save method that will create a file, serialize the data and store it on the hard drive.

On lines I highlighted the necessary imports. Thirdly, in order to access the data we need to load it first. It will make an attempt to read from the previously saved file, deserialize the data and recreate the object from it.

In this article I have presented few methods of preserving the data between scenes and game sessions in Unity. All solutions I showed here come with pros and cons and it is up to the developer to decide which one to use. It is usually dictated by the type of data we are dealing with and whether we want to preserve it only for the duration of a single game session or permanently save it on hard drive to access it between different game sessions.

Data Persistence in Unity by Pav. Introduction While you work on your project in Unity, sooner or later you will come across a problem of data persistence. The Solutions to data persistence In this section I will focus on the main commonly used solutions for data persistence in Unity games.



0コメント

  • 1000 / 1000