您的位置:首页 > 其它

PlayerPrefs 游戏存档

2015-08-31 14:54 288 查看
PlayerPrefs 这个类是一个密封类

当对一个类应用 sealed
修饰符时,此修饰符会阻止其他类从该类继承。
如果我们有一个需求,这次打开一个功能时停留在某个界面,下次进入游戏打开这个功能时还在这个界面。
不由服务器提供数据。

方法:
1.一个管理游戏内存的类,里面实现PlayerPrefs提供的方法,存入字典。

public const string GAME_KEY="XXXX”;//不变的常量

public static void SetInt(string key,int value)
{
PlayerPrefs.SetInt(Setting.platform+GAME_KEY+ key, value);
//这样命名可以保证key的唯一性,即使手机上有两个游戏也不要紧,因为每个游戏的GAME_KEY是不一样的。

}

这个类可以把数据存储在硬盘里,下次进入游戏的时候还在。

2.GET value

GameSave.SetInt(KEY_MUSIC,v?1:0);

GameSave.GetInt(KEY_MUSIC)

附:
PlayerPrefs 游戏存档
官方文档及对应的方法

在游戏会话中储存和访问游戏存档。
可以理解为持久化储存,还可以理解为游戏存档, 玩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
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.
写入所有修改参数到硬盘。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: