C# Unity 3d持久数据,保存文件不';我没能如期而至

C# Unity 3d持久数据,保存文件不';我没能如期而至,c#,unity3d,persistence,saving-data,C#,Unity3d,Persistence,Saving Data,我最近从团结开始。目前,当涉及到数据持久性时,我陷入了困境。虽然我可以创建一个文件,但是它不包含序列化变量。我已经阅读并看过各种教程。不幸的是,我仍然没有取得成功。如果有人能帮助我,我将非常感激 这是我的SaveLoad.cs我可以不用太多编辑就可以使用: using UnityEngine; using System.Collections; using System.Collections.Generic; using System.Runtime.Serialization.Formatt

我最近从团结开始。目前,当涉及到数据持久性时,我陷入了困境。虽然我可以创建一个文件,但是它不包含序列化变量。我已经阅读并看过各种教程。不幸的是,我仍然没有取得成功。如果有人能帮助我,我将非常感激

这是我的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();
        }
    }
}

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

实现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与此代码一起使用-无论您喜欢哪一种,Unity都不能序列化
static
vars;见和。也许可以尝试将<代码>设置为当前成为<代码>公共游戏?首先,感谢您提供的链接。但是,当我在任何地方删除“static”时,都会出现一些错误:“访问非静态成员‘SaveLoad.Save()’需要对象引用”。我想这是因为类“File”已经是静态的。您也可以使用
XMLSerializer
来代替
BinaryFormatter
。这样你就可以阅读savegame了。当您看到保存的内容并创建自定义的保存游戏时,这使调试方式更加容易。
   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;
   }