SerializationException:serializationStream支持查找,但其长度为0

时间:2019-04-08 07:17:59

标签: c# unity3d serialization

我正在使用以下类在Unity游戏的Android版本中的persistentDataPath上保存和加载用户数据。 该课程非常适合我们在游戏中进行的测试,但是在我们的Google Analytics(分析)工具中,我看到一些用户(50K左右为5K)存在一个问题,即一段时间后进入游戏时,其数据已被重置,在Load()函数中提到的错误。

public class GameData : MonoBehaviour
{
    private static Data _playerData;

    public static Data GetPlayerDataInstance()
    {
        Load();
        return _playerData;
    }
    public static void Save(Data data)
    {
        BinaryFormatter binaryFormatter = new BinaryFormatter();
        FileStream file = File.Create(Application.persistentDataPath + "/playerInfo.dat");

        _playerData = data;

        try
        {
            binaryFormatter.Serialize(file, data);
        }
        catch (SerializationException e)
        {
            Console.WriteLine("Failed to serialize. Reason: " + e.Message);
            throw;
        }
        finally
        {
            file.Close();
        }
    }

    public static void Drop()
    {
        if (File.Exists(Application.persistentDataPath + "/playerInfo.dat"))
        {
            print("Drop GameData");
            File.Delete(Application.persistentDataPath + "/playerInfo.dat");

            _playerData = null;

        }
        else
        {
            _playerData = null;
        }
    }

    public static void Load()
    {
        if (File.Exists(Application.persistentDataPath + "/playerInfo.dat"))
        {
            BinaryFormatter binaryFormatter = new BinaryFormatter();
            FileStream file = File.Open(Application.persistentDataPath + "/playerInfo.dat", FileMode.Open);

            Data data = null;

            try
            {
                data = (Data) binaryFormatter.Deserialize(file);
            }
            catch (SerializationException e)
            {
                 Console.WriteLine("Failed to deserialize. Reason: " + e.Message);                
                 throw;             
            }
            finally
            {                
                file.Close();
            }

            _playerData = data;
        }
        else
        {
            _playerData = null;
        }
    }
}

当我阅读有关此错误的主题时,问题几乎是所保存的文件(即playerInfo.dat)为空。 仅对某些用户怎么可能?有解决此问题的解决方案吗?

更新: 这就是数据类实现的外观:

[Serializable]
public class Data
{
    public string PlayerId { get; set; }
    public string AccessToken { get; set; }

    public bool PlayMusic { get; set; }
    public bool PlaySoundEffect { get; set; }
    public bool PlayVibration { get; set; }
    public bool FinishedTutorial { get; set; }
    public bool SkippedTutorial { get; set; }
    public bool MainSceneTutorial { get; set; }
    public bool FinishedHitOpponentMohrehTutorial { get; set; }
    public bool FinishedHitByOpponentTutorial { get; set; }
    public bool FinishedEndGameExactPlaceTutorial { get; set; }
    public bool FinishedEndGameGreaterPlaceTutorial { get; set; }
    public bool FinishedEndGameLessPlaceTutorial { get; set; }
    public bool FinishedUndoButtonTutorial { get; set; }
    public bool FinishedDoubleButtonTutorial { get; set; }
    public bool FinishedDragTutorial { get; set; }
    public bool IncomingMohrehBlockedByOpponent { get; set; }
    public int ClientStickerId { get; set; }
    public int PlayCount;
    public bool FinishedTurnTimerTutorial { get; set; }

    public bool ChangedNameForEnterLeaderboard { get; set; }
    public bool LeaderboardUnlocked { get; set; }

    public PurchaseToken LastPurchaseToken { get; set; }
    public PurchaseToken LastSpinnerPurchaseToken { get; set; }

    public Data(string playerId, string accessToken)
    {
        PlayerId = playerId;
        AccessToken = accessToken;
        PlayMusic = true;
        PlaySoundEffect = true;
        PlayVibration = true;
        FinishedTutorial = false;
        SkippedTutorial = false;
        MainSceneTutorial = false;
        ClientStickerId = 0;
        LastPurchaseToken = null;
        FinishedHitOpponentMohrehTutorial = false;
        FinishedHitByOpponentTutorial = false;
        FinishedEndGameExactPlaceTutorial = false;
        FinishedEndGameGreaterPlaceTutorial = false;
        FinishedEndGameLessPlaceTutorial = false;
        IncomingMohrehBlockedByOpponent = false;
        FinishedUndoButtonTutorial = false;
        FinishedDoubleButtonTutorial = false;
        FinishedDragTutorial = false;

        ChangedNameForEnterLeaderboard = false;
        LeaderboardUnlocked = false;

        PlayCount = 1;
        FinishedTurnTimerTutorial = false;
    }
}

1 个答案:

答案 0 :(得分:1)

不要使用属性! ...仅字段可序列化!

之后

binaryFormatter.Serialize(file, data);

您将始终获得一个空文件。


更改您的班级以改用字段

[Serializable]
public class Data
{
    public string PlayerId;
    public string AccessToken;

    public bool PlayMusic;
    public bool PlaySoundEffect;
    public bool PlayVibration;
    public bool FinishedTutorial;
    public bool SkippedTutorial;
    public bool MainSceneTutorial;
    public bool FinishedHitOpponentMohrehTutorial;
    public bool FinishedHitByOpponentTutorial;
    public bool FinishedEndGameExactPlaceTutorial;
    public bool FinishedEndGameGreaterPlaceTutoria;
    public bool FinishedEndGameLessPlaceTutorial;
    public bool FinishedUndoButtonTutorial;
    public bool FinishedDoubleButtonTutorial;
    public bool FinishedDragTutorial;
    public bool IncomingMohrehBlockedByOpponent;
    public int ClientStickerId;
    public int PlayCount;
    public bool FinishedTurnTimerTutorial;

    public bool ChangedNameForEnterLeaderboard;
    public bool LeaderboardUnlocked;

    public PurchaseToken LastPurchaseToken;
    public PurchaseToken LastSpinnerPurchaseToken;

    ...
}

其中PurchaseToken也应该是Serializable类型。