This can be useful for loading settings where using an automatic default might be a bad thing, such as setting the volume to 0, simply because no player preferences exist.Īlternatively, it’s possible to check that a key exists before trying to load it, using the Has Key method. Like this: float volume = PlayerPrefs.GetFloat("masterVolume", 0.75f) However, it’s also possible to specify a custom default value that will be used if the key doesn’t exist.įor example, when getting a float value for a volume setting, if the preference doesn’t exist, instead of using 0, I could use a custom default value of 0.75 instead. When loading a preference value, if the key that you enter doesn’t exist, the default value for the data type will be returned instead, which will either be 0 for float and integer values or blank for a string. However, if you’re using Player Prefs to only store options settings, you might use Delete All to reset everything, returning all custom settings back to their default values. This will clear a specific key from Player Prefs file.Īlternatively, to delete all created keys, use the Delete All function.ĭelete All clears everything, so it should be used with caution, especially if you’re saving any kind of progress data using Player Prefs. Like this: PlayerPrefs.DeleteKey("myString") To delete a key that you’ve created, simply call the Delete Key function of the Player Prefs class, passing in the key you want to remove. This means that, if you want to remove a preference key, you’ll need to manually delete it using a script, even when working in the editor. The Player Prefs values are stored locally on the device in a preferences file, meaning that they will exist after the game is closed. String loadedString = PlayerPrefs.GetString("myString") PlayerPrefs.SetString("myString", "Hello!") Like this: public void SetString(string value) PlayerPrefs.SetFloat("myFloat", 0.1234f) įloat loadedFloat = PlayerPrefs.GetFloat("myFloat") The Set Int function allows you to save and load an integer value however, similar methods are also available to store other data types, such as a float. Int loadedNumber = PlayerPrefs.GetInt(myNumber) To load the preference again, all you need to do is call Get Int, passing in the same key. This will create a new stored preference under that name or, if the key already exists, it will overwrite it, using the new value. It works by storing a string, float or integer value to disk with a key, which is a text string that’s used to store and recall a preference.įor example, to save an integer, simply call the Set Int function of the Player Prefs class and pass in the key you want to use to retrieve it again. The Player Prefs class in Unity allows you to save basic data between gameplay sessions, such as options settings, that can be saved and reloaded, even after a game has been closed. Let’s get started… How to use Player Prefs in Unity How to save a colour using Player Prefs.How to save a boolean using Player Prefs.Saving to multiple slots with Player Prefs.Saving and loading multiple settings with Player Prefs.How to save options settings using Player Prefs.When to use the Player Prefs Save function.However, if you’ve tried to use Player Prefs before, particularly to save a game, you may have read or been told not to use them for anything more advanced than basic preference values.Īnd while there’s some truth to that, there are also many reasons why you would choose to use Player Prefs, so long as you understand how they work.īut don’t worry, in this article I’ll show you how to use Player Prefs in Unity, how they work and how you can use them to save different kinds of data, so that you can decide for yourself how to use them in your own project. This can be extremely useful for saving and loading basic sets of data, such as option settings or simplified game saves. The Player Prefs class in Unity is used to save small amounts of data from your game to disk, which allows you to store and recall single string, integer and float values between gameplay sessions.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |