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.