How To Save / Load with PlayerPrefs in Unity3D | Tutorial | Guide

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to a new tutorial today we're going to talk about one of the many ways to save and load your game in unity player prefs this video consists of four parts first i'm going to show you the example we will be working with then we are going to look at the player prefs themselves and how to use them third will be the actual implementation and functions you need to save and load your game using player press after that we'll finish up with a recap and some pros and cons for player proofs you can find timestamps in the description if you feel like you want to jump ahead to a specific part also you can just download the whole example using the github link in the description and jump right into it if you have any questions or there's something missing in the video feel free to post in the comments down below or just join our discord server and we'll have a chat there let's get started the example implementation i'm going to show you is the simplest thing i could think of to just focus on the player preps themselves we will just create an empty 3d project and all we need to do is add a capsule and attach several scripts to it we're going to use the capsule collider that is already attached to the capsule itself to register clicks using the on mouse down event the scripts we are going to attach you can see down here this is going to be the player prefs example as you can see there are other scripts down here you find one video for each of those to save data if they've already been released you get a link to them shown in the top right corner now and also a link in the description if not i'd suggest subscribing so that you get notified as soon as they are released in the script itself we will be using the on mouse down to register clicks on our capsule and all we're going to do here is to count how often we click it by adding a field of type integer up here that is holding our data this one will be used to save and load using the player press player prefs are a built-in mechanic to save data in unity they are part of the unity engine library and what you can do with them is saving strings floats and integers but that's about it the data stored using player prefaces then save in a file or registry depending on your operating system in the documentation you can find a list of that in my case i'm on windows so in this example we will be saving the player prefs using the windows registry under software example company example product name and it's key and those two example company and product name will just be whatever you set up as a company and product name in unity's player settings the key is manually defined by us we'll look into that in the example itself there's an important difference if you look at the standalone player storage location which is when you actually build and run the game as a standalone application if you're using the editor itself while still in development those paths are slightly different but they're also stated in the documentation the static methods you will be using when using player prefs are quite limited the most important ones get float indent string as well as set float indent string down here will be the ones we will be using to load and save floats in set strings now let's look at the way to actually save and load the data and go back to our script we've already seen how to register the clicks and where they are saved let's now look into the actual code to achieve this the first thing we want to do is call the corresponding function on player prefs since we are using an integer to count our clicks we have to call setint we then use a key in that case i've specified that key to be hit count key it can be any kind of string but it has to be of type string and then we just pass in the hit count itself and that's what's going to be saved after we did call the set in function we also have to call safe to actually make sure this data gets persisted otherwise it's not going to be written to the file directly so in case your game crashes at some point nothing is going to be safe keep in mind that you don't need to call safe if the game is quit normally because it's automatically called in the on application exit message which is the last message that's going to be sent by unity the reason i don't just put the string in here but instead save it in a private read-only field appears as you can see because we need it in several places and it's always a good idea to save a string in one location and then reusing this field instead of writing it in here every single time which could potentially lead to typos and therefore a program that doesn't work the part where we load the data works exactly the same as the setting instead of set in we use get in and all we need to pass in is the key here we don't need the hit count itself because that's what we're reading but the integer we get back will just be saved in hit count which is the field we used in this mono behavior notice there's one thing i do surround the loading and that is playerprefs.has key playerpress.haskey returns true in case the key actually exists it returns false if it doesn't it's good practice to do that to make sure that you don't load any data that has never been written before because in that case you only get back the default value in this example this might not be a problem because it would return the default value for integer which is zero our hit count would just be zero and that would be fine in addition to checking if a key exists and reading this key saving this key you can also delete the key in case there is data you no longer need or you actually want to reset let's say you used some settings and the player wants to reset those settings it's probably a good idea to just delete the key because the player has reset that let's look how this actually works in our game the first thing i'm going to do is to look at the registry and as you've seen before in current user software unity unity editor company name and product name you'll see the save data we don't start with anything because we haven't started our game yet but as soon as we do that and over here monitor the hit count you can see it's going up and when i go back to the registry just have to click somewhere else and back in here so that it actually refreshes you can see there's some data saved that unity needs for other reasons but the only one we're interested in is the one that has our pecan key included you can already see it is set to 10 here but you can also go to modify decimal and now you can see the data is saved in plain text right here and that is the problem i mentioned earlier so if i change this one to let's say 15 save it first of all you can see the 15 is saved here i go back to my game and now the 15 is loaded in here so this is important to keep in mind here because the data could be easily modified by a user all right now that we've seen how to save and load data using set and get and also check for keys or delete them let's look at some of the pros and cons of player press first of all they are included in unity engine standard library so you don't need anything extra other than that you can just use them right away they're easy to understand it's a simple pair of key and value and they're easy to implement it's just a couple lines of code the cons on the other side though you can only save simple data in float string and that's it there's nothing else you can save you can of course use those strings to save any kind of more complex data and read it later decode it but in itself user preference is only offered in floats and string the other major issue is that the data is saved as plain text in this example here since we are in windows we save to the registry so it means the data can be read by any user and also edit it the good rule of thumb therefore is if the user is allowed to change the data you are saving in the game anyway it's fine to use player prefs if not maybe think about other options and some of them can be found in my other videos alright that's it for this tutorial i hope you learned how to use player preps to save and load your game in unity if you have any questions or you missed something in the video feel free to post them down below in the comments or just join our discord server and we'll have a chat there see you there
Info
Channel: dodoTV42
Views: 295
Rating: undefined out of 5
Keywords: realm, unity, persistence, save, load, gamedev, unity3d, tutorial, unity3d tutorial, unity3d game tutorial, realm sdk, realm .net sdk, unity tutorial, unity game development, unity game tutorial, unity game dev, guide, how to, brackeys, playerprefs, playerprefs unity, unity brackeys, playerprefs brackeys, playerprefs unity tutorial, dotnet, c sharp, c sharp for beginners, c sharp for unity
Id: gM73B5PyGh8
Channel Id: undefined
Length: 7min 59sec (479 seconds)
Published: Mon May 24 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.