unity游戏存档playerprefs

unity中有一个简单的游戏存档类–playerprefs,这个类有什么作用呢?
PlayerPrefs是Unity3d提供了一个用于数据本地持久化保存与读取的类。工作原理十分简单,就是以key-value的形式将数据保存在本地,然后在代码中可以写入、读取、更新数据。它可以用来储存一些非关键性的数据,尤其是一些没有服务器的单机游戏中,游戏存档、分数排名等都需要用到数据存储,这个时候可以使用playerprefs轻松实现数据存储
需要知道的是,这个类存储的数据是以键值对的形式进行,可以简单的看成是一个字典,同时他也是通过键名来进行读取,当键不存在的时候,返回默认值

对应函数:

#region 程序集 UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// D:\unity2018.2.3\install\Editor\Data\Managed\UnityEngine\UnityEngine.CoreModule.dll
#endregion

using UnityEngine.Bindings;

namespace UnityEngine
{
    //
    // 摘要:
    //     Stores and accesses player preferences between game sessions.
    [NativeHeader("Runtime/Utilities/PlayerPrefs.h")]
    public class PlayerPrefs
    {
        public PlayerPrefs();

        //
        // 摘要:
        //     Removes all keys and values from the preferences. Use with caution.
        [NativeMethod("DeleteAllWithCallback")]
        public static void DeleteAll();
        //
        // 摘要:
        //     Removes key and its corresponding value from the preferences.
        //
        // 参数:
        //   key:
        public static void DeleteKey(string key);
        //
        // 摘要:
        //     Returns the value corresponding to key in the preference file if it exists.
        //
        // 参数:
        //   key:
        //
        //   defaultValue:
        public static float GetFloat(string key, float defaultValue);
        //
        // 摘要:
        //     Returns the value corresponding to key in the preference file if it exists.
        //
        // 参数:
        //   key:
        //
        //   defaultValue:
        public static float GetFloat(string key);
        //
        // 摘要:
        //     Returns the value corresponding to key in the preference file if it exists.
        //
        // 参数:
        //   key:
        //
        //   defaultValue:
        public static int GetInt(string key, int defaultValue);
        //
        // 摘要:
        //     Returns the value corresponding to key in the preference file if it exists.
        //
        // 参数:
        //   key:
        //
        //   defaultValue:
        public static int GetInt(string key);
        //
        // 摘要:
        //     Returns the value corresponding to key in the preference file if it exists.
        //
        // 参数:
        //   key:
        //
        //   defaultValue:
        public static string GetString(string key, string defaultValue);
        //
        // 摘要:
        //     Returns the value corresponding to key in the preference file if it exists.
        //
        // 参数:
        //   key:
        //
        //   defaultValue:
        public static string GetString(string key);
        //
        // 摘要:
        //     Returns true if key exists in the preferences.
        //
        // 参数:
        //   key:
        public static bool HasKey(string key);
        //
        // 摘要:
        //     Writes all modified preferences to disk.
        [NativeMethod("Sync")]
        public static void Save();
        //
        // 摘要:
        //     Sets the value of the preference identified by key.
        //
        // 参数:
        //   key:
        //
        //   value:
        public static void SetFloat(string key, float value);
        //
        // 摘要:
        //     Sets the value of the preference identified by key.
        //
        // 参数:
        //   key:
        //
        //   value:
        public static void SetInt(string key, int value);
        //
        // 摘要:
        //     Sets the value of the preference identified by key.
        //
        // 参数:
        //   key:
        //
        //   value:
        public static void SetString(string key, string value);
    }
}

函数作用就是英文释义。

最后一个事情,这个类保存的数据存在哪里,这里简单的说几个平台
1.MacOS平台下,储存在~/Library/Preferences文件夹,名为unity.[company name].[product name].plist。
2.在windows平台,存储在注册表的 HKEY_CURRENT_USER\Software[company name][product name]键下。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答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、付费专栏及课程。

余额充值