• About
  • Advertise
  • Privacy & Policy
  • Contact
Tech News, Magazine & Review WordPress Theme 2017
  • Home
    • Home – Layout 1
    • Home – Layout 2
    • Home – Layout 3
    • Home – Layout 4
    • Home – Layout 5
  • Review
  • Gaming
  • Gear

    Trending Tags

    • Best iPhone 7 deals
    • Apple Watch 2
    • Nintendo Switch
    • CES 2017
    • Playstation 4 Pro
    • iOS 10
    • iPhone 7
    • Sillicon Valley
  • Computers
  • Applications
  • Security
No Result
View All Result
  • Home
    • Home – Layout 1
    • Home – Layout 2
    • Home – Layout 3
    • Home – Layout 4
    • Home – Layout 5
  • Review
  • Gaming
  • Gear

    Trending Tags

    • Best iPhone 7 deals
    • Apple Watch 2
    • Nintendo Switch
    • CES 2017
    • Playstation 4 Pro
    • iOS 10
    • iPhone 7
    • Sillicon Valley
  • Computers
  • Applications
  • Security
No Result
View All Result
Overflow Archives
No Result
View All Result

Unity PlayerPrefs – Save Data in Unity 3D Using Built-in Methods

by
Home Unity
Share on FacebookShare on Twitter

PlayerPrefs is a built-in utility in Unity 3D which provides the option to store certain types of data in the application. It makes the data sharing easier between scenes, PlayerPrefs can be created by setting a key/value pair, similar to dictionary or hashtable.

Unity PlayerPrefs – Save Data in Unity 3D Using Built-in Methods - OverflowArchives
Unity PlayerPrefs – Save Data in Unity 3D

Today we’re going to see how to use the PlayerPrefs in Unity 3D for storing the game information like username, scores, and level information. PlayerPrefs in Unity 3D is limited to certain data types, so storing collections is not going to work in this.

List of Data Types and Functions Available in PlayerPrefs

Below listed items are the static methods in Unity PlayerPrefs, will see one by one.

SetFloatSets the Float value of the preference identified by a key.
SetIntSets the Integer value of the preference identified by a key.
SetStringSets the String value of the preference identified by a key.
HasKeyReturns true if the key exists in the preferences.
GetFloatReturns the Float corresponding to key in the preference file if it exists.
GetIntReturns the integer value corresponding to key in the preference file if it exists.
GetStringReturns the value corresponding to key in the preference file if it exists.
SaveWrites all modified preferences to disk.
DeleteKeyRemoves key and its corresponding value from the preferences.
DeleteAllRemoves all keys and values from the preferences.

Set methods are used to create/store the values into the preference. To set or create value in PlayerPrefs we need a key identifier for each value. Below are some example of setting the values

SetFloat

To set float value use the following method. PlayerPrefs.SetFloat("yourKey", value); Here yourKey is the key which is the identifier for your float value.
Ex: PlayerPrefs.SetFloat("playTime", 1.30f); In this playTime is the key and 1.30f is the value which I’m going to save in the preference.

SetInt

Similarly, for integer value use the following method. PlayerPrefs.SetInt("yourKey", value); Here yourKey is the key which is the identifier for your integer value.
Ex: PlayerPrefs.SetInt("score", 4562); In this playTime is the key and 4562 is the value which I’m going to save in the preference.

SetString

And, for string value use the following method. PlayerPrefs.SetString("yourKey", value); Here yourKey is the key which is the identifier for your string/text value.
Ex: PlayerPrefs.SetString("playerName", "Tilak"); In this playTime is the key and 4562 is the value which I’m going to save in the preference.

HasKey

HasKey the method is used to check whether the given key is present in the PlayerPrefs when you’re going to retrieve the value. To avoid getting null, it is recommended to check for the key. PlayerPrefs.HasKey("yourKey")Here yourKey is the key identifier which is holding your value.

This method will help you to verify the preference and manipulate the values.

Get methods are used to retrieve the values from the preference. To get value in PlayerPrefs we need to use the key identifier for each value. Below are examples of getting the values from the PlayerPrefs.

