首页 > 学院 > 开发设计 > 正文

unity中数据的持久化存储

2019-11-17 02:14:28
字体:
来源:转载
供稿:网友

unity中数据的持久化存储

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

Class Functions类函数

    • SetIntSets the value of the preference identified by key.设置由key确定的参数值。
    • GetIntReturns the value corresponding to key in the preference file if it exists.如果存在,返回偏好文件中key对应的值。
    • SetFloatSets the value of the preference identified by key.设置由key确定的参数值。
    • GetFloatReturns the value corresponding to key in the preference file if it exists.如果存在,返回游戏存档文件中key对应的值。
    • SetStringSets the value of the preference identified by key.设置由key确定的参数值。
    • GetStringReturns the value corresponding to key in the preference file if it exists.如果存在,返回游戏存档文件中key对应的值。
    • HasKeyReturns true if key exists in the preferences.如果key在游戏存档中存在,返回true。
    • DeleteKeyRemoves key and its corresponding value from the preferences.从游戏存档中删除key和它对应的值。
    • DeleteAllRemoves all keys and values from the preferences. Use with caution.从偏好中删除所有key。请谨慎使用。
    • SaveWrites 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”);
今天就先弄到这里吧。感觉格式上还是不太整齐,以后熟悉了会好想一些的。~~~ 欢迎志同道合的朋友一起学习, 一起进步。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表