Unity游戏存档-PlayerPrefs类

对于游戏存档除了XML,Json,Sqlite,unity提供了一个类可以用来存储数据,那就是PlayerPrefs。

这个类对应的API如下

Class Functions类函数

  • Sets the value of the preference identified by key.
    设置由key确定的参数值。
  • Returns the value corresponding to key in the preference file if it exists.
    如果存在,返回偏好文件中key对应的值。
  • Sets the value of the preference identified by key.
    设置由key确定的参数值。
  • Returns the value corresponding to key in the preference file if it exists.
    如果存在,返回游戏存档文件中key对应的值。
  • Sets the value of the preference identified by key.
    设置由key确定的参数值。
  • Returns the value corresponding to key in the preference file if it exists.
    如果存在,返回游戏存档文件中key对应的值。
  • Returns true if key exists in the preferences.
    如果key在游戏存档中存在,返回true。
  • Removes key and its corresponding value from the preferences.
    从游戏存档中删除key和它对应的值。 
  • Removes all keys and values from the preferences. Use with caution.
    从偏好中删除所有key。请谨慎使用。
  • Writes all modified preferences to disk.
    写入所有修改参数到硬盘。
PlayerPrefs类的用法概要:

1.存储的数据类型只支持int,float,string三种类型。

2.在unity中该类是使用键值对存储数据的。

3.当通过键名来读取值得时候,如果对应的值不存在,就返回默认值。

4.基本用法:

PlayerPrefs.SetString("Name",mName);  
PlayerPrefs.SetInt("Age",mAge);  
PlayerPrefs.SetFloat("Grade",mGrade)  

PlayerPrefs.SetString("Name",mName);  
PlayerPrefs.SetInt("Age",mAge);  
PlayerPrefs.SetFloat("Grade",mGrade)  


下面用一个实例来说明具体的用法:

public class Playerprefs : MonoBehaviour
{
    public string set_NAME;
    public string get_NAME;

    void OnGUI()
    {
        GUILayout.BeginHorizontal("box");
        GUILayout.Label("姓名:");
        set_NAME = GUILayout.TextArea(set_NAME, 200, GUILayout.Width(50));
        if (GUILayout.Button("存储数据"))
        {

            //将我们输入的姓名保存到本地,命名为_NAME ;
            PlayerPrefs.SetString("_NAME", set_NAME);
        }
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal("box");
        if (GUILayout.Button("读取数据"))
        {

            //读取本地数据中名称为_NAME 的数据;
            get_NAME = PlayerPrefs.GetString("_NAME");
        }
        GUILayout.Label("你输入的姓名:" + get_NAME);
        GUILayout.EndHorizontal();
    }
}


### 回答1: 在Unity游戏存档可以使用PlayerPrefs来实现。PlayerPrefs可以在本地存储少量的键值对数据,并在游戏重新启动后保留数据。 下面是一个示例代码,该代码将游戏分数存储到PlayerPrefs中: ```c# int score = 100; PlayerPrefs.SetInt("Score", score); PlayerPrefs.Save(); ``` 在游戏重新启动后,可以使用以下代码获取存储的分数: ```c# int savedScore = PlayerPrefs.GetInt("Score"); ``` 注意,PlayerPrefs存储的数据只能是 int, float, 和 string 型。如果需要存储更复杂的数据型,可以使用 JsonUtility 将对象序列化为字符串并存储。 ### 回答2: Unity游戏存档C#脚本可以包括以下几个关键功能: 1. 存档功能:通过点击游戏中的保存按钮或触发特定事件时,将游戏的当前状态保存到一个文件中。这可以通过使用C#中的StreamWriter来实现。首先,可以创建一个FileStream对象来打开一个文件,再将其传递给StreamWriter对象以便写入数据。然后,可以使用StreamWriter的WriteLine或Write方法将游戏中的数据写入文件中。 2. 读档功能:当玩家需要恢复到之前保存的进度时,可以通过读取存档文件来实现。可以使用C#中的StreamReader来读取存档文件中的数据。首先,可以使用FileStream对象打开存档文件,然后将其传递给StreamReader对象以便读取数据。可以使用StreamReader的ReadLine或Read方法来读取文件中的数据,并将其加载到游戏中的相应变量中。 3. 存档路径管理:为了确保游戏存档的可靠性和方便性,可以在脚本中添加相关的路径管理功能。可以使用C#中的Path来构建相对或绝对路径,以便在不同平台上找到正确的存档位置。 4. 存档管理:管理多个存档的功能可以通过使用C#中的序列化和反序列化来实现。可以将游戏状态序列化为一个二进制或JSON文件,并将其保存在特定的存档文件夹中。当需要读取某个存档时,可以将存档文件反序列化为游戏状态,并进行加载。 5. 存档删除:提供删除存档的功能,以便玩家可以在不再需要某个存档时将其删除。可以使用C#中的File来删除存档文件。 总之,通过编写以上功能的C#脚本,可以实现Unity游戏存档和读档功能,使玩家可以在游戏中保存进度,并在需要时恢复到之前的状态。 ### 回答3: 存档是一个游戏中非常重要的功能,它使玩家能够在游戏中保存进展并在以后继续游戏。下面是一个使用C#编写的Unity游戏存档的示例代码: ```csharp using System.IO; using System.Runtime.Serialization.Formatters.Binary; using UnityEngine; public static class SaveManager { public static void SaveGame(GameData data) { BinaryFormatter formatter = new BinaryFormatter(); string filePath = Application.persistentDataPath + "/saveData.dat"; FileStream fileStream = new FileStream(filePath, FileMode.Create); formatter.Serialize(fileStream, data); fileStream.Close(); } public static GameData LoadGame() { string filePath = Application.persistentDataPath + "/saveData.dat"; if (File.Exists(filePath)) { BinaryFormatter formatter = new BinaryFormatter(); FileStream fileStream = new FileStream(filePath, FileMode.Open); GameData data = formatter.Deserialize(fileStream) as GameData; fileStream.Close(); return data; } else { Debug.LogError("Save file not found."); return null; } } public static bool DeleteSave() { string filePath = Application.persistentDataPath + "/saveData.dat"; if (File.Exists(filePath)) { File.Delete(filePath); return true; } else { Debug.LogError("Save file not found."); return false; } } } ``` 上述代码中,我们创建了一个`SaveManager`,其中包含了三个主要函数。`SaveGame`函数用于将游戏数据对象进行序列化,并将其保存到名为"saveData.dat"的二进制文件中。`LoadGame`函数用于从文件中读取并反序列化保存的游戏数据,并将其返回给调用者。`DeleteSave`函数用于删除保存文件。 为了实现这些功能,我们使用了`BinaryFormatter`来进行序列化和反序列化操作,并使用`FileStream`来创建、打开和关闭文件。我们还使用了`Application.persistentDataPath`来确定保存文件的路径,该路径是在多个平台上都可靠的,并且不会在应用程序更新时被清除。 通过使用这些功能,开发者可以很容易地实现Unity游戏存档功能,并提供给玩家更好的游戏体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值