
unity之数据储存————PlayerPrfs类
发布日期:2021-05-07 17:59:04
浏览次数:27
分类:技术文章
本文共 1464 字,大约阅读时间需要 4 分钟。
在各种游戏中,由于游戏的数据并不是每一个关卡都要初始化,有的场景需要知道上一个场景中的某些数据,并且根据这些数据来调整游戏的场景,因此需要一个类来保存游戏中的数据 ———— 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{ ////// PlayerPrefs类的使用方法与详解 /// // Start is called before the first frame update public Button First;////// 三个button变量 /// 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")); }}
发表评论
最新留言
路过,博主的博客真漂亮。。
[***.116.15.85]2025年04月12日 23时31分50秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
TypeScript系列文章导航
2019-03-04
base64编码字符串和图片的互转
2019-03-04
linux 下安装kolla报错 提示Cannot uninstall requests
2019-03-04
设计模式之组合模式
2019-03-04
(Python学习笔记):字典
2019-03-04
(C++11/14/17学习笔记):并发基本概念及实现,进程、线程基本概念
2019-03-04
(C++11/14/17学习笔记):线程启动、结束,创建线程多法、join,detach
2019-03-04
leetcode 14 最长公共前缀
2019-03-04
做做Java
2019-03-04
map的find函数和count函数
2019-03-04
C++并发与多线程(一)
2019-03-04
7628 EDCCA认证寄存器修改(认证自适应)
2019-03-04
计算机网络子网划分错题集
2019-03-04
java一些基本程序
2019-03-04
神经元与神经网络一之概述
2019-03-04
FANUC机器人R-30iB_R-30iB PLUS备件规格型号统计整理
2019-03-04
FANUC机器人的镜像备份操作及U盘格式化具体步骤
2019-03-04
vue-依赖-点击复制
2019-03-04
js井子棋
2019-03-04