Go The use of data persistence playerprefs for Unity3d game development

Source: Internet
Author: User

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

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.