【问题标题】:saving a class data as a text file将类数据保存为文本文件
【发布时间】:2020-06-06 07:33:15
【问题描述】:

我正在使用 binaryFormatter 加密数据,但我想在不加密的情况下保存它。 所以下次我只是施放它。 这是我正在使用的代码。

   //Creat a binaryFormatter
    BinaryFormatter formatter = new BinaryFormatter();

    //Direction, Filename and Extention
    string path = Application.persistentDataPath + "/enem.sav";

    //Creat the File (Blank)
    FileStream stream = new FileStream(path, FileMode.Create);

    //Get the Data
    EnemData data = new EnemData();

    //Enter the Data and Encrypt it
    formatter.Serialize(stream, data);
    stream.Close();

【问题讨论】:

  • BinaryFormatter 不会加密您的数据,它只是将您的对象转换为二进制表示。你能指定你想要达到的目标吗? - 因为我并没有真正得到你想要的
  • 我只是不想将我的数据转换为二进制,我很难

标签: c# visual-studio unity3d io


【解决方案1】:

您可以使用JsonUtility.ToJson 将对象的数据转换为 JSON 格式。

public class PlayerState : MonoBehaviour
{
    public string playerName;
    public int lives;
    public float health;

    public string SaveToString()
    {
        return JsonUtility.ToJson(this);
    }

    // Given:
    // playerName = "Dr Charles"
    // lives = 3
    // health = 0.8f
    // SaveToString returns:
    // {"playerName":"Dr Charles","lives":3,"health":0.8}
}

Here 你可以找到如何在文件中读取和写入字符串。

// Create a file to write to.
string createText = "Hello and Welcome" + Environment.NewLine;
File.WriteAllText(path, createText);

// Open the file to read from.
string readText = File.ReadAllText(path);

要加载数据,只需使用JsonUtility.FromJson 或JsonUtility.FromJsonOverwrite。

例子:

public class PlayerState : MonoBehaviour
{
    public string playerName;
    public int lives;
    public float health;
    public string path = "your file path";

    public string SaveToString()
    {
        File.WriteAllText(path, JsonUtility.ToJson(this));
    }

    public static PlayerState Load(string path)
    {
        return JsonUtility.FromJson<PlayerState>(File.ReadAllText(path));
    }
}

【讨论】:

    【解决方案2】:

    不确定您需要什么。如果您只想序列化(将您的类转换为二进制表示并保存到文件中)然后将其取回,这应该适用于您:

        //Create a binaryFormatter
        BinaryFormatter formatter = new BinaryFormatter();
    
        //Direction, Filename and Extention
        string path = Application.persistentDataPath + "/enem.sav";
    
        //Creat the File (Blank)
        Stream stream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None);
    
        //Get the Data
        EnemData data = new EnemData { Id = 123, SomeBool = true, Name = "Enem" };
    
        //Enter the Data and Encrypt it
        formatter.Serialize(stream, data);
        stream.Close();
    
    
        //Restore it
    
        Stream stream2 = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
        EnemData restoredClass = (EnemData)formatter.Deserialize(stream2);
        stream.Close();
    

    但请记住,您必须将您的类标记为可序列化:

    [Serializable]
    public class EnemData
    {
        public int Id { get; set; }
        public bool SomeBool { get; set; }
        public string Name { get; set; }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-10
      • 1970-01-01
      • 1970-01-01
      • 2013-12-24
      • 2014-02-18
      相关资源
      最近更新 更多