Transfer from http://blog.csdn.net/qinyuanpei/article/details/24195977
First, let's take a look at the simple code for data reading and writing in two sections of Unity3d:
Save Data playerprefs.setstring ("Name", mname); Playerprefs.setint ("Age", MAge); Playerprefs.setfloat ("Grade", Mgrade)
Read Data mname=playerprefs.getstring ("Name", "DefaultValue"); Mage=playerprefs.getint ("Age", 0); Mgrade=playerprefs.getfloat ("Grade", 0F);
With the above two pieces of code, we can find two points:
1. Data persistence in Unity3d is stored in the form of a key value, which can be viewed as a dictionary.
2, the Unity3d value is read by the key name, the value does not exist, returns the default.
Currently, only three data types such as int, string, float are supported in Unity3d, so we can use these three types of data to store simple data. Currently, the classes used in Unity3d for data persistence are layerprefs, and the main class methods are:
static function DeleteAll (): void Description: Remove all keys and values from the settings file and use them with caution. static function DeleteKey (key:string): void Description: Removes the key and its corresponding value from the settings file. static function GetFloat (key:string, defaultvalue:float=of): Float Description: If present, Returns the value corresponding to the key in the settings file. If it does not exist, it will return DefaultValue. static function GetInt (key:string, defaultvalue:int): int Description: Returns the value corresponding to the key in the settings file, if present. If it does not exist, it will return DefaultValue. static function GetString (key:string, defaultvalue:string=**): String Description: Returns the value corresponding to the key in the settings file. If it exists, it will return DefaultValue if it does not exist. static function Haskey (key:string): BOOL Description: Returns True if a key is present in the settings file. static function SetFloat (key:string, value:float): void Description: Sets the value determined by key. static function Setint (key:string, value:int): void Description: Sets the value determined by key. static function SetString (key:string, value:string): void Description: Sets the value determined by key.
Go The use of data persistence playerprefs for Unity3d game development