您当前的位置: 首页 >  unity

unity工具人

暂无认证

  • 2浏览

    0关注

    205博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Unity3D 本地数据持久化记录存储

unity工具人 发布时间:2021-07-26 21:45:00 ,浏览量:2

本地数据持久化
  • 一、 PlayerPrefs
    • 使用案例
  • 二、xml
  • 三、json

一、 PlayerPrefs

PlayerPrefs是unity自带的一个数据存储类 描述 PlayerPrefs 是一个在游戏会话之间存储玩家偏好的类。它可以将字符串、浮点数和整数值存储到用户的平台注册表中。

Unity 根据应用程序运行的操作系统以不同方式存储“PlayerPrefs”数据。在本页给出的文件路径中,公司名称和产品名称是您在 Unity 的Player Settings 中设置的名称。

独立播放器存储位置

在 macOS 上,PlayerPrefs 存储在~/Library/Preferences/com.ExampleCompanyName.ExampleProductName.plist. Unity 对编辑器和独立播放器中的项目使用相同的 .plist 文件。

在 Windows 上,PlayerPrefs 存储在HKCU\Software\ExampleCompanyName\ExampleProductNamekey 中。

在 Linux 上,PlayerPrefs 存储在~/.config/unity3d/ExampleCompanyName/ExampleProductName.

在 Windows 应用商店应用中,PlayerPrefs 存储在%userprofile%\AppData\Local\Packages[ProductPackageId]\LocalState\playerprefs.dat.

在 Windows Phone 8 上,Unity 将 PlayerPrefs 数据存储在应用程序的本地文件夹中。有关详细信息,请参阅Directory.localFolder。

在 Android 上,PlayerPrefs 存储在/data/data/pkg-name/shared_prefs/pkg-name.v2.playerprefs.xml. Unity 将 PlayerPrefs 数据存储在设备上的SharedPreferences 中。C#、JavaScript、Android Java 和本机代码都可以访问 PlayerPrefs 数据。

在 WebGL 上,Unity 使用浏览器的 IndexedDB API 存储 PlayerPrefs 数据。有关更多信息,请参阅IndexedDB。

编辑器内播放模式存储位置

在 macOS 上,PlayerPrefs 存储在/Library/Preferences/[bundle identifier].plist.

在 Windows 上,PlayerPrefs 存储在HKCU\Software\Unity\UnityEditor\ExampleCompanyName\ExampleProductNamekey 中。Windows 10 使用应用程序的 PlayerPrefs 名称。例如,Unity 添加一个DeckBase字符串并将其转换为 DeckBase_h3232628825。应用程序忽略扩展名。

Unity 将 PlayerPrefs 存储在本地注册表中,没有加密。不要使用 PlayerPrefs 数据来存储敏感数据。

静态函数说明DeleteAll从首选项中删除所有键和值。谨慎使用。DeleteKey从 PlayerPrefs 中删除给定的键。如果该键不存在,则 DeleteKey 没有影响。GetFloat如果存在,则返回与首选项文件中的键对应的值。GetInt如果存在,则返回与首选项文件中的键对应的值。GetString如果存在,则返回与首选项文件中的键对应的值。HasKey如果给定的键存在于 PlayerPrefs 中,则返回 true,否则返回 false。Save将所有修改的首选项写入磁盘。SetFloat设置由给定键标识的首选项的浮点值。您可以使用 PlayerPrefs.GetFloat 来检索此值。SetInt为给定键标识的首选项设置单个整数值。您可以使用 PlayerPrefs.GetInt 来检索此值。SetString为给定键标识的首选项设置单个字符串值。您可以使用 PlayerPrefs.GetString 来检索此值。 使用案例

不得不说,这些函数用起来是真舒服

//保存数据

PlayerPrefs.SetString("Name",mName);
PlayerPrefs.SetInt("Age",mAge);
PlayerPrefs.SetFloat("Grade",mGrade)
//读取数据

mName=PlayerPrefs.GetString("Name","DefaultValue");
mAge=PlayerPrefs.GetInt("Age",0);
mGrade=PlayerPrefs.GetFloat("Grade",0F);
//清除所有记录
 PlayerPrefs.DeleteAll();
//删除其中某一条记录
PlayerPrefs.DeleteKey("Age");
//将记录写入磁盘
PlayerPrefs.Save()
二、xml

https://blog.csdn.net/weixin_45023328/article/details/108150293

三、json

https://blog.csdn.net/weixin_45023328/article/details/107786166 ———————————————— 文档:https://docs.unity3d.com/ScriptReference/PlayerPrefs.html 原文链接:https://blog.csdn.net/piai9568/article/details/98885750

关注
打赏
1656671177
查看更多评论
立即登录/注册

微信扫码登录

0.0479s