【问题标题】:Unity 3d Persistent data, Save file doesn't come out as hopedUnity 3d 持久数据,保存文件未按预期输出
【发布时间】:2015-05-26 21:50:24
【问题描述】:

我最近开始使用 Unity。目前,我在数据持久性方面陷入困境。虽然我可以创建一个文件,但它不包含序列化变量。我已经阅读并看过各种教程。不幸的是,我仍然没有领先。如果有人可以帮助我,我将不胜感激。

这是我的 SaveLoad.cs,我无需进行太多编辑即可使用:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

public static class SaveLoad {
    public static List<Game> savedGames = new List<Game>();

    public static void Save() {
        savedGames.Add(Game.current);
        BinaryFormatter bf = new BinaryFormatter();
        FileStream file = File.Create (Application.persistentDataPath + "/savedGames.gd");
        bf.Serialize(file, SaveLoad.savedGames);
        file.Close();
    }
    public static void Load() {
        if(File.Exists(Application.persistentDataPath + "/savedGames.gd")) {
            BinaryFormatter bf = new BinaryFormatter();
            FileStream file = File.Open(Application.persistentDataPath + "/savedGames.gd", FileMode.Open);
            SaveLoad.savedGames = (List<Game>)bf.Deserialize(file);
            file.Close();
        }
    }
}

这是我的 game.cs 与(我希望)我尝试保存的序列化变量:

using UnityEngine;
using System.Collections;

[System.Serializable]
public class Game { 

    public static Game current;
    public Click CurrentEuros { get; set; }

    public Game () {
        //CurrentEuros = 1;
        CurrentEuros = new Click();
    }
}

结果总是在 savedGames.gd “游戏”的最后一行。

【问题讨论】:

  • Unity 无法序列化static vars;见herehere。也许尝试将current 设为public Game
  • 首先,感谢您的链接。但是,当我只需要在任何地方擦除“静态”时,就会出现一些错误“访问非静态成员‘SaveLoad.Save()’需要对象引用”。我猜这是因为“文件”类已经是静态的。
  • 您也可以使用XMLSerializer,而不是BinaryFormatter。这样你就可以阅读存档了。当您查看保存的内容并创建自定义保存游戏时,这使调试变得更加容易。

标签: c# unity3d persistence saving-data


【解决方案1】:

实现ISerializable接口:

using System;
using System.IO;
using System.Runtime.Serialization;

[Serializable]
public class PlayerProfile : ISerializable
{
   private string _profileName;
   private string _saveGameFolder;

   public PlayerProfile()
   {
   }

   public PlayerProfile(SerializationInfo info, StreamingContext context)
   {
      _profileName = (string)info.GetValue( "ProfileName", typeof( string ) );
      _saveGameFolder = (string)info.GetValue( "SaveGameFolder", typeof( string ) );
   }

   public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
   {
      info.AddValue( "ProfileName", _profileName, typeof( string ) );
      info.AddValue( "SaveGameFolder", _saveGameFolder, typeof( string ) );
   }
}

像这样使用它:

   using UnityEngine;
   using System;
   using System.IO;
   using System.Reflection;
   using System.Runtime.Serialization.Formatters.Binary;
   using System.Runtime.Serialization.Formatters.Soap;
   using System.Runtime.Serialization;

   PlayerProfile _playerProfile;

   public void SaveProfile()
   {
      //IFormatter formatter = new BinaryFormatter();
      IFormatter formatter = new SoapFormatter();

      string fileName = "my.profile";

      FileStream stream = new FileStream( fileName, FileMode.Create );
      formatter.Serialize( stream, _playerProfile );
      stream.Close();
   }

   public bool LoadProfile()
   {
      //IFormatter formatter = new BinaryFormatter();
      IFormatter formatter = new SoapFormatter();

      string fileName = "my.profile";

      if( !File.Exists( fileName ) )
         return false;

      FileStream stream = new FileStream( fileName, FileMode.Open );
      _playerProfile = formatter.Deserialize( stream ) as PlayerProfile;
      stream.Close();

      return true;
   }

这是经过测试并且 100% 有效

您可以在此代码中使用 BinaryFormatter 或 SoapFormatter - 您喜欢哪一种

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-12
    • 1970-01-01
    • 2014-10-07
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多