PlayerPrefs 游戏存档

来源:互联网 发布:telnet 端口打开失败 编辑:程序博客网 时间:2024/05/17 21:51

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 XPlayerPrefs存储在~/Library/PlayerPrefs文件夹,名为unity.[company name].[product name].plist,这里companyproduct名是在Project Setting中设置的,相同的plist用于在编辑器中运行的工程和独立模式.

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

Web模式,PlayerPrefs存储在Mac OS X的二进制文件 ~/Library/Preferences/Unity/WebPlayerPrefs中和Windows %APPDATA%\Unity\WebPlayerPrefs中,一个游戏存档文件对应一个web播放器URL并且文件大小被限制为1MB。如果超出这个限制,SetIntSetFloatSetString将不会存储值并抛出一个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.
  • 写入所有修改参数到硬盘。




0 0
原创粉丝点击