c# - Unity 3d 持久数据,保存文件没有按预期出现

标签 c# unity3d persistence saving-data

我最近开始使用 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“游戏”的最后一行。

最佳答案

实现 ISerializable 接口(interface):

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 与此代码一起使用 - 无论您喜欢哪种

关于c# - Unity 3d 持久数据,保存文件没有按预期出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30469560/

相关文章:

database - 如何使用带外键的 Slick 映射表?

java - jpa 查询似乎没有正确解析枚举值并且总是返回空列表

c# - 在c#中运行cmd命令

c# - 读取鼠标拖动的项目

c# - Unity3D:让玩家使用 ParticleSystem 和 OnCollisionEnter 点亮手电筒?

unity3d - 如何在运行时加载平台特定字体

ios - 每次重建应用程序或安装新版本时,FileURL 都会无效

c# - 与很少使用的值类型初始化混淆

C#:将字符串插入另一个字符串 - 性能问题

java - 将 Unity3D 项目导出并运行到 Android Studio