功能

在游戏会话中储存和访问游戏存档。这个是持久化数据储存,比如保存游戏记录。

静态函数

  • DeleteAll

    Removes all keys and values from the preferences. Use with caution. 从游戏存档中删除所有key。请谨慎使用。

  • DeleteKey

    Removes key and its corresponding value from the preferences. 从游戏存档中删除key和它对应的值。

  • GetFloat

    Returns the value corresponding to key in the preference file if it exists. 如果存在,返回游戏存档文件中key对应的浮点数值。

  • GetInt

    Returns the value corresponding to key in the preference file if it exists. 如果存在,返回游戏存档文件中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。

  • Save

    Writes all modified preferences to disk. 写入所有修改参数到硬盘。

  • SetFloat

    Sets the value of the preference identified by key. 设置由key确定的浮点数值。

  • SetInt

    Sets the value of the preference identified by key. 设置由key键确定的整数值。

  • SetString

    Sets the value of the preference identified by key. 设置由key确定的字符串值。

代码:

  • 保存数据
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的存储是有局限的,在unty3D中只支持int,string,float三种数据类型的写和读。

扩展

由于vector3是Unity3d中非常常见的数据类型,因此在这里我举例把vector3类型扩展到PlayerPrefs里面.

        /// <summary>
/// 存储Vector3类型的值
/// </summary>
public static bool SetVector3(string key, Vector3 vector)
{
return SetFloatArray(key, new float[3] { vector.x, vector.y, vector.z });
} /// <summary>
/// 读取Vector3类型的值
/// </summary>
public static Vector3 GetVector3(string key)
{
float[] floatArray = GetFloatArray(key);
if (floatArray.Length < 3)
return Vector3.zero;
return new Vector3(floatArray[0], floatArray[1], floatArray[2]);
}

把上面的代码放到playerprefs原来的代码里面,就能保存和读取Vector3类型的数据了,其他类型的扩展类似,就不贴代码了.

Unity3d中的PlayerPrefs游戏存档API的扩展的更多相关文章

  1. c# unity PlayerPrefs 游戏存档,直白点就是讲游戏数据本地保存下来

    在游戏会话中储存和访问游戏存档.这个是持久化数据储存,比如保存游戏记录. 我的理解是通过某个特殊的标签来保存在本地,而且该标签为key的意思,初始值不用赋值. 在游戏开发中较为实用. 暂时用到了 Se ...

  2. Unity3D系列教程--使用免费工具在Unity3D中开发2D游戏 第一节

    声明:   本博客文章翻译类别的均为个人翻译,版权全部.出处: http://blog.csdn.net/ml3947,个人博客:http://www.wjfxgame.com. 译者说明:这是一个系 ...

  3. (转)PlayerPrefs游戏存档

    unity3d提供了一个用于本地持久化保存与读取的类——PlayerPrefs.工作原理非常简单,以键值对的形式将数据保存在文件中,然后程序可以根据这个名称取出上次保存的数值.    PlayerPr ...

  4. PlayerPrefs游戏存档

    本地存储.相当于Flash里面的SharedObject. Android位置:机器自身存储的(非扩展卡)/data/data/appname/shared_prefs/{AppName}.xml,如 ...

  5. 【Unity3D】Unity3D之 注册表动态存取游戏存档——PlayerPrefs类

    [Unity3D]Unity3D之 注册表动态存取游戏存档--PlayerPrefs类 1.Unity3D提供了一个用于本地持久化保存与读取的类--PlayerPrefs.工作原理非常简单,以键值对的 ...

  6. Unity3D游戏开发之游戏读/存档功能在Unity3D中的实现

    喜欢我的博客请记住我的名字:秦元培,我的博客地址是:http://qinyuanpei.com 转载请注明出处,本文作者:秦元培, 本文出处:http://blog.csdn.net/qinyuanp ...

  7. [Unity3D]Unity3D持久性数据的游戏开发PlayerPrefs采用

    大家好,我是秦培,欢迎关注我的博客,我的博客地址">blog.csdn.net/qinyuanpei. 博主今天研究了在Unity3D中的数据持久化问题.数据持久化在不论什么一个开发领 ...

  8. 【Unity3d游戏开发】Unity3D中的3D数学基础---向量

    向量是2D.3D数学研究的标准工具,在3D游戏中向量是基础.因此掌握好向量的一些基本概念以及属性和常用运算方法就显得尤为重要.在本篇博客中,马三就来和大家一起回顾和学习一下Unity3D中那些常用的3 ...

  9. Unity3D游戏开发之在Unity3D中视频播放功能的实现

    版权声明:欢迎订阅公众号[5厘米的理想],愿生命里的每个小理想,都能成为生命里的小确幸.本文地址为: https://blog.csdn.net/qinyuanpei/article/details/ ...

随机推荐

  1. Redis实战与 Session缓存

    C#操作Redis的库有很多,比如C# Redis Client就很好用, 在NuGet上搜索 ServiceStack.Redis  安装到项目中,将会添加以下引用 ServiceStack.Red ...

  2. Hibernate框架单向多对多关联映射关系

    建立单向多对多关联关系    Project.java (项目表)                private Integer proid;                private Strin ...

  3. Java设计模式之《职责链模式》及应用场景

    原创作品,可以转载,但是请标注出处地址:http://www.cnblogs.com/V1haoge/p/6530089.html 职责链模式(称责任链模式)将请求的处理对象像一条长链一般组合起来,形 ...

  4. Macaca 自动化框架 [Python 系列]

    介绍 Macaca是一套完整的自动化测试解决方案,基于node.js开发.由阿里巴巴公司开源: 地址:http://macacajs.github.io/macaca/ 特点: 同时支持PC端和移动端 ...

  5. Swap Nodes in Pairs leetcode

    Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...

  6. jQuery获取Select选择的Text和Value

     jQuery获取Select选择的Text和Value:语法解释:1. $("#select_id").change(function(){//code...});   //为S ...

  7. c++ string 对象操作

    字符串转换大小写如下: #include "stdafx.h" #include <iostream> #include <string> using na ...

  8. Gridview 重建表头/单击单元格弹出对话框/改变单元格背景色

    整理工作~ 完整的代码在GitHub上, 路径: 项目背景:追踪某个issue,并且记录每天的状态. 要求:1.点击日期就能更改,并且用颜色标志不同的状态 2.增加按钮可关闭issue 3.布局要求日 ...

  9. 本地存储和cookies之间的区别

  10. 构建微服务-使用OAuth 2.0保护API接口

    微服务操作模型 基于Spring Cloud和Netflix OSS 构建微服务-Part 1 基于Spring Cloud和Netflix OSS构建微服务,Part 2 在本文中,我们将使用OAu ...