星空网 > 软件开发 > ASP.net

unity中数据的持久化存储

unity 提供了PlayerPrefs这个类用于存储游戏数据到电脑硬盘中。 这个类有10个函数可以使用

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.
      写入所有修改参数到硬盘。

通过PlayerPrefs 类函数保存的数据都以 键值对集合的形式保存在硬盘中。

 

其中   set 类函数 的第一个参数为 字符串,表示要设置的数据的名称,第二个参数就要看要保存的 数据类型了。如果想要保存一个浮点型数据,名字是 playerScore,那么应该像这样写:

PlayerPrefs.SetFloat("Player Score", 10.5);

同样的如果想要博存的是int 类型的数据或者 string类型的数据,那么只需要把第二个参数指定为相应的类型就可以了。



关于get 类函数参数也有两个。第一个必须是 sting 类型的参数,表示你要取出的值对应的键名称,第二个参数则表示 如果没找到对应的键的情况下返回的默认值,下面是 一个例子:

print (PlayerPrefs.GetString("Player Name","没找到,我是默认返回值"));

像这样,如果买有找到 PlayerName 这个键 ,那么将会在控制台 打印
"没找到,我是默认返回值"

还用两个常用的函数;

//删除 PlayerPrefs 中某一个key的值
  PlayerPrefs. DeleteKey (“key”);
 
  //判断 PlayerPrefs中是否存在这个key
  bool b = PlayerPrefs.HasKey(“key”);
 今天就先弄到这里吧。感觉格式上还是不太整齐,以后熟悉了会好想一些的。~~~ 
欢迎志同道合的朋友一起学习, 一起进步。





原标题:unity中数据的持久化存储

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

国内商标:唯一性是否有保障?:https://www.kjdsnews.com/a/1314011.html
商标申请:宣誓费用攻略:https://www.kjdsnews.com/a/1314012.html
商标注册:快7个月即可拿到证书:https://www.kjdsnews.com/a/1314013.html
商标注册:多长时间拿到证书?:https://www.kjdsnews.com/a/1314014.html
商标注册费用有多少?你需要知道的一切:https://www.kjdsnews.com/a/1314015.html
商标注册证:多少钱才能拥有?:https://www.kjdsnews.com/a/1314016.html
南京浦口都有什么好玩的地方 南京浦口都有什么好玩的地方推荐:https://www.vstour.cn/a/363180.html
永康白云风景区怎么走 白云山风景区怎么去??:https://www.vstour.cn/a/363181.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流