Unity各类数据库的基本操作(五)-- PlayerPrefs

来源:互联网 发布:js修改css样式 生效 编辑:程序博客网 时间:2024/06/05 03:27


PlayerPrefs 游戏存档

Stores and accesses player preferences between game sessions.

On Mac OS X PlayerPrefs are stored in ~/Library/Preferences folder, in a file named unity.[company name].[product name].plist, where company and product names are the names set up in Project Settings. The same .plist file is used for both Projects run in the Editor and standalone players.

On Windows standalone players, PlayerPrefs are stored in the registry under HKCU\Software\[company name]\[product name] key, where company and product names are the names set up in Project Settings.

On Web players, PlayerPrefs are stored in binary files under ~/Library/Preferences/Unity/WebPlayerPrefs on Mac OS X and %APPDATA%\Unity\WebPlayerPrefs on Windows. There is one preference file per Web player URL and the file size is limited to 1 megabyte. If this limit would be exceeded, SetInt, SetFloat and SetString will not store the value and throw aPlayerPrefsException.

在游戏会话中储存和访问游戏存档。

可以理解为持久化储存,还可以理解为游戏存档, 玩RPG游戏的时候肯定会有游戏存档 存档中就会记录玩家以前游戏的过程,这些都是以数据的形式存在PlayerPrefs中的。

在Mac OS X上PlayerPrefs存储在~/Library/PlayerPrefs文件夹,名为unity.[company name].[product name].plist,这里company和product名是在Project Setting中设置的,相同的plist用于在编辑器中运行的工程和独立模式.

在Windows独立模式下,PlayerPrefs被存储在注册表的 HKCU\Software\[company name]\[product name]键下,这里company和product名是在Project Setting中设置的.

在Web模式,PlayerPrefs存储在Mac OS X的二进制文件 ~/Library/Preferences/Unity/WebPlayerPrefs中和Windows的 %APPDATA%\Unity\WebPlayerPrefs中,一个游戏存档文件对应一个web播放器URL并且文件大小被限制为1MB。如果超出这个限制,SetInt、SetFloat和SetString将不会存储值并抛出一个PlayerPrefsException。



using UnityEngine;using System.Collections;public class PlayerPrefsTest : MonoBehaviour {int j;float f;void Start () {j = PlayerPrefs.GetInt ("MyCount",10);//当PlayerPrefs第一次被使用的时候只创建不会赋值,第二次运行的时候就会赋值了//当第一次使用该键的时候,而且该键没有对应的值得时候,这个值就等于默认值//问:键什么时候会建立//没有键的时候,使用set 方法,会创建该键f = PlayerPrefs.GetFloat("MyFloatssss",0.5f);print (f);//在这直接打印打印的是f = 0.5f的值,而不是“MyFlost”的值print ("MyCount = " + PlayerPrefs.GetInt ("MyCount"));//第一遍打印的是"MyCount"的值,因为第一遍只创建不赋值所以为默认值0 }//end_Startvoid Update () {if (Input.GetKeyDown(KeyCode.A)) {j += 1;PlayerPrefs.SetInt ("MyCount", j);}//将j的值给键MyCountif (Input.GetKeyDown(KeyCode.C)) {//删除键PlayerPrefs.DeleteKey("MyCount");print ("删除MyCount键成功");}//判断是否存在“MyString”这个键if (PlayerPrefs.HasKey ("MyString")) {print ("存在MyString");} else {print ("不存在MyString");}}//end_Update}

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 小米6充电线坏了怎么办 小米6导航信号弱怎么办 麦多多充不了电怎么办 一加数据线坏了怎么办 小米耳机泡水了怎么办 公司拖欠工资公司破产了怎么办 苹果x外壳掉漆怎么办 手机壳按键很硬怎么办 棉质白衣服染色怎么办 白棉t恤混洗染色怎么办 包包被衣服染色了怎么办 白色衣服染了菜汁怎么办 一加3t屏幕刺眼怎么办 怀孕吃了好多杏怎么办 门破了个洞怎么办 钢圈轮毂刮花了怎么办 瓷砖用刀子划了怎么办 陶瓷洗手台裂了怎么办 洗车泵水管坏了怎么办 印胶浆里面渗入了发泡浆怎么办? 管子断在水管里怎么办 衣服上的织带缩水怎么办 真丝衣服拔缝了怎么办 顾客说衣服太花怎么办 铝和碱反应变黑怎么办 40度高温多肉怎么办 沾到医用蓝药水怎么办? 裤子弄上泡沫胶怎么办 苍蝇纸粘衣服上怎么办 苍蝇胶沾衣服上怎么办 灯带为什么不亮怎么办 苹果6比屏幕变黄怎么办 雷腾键盘锁了怎么办 自吸泵电机不转怎么办 孕38周胎儿偏小怎么办 被火烧黑的铁怎么办 锅被烟熏黑了怎么办 墙壁被烟熏黑了怎么办 壁纸被烟熏黑了怎么办 空调被烟熏黑了怎么办 牙被烟熏黑了怎么办