unity之数据储存————PlayerPrfs类

在各种游戏中,由于游戏的数据并不是每一个关卡都要初始化,有的场景需要知道上一个场景中的某些数据,并且根据这些数据来调整游戏的场景,因此需要一个类来保存游戏中的数据 ———— PlayerPrfs类
首先要明确的一点就是,PlayerPrfs类记录消息的机制是通过键值对来记录,每一个标识符都有一个值与其对应,不论是储存数据还是提取数据都需要两者协同合作才能进行
类中的方法

SetInt将需要记录的整型消息用标识符记录下来
SetFloat浮点类型数据
SetString字符串数据
DeleteKey删除标识符
HasKey是否存在标识符,存在返回true反之返回False
DeleteAll删除全部储存的信息
Save保存数据
GetInt/GetFloat/GetString根据标识符获取对应的数据

实例

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerPrefsDemo : MonoBehaviour
{
    /// <summary>
    /// PlayerPrefs类的使用方法与详解
    /// </summary>
    // Start is called before the first frame update

    public Button First;/// <summary>
    /// 三个button变量
    /// </summary>
    public Button Second;
    public Button Thirdd;
    private void Awake()
    {
        PlayerPrefs.SetInt("Fir", 666);//将666整型数据储存
        PlayerPrefs.SetFloat("Sec",0.003f);//储存浮点数据
        PlayerPrefs.SetString("Third",WWW.EscapeURL("unity3D实战训练"));///将字符串中包含的中文字符转义

        Print();//输出数据
        PlayerPrefs.DeleteKey("Fir");//删除类中键值
        Check();
        PlayerPrefs.HasKey("Fir");
    }
    void Start()
    {
        
    }

    private void Print()
    {
        Debug.Log("First value" + PlayerPrefs.GetInt("Fir"));
    }
    // Update is called once per frame
    void Update()
    {
        
    }
    private void Check()
    {
        Debug.Log("Fir's key" + PlayerPrefs.HasKey("Fir"));
        Debug.Log("Sec's key" + PlayerPrefs.HasKey("Sec"));
        Debug.Log("Third's key" + PlayerPrefs.HasKey("Third"));
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值