GetFloat

To get float value use the following method. PlayerPrefs.GetFloat("yourKey"); Here yourKey is the key which is the identifier for the stored preference value..
Ex: PlayerPrefs.GetFloat("playTime"); In this playTime is the key and it will return the values saved in the preference.

GetInt

For the integer value using the following method. PlayerPrefs.GetInt("yourKey"); Here yourKey is the key which is the identifier for the stored preference value.
Ex: PlayerPrefs.GetInt("score"); In this score is the key and it will return the values saved in the preference.

GetString

Same as to get String value use the following method. PlayerPrefs.GetString("yourKey"); Here yourKey is the key which is the identifier for the stored preference value.
Ex: PlayerPrefs.GetString("playerName"); In this playerName is the key and it will return the values saved in the preference.

Save

You can save all the modified preferences into the local disk by using this method. To save the preference use PlayerPrefs.Save(); This will store all preferences in the disk at this location.

On Windows standalone players, PlayerPrefs are stored in the registry under HKCU\Software[company name][product name] key

Web players, PlayerPrefs are stored in binary files under %APPDATA%\Unity\WebPlayerPrefs on Windows.

Android – /data/data/<yourpackageName>/shared_prefs/<yourpackageName>.xml

Mac OS X PlayerPrefs are stored in ~/Library/Preferences folder, in a file, named unity.[company name].[product name].plist.

Where company and product names are the names set up in Project Settings.

DeleteKey

DeleteKey method is used to delete a key value from the preference, it will remove the preference value of the given key. Ex: PlayerPrefs.DeleteKey("playTime"); this will remove the key and its corresponding value from the preferences.

DeleteAll

DeleteAll method is used to clear all the key and values from the preference. As it can’t be undone use this method with caution. PlayerPrefs.DeleteAll(); this will remove all keys and values from the preferences.

Pros and Cons of PlayerPrefs in Unity

Even though using the PlayerPrefs to store and access is easier and fast, there are some points we have to keep in mind.

1. PlayerPrefs are not recommended to save sensitive data.
2. Stored data are not obfuscated or encrypted. Players game easily find the file and modify the information stored.
3. It is slower than the other methods.
4. WebGL build storage is limited to 1MB
5. And the file format for each platform is different, so multi-platform support is tough.

However, in my view, Serializing is a better and professional way to handle the data. Happy Coding!

Tags: saving data in unityunity 3dunity 3d for beginnersunity 3d tutorialunity data savingunity playerprefsunity storageunity tutorialunity tutorial for beginners

Next Post
Web URL Preview in Android – Android Tutorial To Load Web URL Preview UI

Web URL Preview in Android - Android Tutorial To Load Web URL Preview UI

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recommended.

How to Create Android Custom Viewpager Adapter – Android Tutorial

How to Create Android Custom Viewpager Adapter – Android Tutorial

How To Parse The JSON File In Unity 3D  C# – Unity 3D Tutorial

How To Parse The JSON File In Unity 3D C# – Unity 3D Tutorial

Trending.

Create Switch button UI in Unity 3D - overflow archives

Create Switch button UI in Unity 3D

Unity Gradient Effect UI Design Without Scripting | Simple Gradient Effects in Unity 3D

Unity Gradient Effect UI Design Without Scripting | Simple Gradient Effects in Unity 3D

How to Create circular progress bar in Unity 3D with rounded edge

Parse Color Runtime In Unity 3D - Thumbnail

Change Color in Runtime – Unity 3D Tutorial

Progress bar in Unity 3D

How to Create Progress bar in Unity 3D

Overflow Archives

© 2023 OverflowArchives.

Navigate Site

  • About
  • Advertise
  • Privacy & Policy
  • Contact

Follow Us

No Result
View All Result
  • Home
  • Review
  • Apple
  • Applications
  • Computers
  • Gaming
  • Gear
    • Audio
    • Camera
    • Smartphone
  • Microsoft
  • Photography
  • Security

© 2023 OverflowArchives.