Unity - usage of the Game Archive PlayerPrefs class

Reprinted from CNBlogs Qiao Gaojian :

 

unity3d provides a class for local persistent saving and reading - PlayerPrefs. The working principle is very simple, the data is saved in the file in the form of key-value pairs, and then the program can retrieve the last saved value according to this name.
    The PlayerPrefs class supports saving and reading of 3 data types, float, integer, and string.
    The corresponding functions are:
    SetInt();Save integer data;
    GetInt();Read integer data;
    SetFloat();Save floating point data;
    GetFlost();Read floating point data;
    SetString();Save String data;
    GetString(); Read string data;
the usage of these functions is basically the same as using Set to save and Get to read. Let's explain the usage in detail based on an example:
   first open unity3D, create a new project file, then create a new c# file in the project view, name it _Playerprefs  
   and enter the code:

public class _Playerprefs : MonoBehaviour {
 2          public string set_NAME;
 3          public string get_NAME;
 4          void OnGUI()
 5          {
 6                  GUILayout.BeginHorizontal("box");
 7                  GUILayout.Label("姓名:");
 8                  set_NAME=GUILayout.TextArea(set_NAME, 200,GUILayout.Width(50));
 9                  if(GUILayout.Button("存储数据"))
 10                   {
 11  
12                          // Save the name we entered locally and name it _NAME; 
13                          PlayerPrefs.SetString( " _NAME " , set_NAME);
 14                   }
 15                   GUILayout.EndHorizontal();
 16                   GUILayout.BeginHorizontal( " box " );
 17                   if (GUILayout.Button( " Read data " ))
 18                   {
 19  
20                           // Read the data named _NAME in the local data; 
21                         get_NAME=PlayerPrefs.GetString( " _NAME " );
 22                   }
 23                   GUILayout.Label( " Your name: " + get_NAME);
 24                   GUILayout.EndHorizontal();
 25                   
26  
27           }
 28   }

Analyze this code, we enter a name in the input box, then click the save data button, store the data locally and name it _NAME, then click the read data button to find the data named _NAME from the local data , and store it in the variable get_NAME we defined, and then display it with Label. The
results are as follows:

 PlayerPrefs.SetString("_NAME", set_NAME); In this method, the first parameter represents the name of the stored data, and the second parameter represents the specific stored value. get_NAME=PlayerPrefs.GetString("_NAME"); In this method, the first data represents the name of the read data, and there is a second parameter, which represents the default value. If the corresponding value is not found through the data name, it will return The default value, which can also be written, returns a null value. Next, we will publish this simple project into EXE format, open the EXE file we published, and enter Zhang San in the input box. As shown in the figure:

QQ screenshot 20140116160855.png

Click the save data button to store the data locally, then close the exe file, then open it again, and click read data after opening: the result is as shown in the figure:

The data was read from the local file by us, and a simple archive operation was completed. The PlayerPrefs class also provides PlayerPrefs . DeleteKey ( key : string) to delete the specified data; PlayerPrefs .DeleteAll() to delete all keys; PlayerPrefs .HasKey (key : string) to determine whether the data exists;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324862612&siteId=291194637