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.
写入所有修改参数到硬盘。
当对一个类应用 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.
写入所有修改参数到硬盘。
相关文章推荐
- Spring IoC容器
- 配置文件的使用 Properties
- Java注释Override、Deprecated、SuppressWarnings详解
- Java中回调函数的使用
- iOS基础面试题(三)
- XP系统不能上网,能ping通dns,ping不通外网解决办法
- UINavigationController详解
- Xamarin iOS UIAlertView
- jquery 用addClass之后 class有对应的事件,为什么要重新绑定一下事件呢
- POJ_2385 Apple Catching(DP)
- [Swift] Any VS AnyObject
- 【iOS学习笔记】block
- 多用引用,而非传值 (Effective C++_20)
- android Volley框架 http请求
- iOS基础面试题(二)
- 【转】android代码CHooseDate问题
- jquery 用addClass之后 class有对应的事件,为什么要重新绑定一下事件呢
- 合唱队(最长子序列问题)
- 使用zlib模块实现HTTP服务端与客户端实现传输数据压缩
- Unity中sdk的学习笔记--Unity与android相互调用