PlayerPrefs data store

1. PlayerPrefs class

PlayerPrefs is a class provided by the unity engine specifically for data persistence. It provides many static methods inside, which can be used to access different types of data in the program. Currently, the supported data types are only integer, floating point and character string three

1. Storage mechanism

PlayerPrefs internally stores all data in the form of a dictionary, that is, each data is a set of key-value pairs, where the key refers to the data name, must be of string type and cannot be repeated, and the value stores the data value itself, and its type is only Can be integer, floating point and string types, as shown in the following table:

Archive data in PlayerPrefs
data key data value type of data
Name Li Hua string
sex male string

PlayerPrefs follows the principle of "modify if there is, add if there is no", that is, if a certain data to be stored does not exist in it, it will be added in the form of a new set of data, otherwise it will modify the corresponding data value

2. Common methods

The methods contained in PlayerPrefs are all static methods, which can be accessed directly through the class name, as follows:

Static methods in PlayerPrefs
method meaning
SetFloat()、GetFloat() Store and read float type data
SetInt()、GetInt() Store and read int type data
SetString()、GetString() Store and read string type data
DeleteAll() delete all stored data
DeleteKey() Delete the data of a key value
HasKey Determine whether there is data whose key is key
Save() Immediately write cached data to disk
public static void SetInt (string key, int value);
// 设置由 key 键确定的整数值
 
public static void SetFloat (string key, float value);
// 设置由 key 键确定的浮点数值
 
public static void SetString (string key, string value);
// 设置由 key 键确定的字符串值
 
public static int GetInt (string key);
public static int GetInt (string key, int defaultValue);
// 获取存储文件中 key 键对应的整数值(不存在则返回 defaultValue)
 
public static float GetFloat (string key);
public static float GetFloat (string key, float defaultValue);
// 获取存储文件中 key 键对应的浮点数值(不存在则返回 defaultValue)
 
public static string GetString (string key);
public static string GetString (string key, string defaultValue);
// 获取存储文件中 key 键对应的字符串值(不存在则返回 defaultValue)
 
public static void Save ();
// 将所有修改的内容写入硬盘(默认情况下,Unity 会在 OnApplicationQuit() 执行过程中自动写入)
 
public static bool HasKey (string key);
// 判断 key 键是否存在(存在则返回 true)
 
public static void DeleteKey (string key);
// 删除 key 键以及其对应的值
 
public static void DeleteAll ();
// 删除所有存储内容(需谨慎使用)

3. Application case (login and registration)

(1) The interface effect is as follows:

 

(2) The basic components are the introduction

 (1) canvas canvas (right click->UI->canvas)

(2) RawImage is used to store the background image (right click -> UI -> RawImage)

(3) login empty object (right click -> Create Empty), used to hang the code login class (the code is behind)

(4) text text displays "Welcome to the registration login interface" (right click -> UI -> text)

(5) username Enter the username (right click -> Input Filed), and the text of the component placeholder is set to "Please enter the username"

 

 (6) passwad Enter the password (right click -> Input Filed), and the text of the component placeholder is set to "Please enter the password"

(7) confirmpassword Confirm password (right button -> Input filed) component placeholder text is set to "Please confirm password"

(8) remindertext is a text component (right click -> UI -> text) and the text is set to "Registration Login Feedback Prompt"

(9) Register button (right button -> UI -> button) text is set to "register"

(10) Login button (copy and paste the registration button, put the two buttons in the same position) text is set to "login", the login button needs to be canceled, only one registration and login is displayed (√cancel on the top right)

(11) Togglegroup Empty object, endow the component with toggle group (right -> Add Component -> Toggle Group)

(12) Toggle registration switch, a group of switches can only be turned on (only one can be selected for registration and login), the background color is set to blue (can be set by yourself), the text of the label is set to "register", copy and paste the label on the background Next, the settings for toggle registration are as follows:

 (13) Toggle login, toggle registration copy and paste, label content is changed to login, label (1) content is also changed to login, label (1) is canceled (the uppermost √ on the right is removed), and the settings are as follows

 (3) Code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
using UnityEngine.SceneManagement;
using MySql.Data.MySqlClient;
using System.Data;
public class LoginClass : MonoBehaviour
{
    //进入前变量
    public InputField username, password, confirmPassword;
    public Text reminderText;
    public int errorsNum;
    public Button loginButton;
    public GameObject hallSetUI, loginUI;
    //进入后变量
    public static string myUsername;

 
    
    public void Register()
    {
       
        if (PlayerPrefs.GetString(username.text) == "")
        {
            if (password.text == confirmPassword.text)
            {
                PlayerPrefs.SetString(username.text, username.text);
                PlayerPrefs.SetString(username.text + "password", password.text);
             
                reminderText.text = "注册成功!";
            }
            else
            {
                reminderText.text = "两次密码输入不一致";
            }
        }
        else
        {
            reminderText.text = "用户已存在";
        }

    }
    private void Recovery()
    {
        loginButton.interactable = true;
    }
    public void Login()
    {
      
        if (PlayerPrefs.GetString(username.text) != "")
        {
            if (PlayerPrefs.GetString(username.text + "password") == password.text)
            {
                
                reminderText.text = "登录成功";
                myUsername = username.text;
                hallSetUI.SetActive(true);
                loginUI.SetActive(false);
                SceneManager.LoadScene(0);
            }
            else
            {
                reminderText.text = "密码错误";
                errorsNum++;
                if (errorsNum >= 3)
                {
                    reminderText.text = "连续错误3次,请30秒后再试!";
                    loginButton.interactable = false;
                    Invoke("Recovery", 5);
                    errorsNum = 0;
                }
            }
        }
        else
        {
            reminderText.text = "账号不存在";
        }
    }
}

(4) Data query after registration and login

(1) edit->project settings, view the project name

 

 

(2) Windows+R input regedit

 (3) Find the file path, scroll down in the software folder, find unity, then find your project, click on the right to see the stored data (there will be a record here after registration, no registration There will be no records), double-click to view the value, right-click to modify, delete, etc.

 

 

 

Guess you like

Origin blog.csdn.net/lxy20011125/article/details/130145944