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


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 a PlayerPrefsException.

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

可以理解为持久化储存,还可以理解为游戏存档, 玩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_Start
	
	void Update () 
	{
		if (Input.GetKeyDown(KeyCode.A)) {
			j += 1;	
			PlayerPrefs.SetInt ("MyCount", j);
		}
		//将j的值给键MyCount

		if (Input.GetKeyDown(KeyCode.C)) {
			//删除键
			PlayerPrefs.DeleteKey("MyCount");
			print ("删除MyCount键成功");
		}
		//判断是否存在“MyString”这个键
		if (PlayerPrefs.HasKey ("MyString")) {
			print ("存在MyString");
		} else {
			print ("不存在MyString");
		}

	}//end_Update
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值