[Unity3D] use of data persistence PlayerPrefs in Unity3D Game Development

Source: Internet
Author: User

Hello everyone, I'm Qin Yuanpei. Welcome to follow my blog. My blog address is blog.csdn.net/qinyuanpei.

Today, the blogger has studied data persistence in Unity3D. Data Persistence is a noteworthy issue in any development field, from configuration file read/write in an application to database management and maintenance. In the Article C # implement data persistence framework Xml4DB Based on Linq and reflection, the blogger introduced the Xml4DB framework developed during the winter vacation, this is a lightweight Xml-based data persistence framework that can process data in an object-oriented manner. In a sense, data persistence is a process of serialization and deserialization. In. NET, we can serialize objects into Xml, Json, and binary. Then, the object is re-obtained through deserialization. Similarly, in Android, we can use Preferences to store key-value data for data persistence (of course, there are other methods. Here we only want to emphasize key-value data ). How can we achieve data persistence in Unity3D? Please follow me into today's article: [Unity3D] use of data persistence PlayerPrefs in Unity3D Game Development

First, let's look at the simple code for reading and writing data in two Unity3D sections:

// Save the 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 );

Through the above two pieces of code, we can find two points:

1. Data Persistence in Unity3D is stored as a key value and can be viewed as a dictionary.

2. The value of Unity3D is read by the key name. If the value does not exist, the default value is returned.

Currently, Unity3D only supports reading int, string, and float data types, so we can use these three data types to store simple data. Currently, the data persistence class in Unity3D is layerPrefs. The main class methods include:

Static function DeleteAll (): void Description: removes all keys and values from the settings file and uses them with caution. Static function DeleteKey (key: string): void Description: removes the key and its corresponding value from the setting file. Static function GetFloat (key: string, defaultValue: float = OF): float Description: If yes, return the value corresponding to the key in the setting file. If no, it returns defaultValue. Static function GetInt (key: string, defaultValue: int): int Description: return the value corresponding to the key in the settings file. If yes. If no, it returns defaultValue. Static function GetString (key: string, defaultValue: string = **): string Description: return the value of the key in the setting file. If yes. if it does not exist, it returns defavalue value. static function HasKey (key: string): bool Description: returns true if a key exists in the setting file. static function SetFloat (key: string, value: float): void Description: set the value determined by the key. static function SetInt (key: string, value: int): void Description: set the value determined by the key. static function SetString (key: string, value: string): void Description: set the value determined by the key.
Now, after learning about the main layerPrefs method, let's take a specific example to learn how to implement data persistence in Unity3D, we hope to input information in a scenario to read the information in the new scenario. We directly create two scenarios named Scene0 and Scene1 respectively (it is said that the number of programmers starts from 0, haha), and keep the main camera in the scenario. Next we will write scripts for two scenarios:

Script For the first scenario:

Using UnityEngine; using System. collections; public class Scene1Script: MonoBehaviour {// name private string mName = "passer-by"; // age private int mAge = 20; // score private float mGrade = 75.5F; void OnGUI () {GUILayout. label ("Unity3D data storage sample program", GUILayout. height (25); // name GUILayout. label ("Enter name:", GUILayout. height (25); mName = GUILayout. textField (mName, GUILayout. height (25); // age GUILayout. label ("Enter age:", GUILayout. height (25); mAge = int. parse (GUILayout. textField (mAge. toString (), GUILayout. height (25); // score GUILayout. label ("Enter the score:", GUILayout. height (25); mGrade = float. parse (GUILayout. textField (mGrade. toString (), GUILayout. height (25); // submit the data if (GUILayout. button ("submit data", GUILayout. height (25) {// Save the data PlayerPrefs. setString ("Name", mName); PlayerPrefs. setInt ("Age", mAge); PlayerPrefs. setFloat ("Grade", mGrade); // switch to the new Application scenario. loadLevel ("Scene1 ");}}}
Script for the second scenario:

Using UnityEngine; using System. collections; public class Scene2Script: MonoBehaviour {private string mName; private int mAge; private float mGrade; void Start () {// read data mName = PlayerPrefs. getString ("Name", "DefaultValue"); mAge = PlayerPrefs. getInt ("Age", 0); mGrade = PlayerPrefs. getFloat ("Grade", 0F);} void OnGUI () {GUILayout. label ("Unity3D data storage sample program", GUILayout. height (25); // name GUILayout. label ("name:" + mName, GUILayout. height (25); // age GUILayout. label ("Age:" + mAge, GUILayout. height (25); // score GUILayout. label ("score:" + mGrade, GUILayout. height (25); // Delete the data if (GUILayout. button ("Clear Data", GUILayout. height (25) {PlayerPrefs. deleteAll ();} // returns Scene0 if (GUILayout. button ("return scenario", GUILayout. height (25) {Application. loadLevel ("Scene0 ");}}}
Here we directly bind the script to the camera, and then compile the project. Note that we put the two scenarios into the compilation sequence and run the program:


Now, this is what we are talking about today. Thank you for your attention to my blog!

If you like my blog, please remember my name: Qin Yuanpei. My blog address is blog.csdn.net/qinyuanpei.

Reprinted please indicate the source, Author: Qin Yuanpei, the source of this article: http://blog.csdn.net/qinyuanpei/article/details/24195977




Related Article

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.