Unity 3d Persistent数据,保存文件不符合希望

时间:2015-05-26 21:50:25

标签: 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“Game”的最后一行。

1 个答案:

答案 0 :(得分:0)

实现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与此代码一起使用 - 您喜欢