对于PlayerPrefs学习以及存储的研究

来源:互联网 发布:教师资格考试题库软件 编辑:程序博客网 时间:2024/05/21 22:51

   对于游戏存档,储存最高分,排行榜都需要用到储存,现在存储的方式有很多,可以存本地的Json,XML,Sqlite,还有一种就是unity自带的一种存储PlayerPrefs。

那么这个PlayerPrefs如何使用呢?

其实很简单,官方的API是里介绍了:

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

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

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

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

Class Functions类函数

  • SetInt
    Sets the value of the preference identified by key.
    设置由key确定的参数值。
  • GetInt
    Returns the value corresponding to key in the preference file if it exists.
    如果存在,返回偏好文件中key对应的值。
  • SetFloat
    Sets the value of the preference identified by key.
    设置由key确定的参数值。
  • GetFloat
    Returns the value corresponding to key in the preference file if it exists.
    如果存在,返回游戏存档文件中key对应的值。
  • SetString
    Sets the value of the preference identified by key.
    设置由key确定的参数值。
  • GetString
    Returns the value corresponding to key in the preference file if it exists.
    如果存在,返回游戏存档文件中key对应的值。
  • HasKey
    Returns true if key exists in the preferences.
    如果key在游戏存档中存在,返回true。
  • DeleteKey
    Removes key and its corresponding value from the preferences.
    从游戏存档中删除key和它对应的值。 
  • DeleteAll
    Removes all keys and values from the preferences. Use with caution.
    从偏好中删除所有key。请谨慎使用。
  • Save
    Writes all modified preferences to disk.
    写入所有修改参数到硬盘。
这个是可以理解到PlayerPrefs的存储是有局限的,在unty3D中只支持int,string,float三种数据类型的写和读。

代码很简单:

//保存数据PlayerPrefs.SetString("Name",mName);PlayerPrefs.SetInt("Age",mAge);PlayerPrefs.SetFloat("Grade",mGrade)
//读取数据mName=PlayerPrefs.GetString("Name","DefaultValue");mAge=PlayerPrefs.GetInt("Age",0);mGrade=PlayerPrefs.GetFloat("Grade",0F);

这里我们可以得出:

1、unity3D中的数据持久化是以键值对的形式存储的,可以看做一个字典。

2、unity3D中的值通过键名来读取的,当值不存在时,返回默认值。

下面通过一个例子来学习:

第一步,新建一个场景scene1,给其添加脚本,我这里是挂在摄像机上了。用于保存数据。

using UnityEngine;using System.Collections;public class Scene1Script : MonoBehaviour {            //姓名            private string mName="路人甲";            //年龄            private int mAge=20;            //成绩            private float mGrade=75.5F;            void OnGUI()            {                GUILayout.Label("Unity3D数据存储示例程序",GUILayout.Height(25));                //姓名                GUILayout.Label("请输入姓名:",GUILayout.Height(25));                mName=GUILayout.TextField(mName,GUILayout.Height(25));                //年龄                GUILayout.Label("请输入年龄:",GUILayout.Height(25));                mAge=int.Parse(GUILayout.TextField(mAge.ToString(),GUILayout.Height(25)));                //成绩                GUILayout.Label("请输入成绩:",GUILayout.Height(25));                mGrade=float.Parse(GUILayout.TextField(mGrade.ToString(),GUILayout.Height(25)));                //提交数据                if (GUILayout.Button("提交数据", GUILayout.Height(25)))                {                    //保存数据                    PlayerPrefs.SetString("Name", mName);                    PlayerPrefs.SetInt("Age", mAge);                    PlayerPrefs.SetFloat("Grade", mGrade);                    //切换到新场景                    Application.LoadLevel(1);                       }               }}
保存一下,新建场景scene2,同样给给其挂一个脚本用于读取数据。

using UnityEngine;using System.Collections;public class Scene2Script : MonoBehaviour {       private string mName;       private int mAge;       private float mGrade;            void Start ()            {                //读取数据                mName=PlayerPrefs.GetString("Name","DefaultValue");                mAge=PlayerPrefs.GetInt("Age",0);                mGrade=PlayerPrefs.GetFloat("Grade",0F);            }            void OnGUI()            {                    GUILayout.Label("Unity3D数据存储示例程序",GUILayout.Height(25));                    //姓名                    GUILayout.Label("姓名:"+mName,GUILayout.Height(25));                    //年龄                    GUILayout.Label("年龄:"+mAge,GUILayout.Height(25));                    //成绩                    GUILayout.Label("成绩:"+mGrade,GUILayout.Height(25));                    //删除数据                    if(GUILayout.Button("清除数据",GUILayout.Height(25)))                            {                            PlayerPrefs.DeleteAll();                                }                    //返回Scene0                   if(GUILayout.Button("返回场景",GUILayout.Height(25)))                              {                            Application.LoadLevel(0);                               }            }}
保存场景,在BuidingSetting里添加场景。运行scene1,可以跳转到scene2里,数据就显示出来了,scene2里点击清除,再打开scene2就会出现默认的值


点击提交数据:


点击清除数据,再重新打开scene2,





0 0
原创粉丝点击