栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

保存游戏状态的最佳方法是什么?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

保存游戏状态的最佳方法是什么?

但是我听说这种方式存在一些问题,不适合保存。

那就对了。在某些设备上,存在问题

BinaryFormatter
。当您更新或更改类时,情况会变得更糟。由于课程不再匹配,您的旧设置可能会丢失。有时,由于这个原因,读取保存的数据时会出现异常。

另外,在iOS上,您必须添加,

Environment.SetEnvironmentVariable("MONO_REFLECTION_SERIALIZER","yes");
否则会遇到问题
BinaryFormatter

最好的保存方式是

PlayerPrefs
Json
。您可以在此处了解如何进行操作。

就我而言,保存格式必须为字节数组

在这种情况下,您可以将其转换为json,然后将json转换

string
byte
array。然后
File.WriteAllBytes
,您可以使用和
File.ReadAllBytes
保存并读取字节数组。

这是一个通用类,可用于保存数据。几乎与此相同,但
使用

PlayerPrefs
。它使用文件来保存json数据。

DataSaver
类:

public class DataSaver{    //Save Data    public static void saveData<T>(T dataToSave, string dataFileName)    {        string tempPath = Path.Combine(Application.persistentDataPath, "data");        tempPath = Path.Combine(tempPath, dataFileName + ".txt");        //Convert To Json then to bytes        string jsonData = JsonUtility.ToJson(dataToSave, true);        byte[] jsonByte = Encoding.ASCII.GetBytes(jsonData);        //Create Directory if it does not exist        if (!Directory.Exists(Path.GetDirectoryName(tempPath)))        { Directory.CreateDirectory(Path.GetDirectoryName(tempPath));        }        //Debug.Log(path);        try        { File.WriteAllBytes(tempPath, jsonByte); Debug.Log("Saved Data to: " + tempPath.Replace("/", "\"));        }        catch (Exception e)        { Debug.LogWarning("Failed To PlayerInfo Data to: " + tempPath.Replace("/", "\")); Debug.LogWarning("Error: " + e.Message);        }    }    //Load Data    public static T loadData<T>(string dataFileName)    {        string tempPath = Path.Combine(Application.persistentDataPath, "data");        tempPath = Path.Combine(tempPath, dataFileName + ".txt");        //Exit if Directory or File does not exist        if (!Directory.Exists(Path.GetDirectoryName(tempPath)))        { Debug.LogWarning("Directory does not exist"); return default(T);        }        if (!File.Exists(tempPath))        { Debug.Log("File does not exist"); return default(T);        }        //Load saved Json        byte[] jsonByte = null;        try        { jsonByte = File.ReadAllBytes(tempPath); Debug.Log("Loaded Data from: " + tempPath.Replace("/", "\"));        }        catch (Exception e)        { Debug.LogWarning("Failed To Load Data from: " + tempPath.Replace("/", "\")); Debug.LogWarning("Error: " + e.Message);        }        //Convert to json string        string jsonData = Encoding.ASCII.GetString(jsonByte);        //Convert to Object        object resultValue = JsonUtility.FromJson<T>(jsonData);        return (T)Convert.ChangeType(resultValue, typeof(T));    }    public static bool deleteData(string dataFileName)    {        bool success = false;        //Load Data        string tempPath = Path.Combine(Application.persistentDataPath, "data");        tempPath = Path.Combine(tempPath, dataFileName + ".txt");        //Exit if Directory or File does not exist        if (!Directory.Exists(Path.GetDirectoryName(tempPath)))        { Debug.LogWarning("Directory does not exist"); return false;        }        if (!File.Exists(tempPath))        { Debug.Log("File does not exist"); return false;        }        try        { File.Delete(tempPath); Debug.Log("Data deleted from: " + tempPath.Replace("/", "\")); success = true;        }        catch (Exception e)        { Debug.LogWarning("Failed To Delete data: " + e.Message);        }        return success;    }}

用法

要保存的示例类

[Serializable]public class PlayerInfo{    public List<int> ID = new List<int>();    public List<int> Amounts = new List<int>();    public int life = 0;    public float highScore = 0;}

保存数据:

PlayerInfo saveData = new PlayerInfo();saveData.life = 99;saveData.highScore = 40;//Save data from PlayerInfo to a file named playersDataSaver.saveData(saveData, "players");

加载数据:

PlayerInfo loadedData = DataSaver.loadData<PlayerInfo>("players");if (loadedData == null){    return;}//Display loaded DataDebug.Log("Life: " + loadedData.life);Debug.Log("High Score: " + loadedData.highScore);for (int i = 0; i < loadedData.ID.Count; i++){    Debug.Log("ID: " + loadedData.ID[i]);}for (int i = 0; i < loadedData.Amounts.Count; i++){    Debug.Log("Amounts: " + loadedData.Amounts[i]);}

删除数据:

DataSaver.deleteData("players");


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/391680.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号