Unity Study Notes Chapter 1: Data Archiving

ps: posting the article is just to stabilize my study record, if there is any mistake, please give me advice

The archiving of data is currently learning the single saving method of using PlayerPrefs in Unity and using Json to save

1:PlayerPrefs single save:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class SaveOrLoad : MonoBehaviour
{
    //定义每个变量
    public string name;
    public int lever;
    public float coin;

    //获取每个变量的引用
    public Text nameTxt;
    public Text leverTxt;
    public Text coinTxt;


    //写一个加载数据的方法,把目前的值传给游戏面板
    void UpdateUI()
    {
        nameTxt.text = name;
        leverTxt.text = lever.ToString();
        coinTxt.text = coin.ToString();
    }


    //BUtton加载点击事件

    public void LoadByDate()
    {
        LoadLocalityData();
        UpdateUI();
    }

    public void SaveBtnData()
    {
        SaveByDate();
    }

   
    //playerPrefs单个保存

    //把数据保存到本地
     void SaveByDate()
    {
        PlayerPrefs.SetString("PlayerName", name);
        PlayerPrefs.SetInt("PlayerLever", lever);
        PlayerPrefs.SetFloat("PlayerCoin", coin);
        PlayerPrefs.Save();
    }
    
    
    //加载本地数据上传
    void LoadLocalityData()
    {
        name = PlayerPrefs.GetString("PlayerName");
        lever = PlayerPrefs.GetInt("PlayerLever");
        coin = PlayerPrefs.GetFloat("PlayerCoin");
    }
    

}

Save local data: Use PlayerPrefs.SetInt(key, value) to save data to the local registry

2: Read local data: Use PlayerPrefs.GetInt(key) to read local data and then assign it to the current variable for a data read transformation

Archive using Json:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

[System.Serializable]
public class Data
{
    //存储数据的变量
    public string name;
    public int lever;
    public float coin;

    public string id;
    public int count;
}

public class SystenObj : MonoBehaviour
{
    public Data data = new Data();

    //显示
    public Text nameTxt;
    public Text leverTxt;
    public Text coinTxt;

    public Text NameText;

    public Text TXT;
    void UpdateUI()
    {
        nameTxt.text = data.name;
        leverTxt.text = data.lever.ToString();
        coinTxt.text = data.coin.ToString();
       
    }

    void UpdateDate()
    {
        data.name = NameText.text;
    }
    /// <summary>
    /// 加载数据的事件
    /// </summary>
    public void LoadByBtn()
    {
        LoadByJson();
        UpdateUI();
    }

    /// <summary>
    /// 定一个保存到本地的事件
    /// </summary>
    public void SaveByBtn()
    {
        UpdateDate();
        SaveByJson();
    }

    void SaveByJson()
    {
        string json = JsonUtility.ToJson(data);
        Debug.Log(json);
        PlayerPrefs.SetString("PlayerJson", json);
    }

    void LoadByJson()
    {
        string json = PlayerPrefs.GetString("PlayerJson");
        data = JsonUtility.FromJson<Data>(json);
    }
}

1: Define a data class, write the data variables that need to be stored in the class, here you can serialize this class [System.Serializable] can be seen in the editor

2: Save local data: Use JsonUtilty.ToJson(data), use this method to convert the data into a string json type, and then use PlayerPrefs.SetString(key, value) to store the data locally

3:

Read local data: After using PlayerPrefs.GetString(key) to read local data, assign it to a string type, and then assign a value to the data through JsonUtlty.FromJson(json) to complete the reading

The above are the two storage methods of using PlayerPref, single storage and using Json

In addition to using playerprefs, you can also use File and json storage

 

Guess you like

Origin blog.csdn.net/Yuanyu1028/article/details/127823878