Unity PlayerPrefs

 对于游戏存档,储存最高分,排行榜都需要用到储存,现在存储的方式有很多,可以存本地的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被存储在注册表的 HKCUSoftware[company name][product name]键下,这里company和product名是在Project Setting中设置的.

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

Class Functions类函数


  • Sets the value of the preference identified by key.
    设置由key确定的参数值。
  • Returns the value corresponding to key in the preference file if it exists.
    如果存在,返回偏好文件中key对应的值。
  • Sets the value of the preference identified by key.
    设置由key确定的参数值。
  • Returns the value corresponding to key in the preference file if it exists.
    如果存在,返回游戏存档文件中key对应的值。
  • Sets the value of the preference identified by key.
    设置由key确定的参数值。
  • Returns the value corresponding to key in the preference file if it exists.
    如果存在,返回游戏存档文件中key对应的值。
  • Returns true if key exists in the preferences.
    如果key在游戏存档中存在,返回true。
  • Removes key and its corresponding value from the preferences.
    从游戏存档中删除key和它对应的值。 
  • Removes all keys and values from the preferences. Use with caution.
    从偏好中删除所有key。请谨慎使用。
  • Writes all modified preferences to disk.
    写入所有修改参数到硬盘。

这个是可以理解到PlayerPrefs的存储是有局限的,在unty3D中只支持int,string,float三种数据类型的写和读。

代码很简单:

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

这里我们可以得出:

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

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

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

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

 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 public class Scene1Script : MonoBehaviour {
 5             //姓名
 6             private string mName="路人甲";
 7             //年龄
 8             private int mAge=20;
 9             //成绩
10             private float mGrade=75.5F;
11             void OnGUI()
12             {
13 
14                 GUILayout.Label("Unity3D数据存储示例程序",GUILayout.Height(25));
15                 //姓名
16                 GUILayout.Label("请输入姓名:",GUILayout.Height(25));
17                 mName=GUILayout.TextField(mName,GUILayout.Height(25));
18                 //年龄
19                 GUILayout.Label("请输入年龄:",GUILayout.Height(25));
20                 mAge=int.Parse(GUILayout.TextField(mAge.ToString(),GUILayout.Height(25)));
21                 //成绩
22                 GUILayout.Label("请输入成绩:",GUILayout.Height(25));
23                 mGrade=float.Parse(GUILayout.TextField(mGrade.ToString(),GUILayout.Height(25)));
24                 //提交数据
25                 if (GUILayout.Button("提交数据", GUILayout.Height(25)))
26                 {
27                     //保存数据
28                     PlayerPrefs.SetString("Name", mName);
29 
30                     PlayerPrefs.SetInt("Age", mAge);
31 
32                     PlayerPrefs.SetFloat("Grade", mGrade);
33                     //切换到新场景
34                     Application.LoadLevel(1);
35                        }
36                }
37 }

保存一下,新建场景scene2,同样给给其挂一个脚本用于读取数据。

 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 public class Scene2Script : MonoBehaviour {
 5        private string mName;
 6        private int mAge;
 7        private float mGrade;
 8             void Start ()
 9             {
10                 //读取数据
11                 mName=PlayerPrefs.GetString("Name","DefaultValue");
12                 mAge=PlayerPrefs.GetInt("Age",0);
13                 mGrade=PlayerPrefs.GetFloat("Grade",0F);
14             }
15             void OnGUI()
16             {
17                     GUILayout.Label("Unity3D数据存储示例程序",GUILayout.Height(25));
18                     //姓名
19                     GUILayout.Label("姓名:"+mName,GUILayout.Height(25));
20                     //年龄
21                     GUILayout.Label("年龄:"+mAge,GUILayout.Height(25));
22                     //成绩
23                     GUILayout.Label("成绩:"+mGrade,GUILayout.Height(25));
24                     //删除数据
25                     if(GUILayout.Button("清除数据",GUILayout.Height(25)))
26                             {
27                             PlayerPrefs.DeleteAll();
28                                 }
29                     //返回Scene0
30                    if(GUILayout.Button("返回场景",GUILayout.Height(25)))
31                               {
32                             Application.LoadLevel(0);
33                                }
34             }
35 }
View Code

保存场景,在BuidingSetting里添加场景。运行scene1,可以跳转到scene2里,数据就显示出来了,scene2里点击清除,再打开scene2就会出现默认的值

点击提交数据:

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

原文链接:http://blog.csdn.net/yeluo_vinager/article/details/50074461

原文地址:https://www.cnblogs.com/Peng18233754457/p/7792295.html