您的位置:首页 > 移动开发 > Unity3D

[Unity3D]手机3D游戏开发:场景切换与数据存储(PlayerPrefs 类的介绍与使用)

2013-01-25 19:04 941 查看
学习Unity脚本推荐:Unity3D官网索引

在Unity中的数据存储和IOS中字典的存储基本相同,是通过关键字实现数据存储与调用。

下面来介绍一下Unity用来存储数据的PlayerPrefs 类。

使用PlayerPrefs可以在在游戏会话中保持并访问玩家偏好设置。

在Mac OS X上PlayerPrefs存储在-/Library/PlayerPrefs文件夹,

名文unity/[companyname]\[product name].plist,这里company和product是在Project Setting中设置的,相同

的plist用于在编辑器中运行的工程和独立模式.

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

在Web模式下,PlayerPrefs存储在Mac OS X的二进制文件-/Library/Preferences/Unity/WebPlayerPrefs中和Windows的%APPDATA%\Unity\WebPlayerPrefs中,一个偏好设置文件对应一个web播放器URL并且文件大小被限制为1兆。如果超出这个限制,SetInt,SetFloat和SetString将不会存储值并相处一个PlayerPrefsException.

类方法

◆ static function DeleteAll(): void

描述:从设置文件中移除所有键和值,谨慎的使用它们。

◆ static function DeleteKey(key: string): void

描述:从设置文件中移除key和它对应的值。

◆ static function GetFloat(key: string, defaultValue: float=OF): float

描述:如果存在,返回设置文件中key对应的值.如果不存在,它将返回defaultValue。

print(PlayerPrefs.GetFlat("Player score"));

◆ static function GetInt(key: string, defaultValue: int): int

描述:返回设置文件中key对应的值,如果存在.如果不存在,它将返回defaultValue。

print(PlayerPrefs.GetInt("Player score"));

◆ static function GetString(key: string, defaultValue: string=**): string

描述:返回设置文件中key对应的值,如果存在.如果不存在,它将返回defaultValue.

print(PlayerPrefs.GetString("Player Name"));

◆ static function HasKey(key: string): bool

描述:在设置文件如果存在key则返回真.

◆ static function SetFloat(key: string, value: float): void

描述:设置由key确定的值.

print(PlayerPrefs.SetFloat("Player Score", 10.0));

◆ static function SetInt(key: string, value: int): void

描述:设置由key确定的值.

PlayerPrefs.SetInt("Player Score", 10);

◆ static function SetString(key: string, value: string): void

描述:设置由key确定的值.

PlayerPrefs.Setstring("Player Name", "Foobar");

下面通过一个案例简单的演示一下。

首先创建两个场景用来实现场景的跳转,命名为CSDNPrefabs1和CSDNPrefabs2来做实验。

基本需求是在第一个场景中创建一些数据并且跳转到第二个场景中显示出来。

创建一个Scene1.cs脚本:

using UnityEngine;
using System.Collections;

public class Scene0Main : MonoBehaviour {

//储存数据的显示
public string testStr;
public string testInt;
public string testFloat;

//GUI皮肤 为上面我们添加的皮肤
//在外面用鼠标拖动上为它赋值
public GUISkin fontSkin;
//显示的图片
public Texture Imagetexture;

// Use this for initialization
void Start () {
//读取key的值
testStr = PlayerPrefs.GetString("testStr", "default");
testInt = PlayerPrefs.GetInt("testInt", 0).ToString();
testFloat = PlayerPrefs.GetFloat("testFloat", 0).ToString();

}

// Update is called once per frame
void Update () {

}

void OnGUI() {

//将GUI的皮肤设置为我们创建的皮肤
GUI.skin = fontSkin;

//贴上图片
GUI.DrawTexture(new Rect((Screen.width - Imagetexture.width) >>1, 10, 120, 120), Imagetexture);

//添加输入框让用户输入信息,这里面我没有捕获异常,因为用户有可能输入一个不合法的数值
testStr = GUI.TextField (new Rect(10, 200, 200, 50), testStr, 50);
testInt = GUI.TextField (new Rect(10, 250, 200, 50), testInt, 50);
testFloat = GUI.TextField (new Rect(10, 300, 200, 50), testFloat, 50);

//点击按钮保存所有数据
if (GUI.Button(new Rect(220, 200, 150, 100), "commit all"))
{

PlayerPrefs.SetString("testStr", testStr);
PlayerPrefs.SetInt("testInt", int.Parse(testInt));
PlayerPrefs.SetFloat("testFloat", float.Parse(testFloat));
//切换场景到scene1
Application.LoadLevel("scene1");
}
}

}

创建一个Scene2.cs脚本:

using UnityEngine;
using System.Collections;

public class scene1Main : MonoBehaviour {

public string testStr;
public string testInt;
public string testFloat;

public GUISkin fontSkin;
public Texture Imagetexture;

// Use this for initialization
void Start () {
testStr = PlayerPrefs.GetString("testStr", "default");
testInt = PlayerPrefs.GetInt("testInt", 0).ToString();
testFloat = PlayerPrefs.GetFloat("testFloat", 0).ToString();

}

void OnGUI() {
GUI.skin = fontSkin;

GUI.DrawTexture(new Rect((Screen.width - Imagetexture.width) >>1, 10, 120, 120), Imagetexture);

//显示label
GUI.Label(new Rect(10,150,300,50),"testStr = "+ testStr);
GUI.Label(new Rect(10,200,300,50),"testInt = "+ testInt);
GUI.Label(new Rect(10,250,300,50),"testFloat = "+ testFloat);

if (GUI.Button(new Rect(220, 200, 150, 100), "clean all"))
{
//删除所有键值
PlayerPrefs.DeleteAll();
// 返回场景0
Application.LoadLevel("scene0");
}

if (GUI.Button(new Rect(220, 320, 150, 100), "only return"))
{
// 返回场景0
Application.LoadLevel("scene0");
}
}
}

然后添加相关贴图的GUISkin完成部署:



此时运行会报错,因为这两个场景需要Build:



这是再运行就可以看到这个简单的Demo了。


在这里输入完毕之后点击commit all:



在另一个场景中便也能显示了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