"Go" Unity game Archive The use of the Playerprefs class

Source: Internet
Author: User

Http://www.cnblogs.com/qiaogaojian/p/5969855.html

Unity3d provides a class--playerprefs that is used to persist and read locally. It works very simply by saving the data in a file in the form of a key-value pair, and then the program can take out the last saved value based on that name.
the Playerprefs class supports the saving and reading of data types in 3, floating-point, shaping, and string-type.
the functions corresponding to each are:
Setint (); Save integer data;
GetInt (); Read the shaping data;
setfloat (); Save floating-point data;
getflost (); Read floating-point data;
SetString (); Save string data;
GetString (); Read string-type data;
The usage of these functions is basically consistent with set and is read using GET. Here's an example of how to use it in detail:
first Open the Unity3d, create a new project file, and then create a new C # file in the project view, named _playerprefs
and enter the code:
 

 1 public class _playerprefs:monobehaviour {2 public string Set_name; 3 public string get_name; 4 void Ongui () 5 {6 guilayout.beginhorizontal ("box"); 7 Guilayout.lab El ("Name:"); 8 Set_name=guilayout.textarea (Set_name, 200,guilayout.width (50));  9 if (Guilayout.button ("Storage Data")) 10 {11 12//Save the name we entered locally, named _name ; Playerprefs.setstring ("_name", set_name);}15 GUILAYOUT.E                  Ndhorizontal (); Guilayout.beginhorizontal ("box"), + if (Guilayout.button ("read Data")) 18 {19 20//reads data from local data with name _name; get_name=playerprefs.getstr ING ("_name");}23 Guilayout.label ("The name you entered:" +get_name); guilayout.en Dhorizontal (); 25 26 27}28  } 


To analyze this code, we enter a name in the input box, then click the Store Data button, store the data locally and name _name, then click the Read Data button to find the data named _name from the local data and store it in our defined variable get_name. And then show it with a label.
The results of the operation are as follows:

Playerprefs.setstring ("_name", set_name); The first parameter in this method represents the name of the stored data, and the second parameter represents the specific stored value. Get_name=playerprefs.getstring ("_name"); The first data in this method represents the name of the read data, there is a second parameter, indicating the default value, if the data name does not find the corresponding value, then return the default value, this value can also be written, return null value. Below we will publish this simple project in EXE format, open our published EXE file, enter Zhang San in the input box.

Click the Store Data button, store the data locally, then close the exe file, then open it, then open it and click Read Data: Results obtained

The data was read from the local file and a simple archive operation was completed. The Playerprefs is also available in the Playerprefs class . DeleteKey(key : String) deletes the specified data;Playerprefs.deleteall () deletes all keys; Playerprefs.haskey (key:string) method for judging the existence of data;

"Go" Unity game Archive The use of the Playerprefs class

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.