Persistent data storage and unity storage in unity

Source: Internet
Author: User

Persistent data storage and unity storage in unity

Unity provides the PlayerPrefs class for storing game data to a computer's hard drive. This class has 10 functions available

Class Functions
    • SetIntSets the value of the preference identified by key.
      Set the parameter value determined by the key.
    • GetIntReturns the value corresponding to key in the preference file if it exists.
      If yes, return the value corresponding to the key in the preference file.
    • SetFloatSets the value of the preference identified by key.
      Set the parameter value determined by the key.
    • GetFloatReturns the value corresponding to key in the preference file if it exists.
      If yes, return the value of the key in the game archive file.
    • SetStringSets the value of the preference identified by key.
      Set the parameter value determined by the key.
    • GetStringReturns the value corresponding to key in the preference file if it exists.
      If yes, return the value of the key in the game archive file.
    • HasKeyReturns true if key exists in the preferences.
      Returns true if the key exists in the game archive.
    • DeleteKeyRemoves key and its corresponding value from the preferences.
      Delete the key and its value from the game archive.
    • DeleteAllRemoves all keys and values from the preferences. Use with caution.
      Delete all keys from preferences. Use it with caution.
    • SaveWrites all modified preferences to disk.
      Write All modification parameters to the hard disk.

The data stored through the PlayerPrefs function is saved in the hard disk in the form of a key-value pair set.

 

The first parameter of the set function is a string, indicating the name of the data to be set. The second parameter depends on the data type to be saved. If you want to save a floating point data named playerScore, you should write it like this:

PlayerPrefs.SetFloat("Player Score", 10.5);

If you want to store int or string data, you only need to specify the second parameter as the corresponding type.



There are also two get function parameters. The first parameter must be of the sting type, indicating the key name corresponding to the value you want to retrieve. The second parameter indicates that the default value is returned if the corresponding key is not found, the following is an example:

Print (PlayerPrefs. GetString ("Player Name", "not found, I am the default return value "));

In this way, if you find the PlayerName key, it will be printed on the console
"Not found. I am the default return value"

Two common functions are also used;

// Delete the value of a key in PlayerPrefs
PlayerPrefs. DeleteKey ("key"); // you can check whether this key bool B = PlayerPrefs. HasKey ("key") exists in PlayerPrefs ");
Let's get it here today. I feel that the format is not neat. I will think about it later .~~~
We welcome like-minded friends to study and make progress together.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.