在 Android 和 iOS 的不同版本中,在 Unity3D 制作的游戏中保存游戏进度的最佳做法是什么
What is best Practice to save Game Progress in a Game made in Unity3D along different Versions for Android and iOS
为简单的 2D 游戏保存游戏进度的最佳做法是什么?对于“进度”,我指的是非常简单的值,例如“收集的物品数量”(int)、“高分”(int)、“货币数量”(float) 和“头像是否解锁”(bool)。没有世界地图状态或任何大的东西可以保存。我使用了 playerprefs 但这是为偏好而设计的,许多专家认为应该避免。这就是为什么我现在想改变它。
我阅读了这个帖子:Whosebug Question,但我无法确定适合我的案例的最佳解决方案,因为没有对建议的方法进行比较。
我的要求很简单:
- 适用于iOS和Android
- 发布新版本应用程序时未被删除
- 易于在脚本中使用,因为我们在很多地方都引用了变量
有什么建议吗?
就我个人而言,我喜欢直接写入文件。它适用于所有平台,不会在应用程序更新时被删除,并且您可以完全控制访问数据的方式。
轻松添加、删除、编辑内容,轻松处理多种保存状态。
你可以用一个简单的 class 来完成,比如这个:
public static class FileUtils
{
public static void WriteToFile(string fileName, string json)
{
var path = GetFilePath(fileName);
var stream = new FileStream(path, FileMode.Create);
using (var writer = new StreamWriter(stream))
writer.Write(json);
}
public static string ReadFromFile(string fileName)
{
var path = GetFilePath(fileName);
if (File.Exists(path))
{
using (var reader = new StreamReader(path))
return reader.ReadToEnd();
}
Debug.LogWarning($"{fileName} not found.");
return null;
}
public static string GetFilePath(string fileName)
{
return Path.Combine(Application.persistentDataPath, fileName);
}
}
然后,你只需要一个 Serializable class 或 struct 来保存,这里我使用 JSON :
[Serializable]
public class UserData
{
public string userName;
public string profilePictureRef;
public UserData(string userName, string profilePictureRef)
{
this.userName = userName;
this.profilePictureRef = profilePictureRef;
}
}
public void SaveUserData(UserData data)
{
var jsonData = JsonUtility.ToJson(data);
FileUtils.WriteToFile(_fileName, jsonData);
}
public void LoadUserData()
{
var jsonData = FileUtils.ReadFromFile(_fileName);
_data = JsonUtility.FromJson<UserData>(jsonData);
}
您甚至可以从服务器获取一些 JSON 并将其保存在本地。
也许您会想要“模糊”内容,以便玩家无法手动编辑它们。
为简单的 2D 游戏保存游戏进度的最佳做法是什么?对于“进度”,我指的是非常简单的值,例如“收集的物品数量”(int)、“高分”(int)、“货币数量”(float) 和“头像是否解锁”(bool)。没有世界地图状态或任何大的东西可以保存。我使用了 playerprefs 但这是为偏好而设计的,许多专家认为应该避免。这就是为什么我现在想改变它。
我阅读了这个帖子:Whosebug Question,但我无法确定适合我的案例的最佳解决方案,因为没有对建议的方法进行比较。
我的要求很简单:
- 适用于iOS和Android
- 发布新版本应用程序时未被删除
- 易于在脚本中使用,因为我们在很多地方都引用了变量
有什么建议吗?
就我个人而言,我喜欢直接写入文件。它适用于所有平台,不会在应用程序更新时被删除,并且您可以完全控制访问数据的方式。 轻松添加、删除、编辑内容,轻松处理多种保存状态。
你可以用一个简单的 class 来完成,比如这个:
public static class FileUtils
{
public static void WriteToFile(string fileName, string json)
{
var path = GetFilePath(fileName);
var stream = new FileStream(path, FileMode.Create);
using (var writer = new StreamWriter(stream))
writer.Write(json);
}
public static string ReadFromFile(string fileName)
{
var path = GetFilePath(fileName);
if (File.Exists(path))
{
using (var reader = new StreamReader(path))
return reader.ReadToEnd();
}
Debug.LogWarning($"{fileName} not found.");
return null;
}
public static string GetFilePath(string fileName)
{
return Path.Combine(Application.persistentDataPath, fileName);
}
}
然后,你只需要一个 Serializable class 或 struct 来保存,这里我使用 JSON :
[Serializable]
public class UserData
{
public string userName;
public string profilePictureRef;
public UserData(string userName, string profilePictureRef)
{
this.userName = userName;
this.profilePictureRef = profilePictureRef;
}
}
public void SaveUserData(UserData data)
{
var jsonData = JsonUtility.ToJson(data);
FileUtils.WriteToFile(_fileName, jsonData);
}
public void LoadUserData()
{
var jsonData = FileUtils.ReadFromFile(_fileName);
_data = JsonUtility.FromJson<UserData>(jsonData);
}
您甚至可以从服务器获取一些 JSON 并将其保存在本地。 也许您会想要“模糊”内容,以便玩家无法手动编辑它们。