[RÈSOLU][C#] problème de sauvegarde : SerializationException

Questions à propos du scripting. Hors Shader, GUI, Audio et Mobile.
Bilu
Messages : 91
Inscription : 20 Nov 2019 09:24

[RÈSOLU][C#] problème de sauvegarde : SerializationException

Message par Bilu » 21 Oct 2021 10:28

Bien le bonjour à tous,

Je fais face à un soucis et mal gré mes recherches sur le net, je n'ai pas trouvé de solution. Plusieurs personnes ont eu ce soucis mais ce qui a marché pour eux n'a pas marché pour moi.

Je fais face à "SerializationException".
Le message d'erreur complet :
"SerializationException: Type 'BuyShip2Button' in Assembly 'Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.
System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers (System.RuntimeType type) (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.Runtime.Serialization.FormatterServices+<>c__DisplayClass9_0.<GetSerializableMembers>b__0 (System.Runtime.Serialization.MemberHolder _) (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.Collections.Concurrent.ConcurrentDictionary`2[TKey,TValue].GetOrAdd (TKey key, System.Func`2[T,TResult] valueFactory) (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.Runtime.Serialization.FormatterServices.GetSerializableMembers (System.Type type, System.Runtime.Serialization.StreamingContext context) (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo () (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize (System.Object obj, System.Runtime.Serialization.ISurrogateSelector surrogateSelector, System.Runtime.Serialization.StreamingContext context, System.Runtime.Serialization.Formatters.Binary.SerObjectInfoInit serObjectInfoInit, System.Runtime.Serialization.IFormatterConverter converter, System.Runtime.Serialization.Formatters.Binary.ObjectWriter objectWriter, System.Runtime.Serialization.SerializationBinder binder) (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize (System.Object obj, System.Runtime.Serialization.ISurrogateSelector surrogateSelector, System.Runtime.Serialization.StreamingContext context, System.Runtime.Serialization.Formatters.Binary.SerObjectInfoInit serObjectInfoInit, System.Runtime.Serialization.IFormatterConverter converter, System.Runtime.Serialization.Formatters.Binary.ObjectWriter objectWriter, System.Runtime.Serialization.SerializationBinder binder) (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize (System.Object graph, System.Runtime.Remoting.Messaging.Header[] inHeaders, System.Runtime.Serialization.Formatters.Binary.__BinaryWriter serWriter, System.Boolean fCheck) (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize (System.IO.Stream serializationStream, System.Object graph, System.Runtime.Remoting.Messaging.Header[] headers, System.Boolean fCheck) (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize (System.IO.Stream serializationStream, System.Object graph, System.Runtime.Remoting.Messaging.Header[] headers) (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize (System.IO.Stream serializationStream, System.Object graph) (at <695d1cc93cca45069c528c15c9fdd749>:0)
SaveSystem.SavePlayerpaidDisplayPost2 (BuyShip2Button paidDisplayPost2) (at Assets/XXXX/SaveSystem.cs:174)
BuyShip2Button.Start () (at Assets/XXXX/BuyShip2Button.cs:51)"
Voici le code du script SaveSystem :

Code : Tout sélectionner

public static void SavePlayerpaidDisplayPost2(BuyShip2Button paidDisplayPost2)
    {
        BinaryFormatter formatter5 = new BinaryFormatter();
        string path5 = Application.persistentDataPath + "/paidDisplayPost2.fun";
        FileStream stream5 = new FileStream(path5, FileMode.Create);

        PlayerpaidDisplayPost2 datapaidDisplayPost2 = new PlayerpaidDisplayPost2(paidDisplayPost2);

        Debug.Log("sauvegarde paid2");


        formatter5.Serialize(stream5, paidDisplayPost2);
        stream5.Close();
    }

    public static PlayerpaidDisplayPost2 LoadPlayerpaidDisplayPost2()
    {
        string path5 = Application.persistentDataPath + "/paidDisplayPost2.fun";
        if (File.Exists(path5))
        {
            BinaryFormatter formatter5 = new BinaryFormatter();
            FileStream stream5 = new FileStream(path5, FileMode.Open);


            Debug.Log("load paid2");
            PlayerpaidDisplayPost2 datapaidDisplayPost2 = formatter5.Deserialize(stream5) as PlayerpaidDisplayPost2;
            stream5.Close();


            return datapaidDisplayPost2;
        }
        else
        {
            Debug.LogError("Save file not found in" + path5);
            return null;
        }
    }
La ligne 174 est "formatter5.Serialize(stream5, paidDisplayPost2);"

Le script BuyShip2Button :

Code : Tout sélectionner


void Start()
    {
        
        
        SaveSystem.SavePlayerpaidDisplayPost2(this);

       
       
       
    }

    // Update is called once per frame
    void Update()
    {
        LoadPlayerpaidDisplayPost2();
    }

La ligne 51 est "SaveSystem.SavePlayerpaidDisplayPost2(this);"

Ce qui est le plus étrange, c'est que j'utilise exactement la même méthode pour d'autres données sauvegardées et je n'ai pas ce soucis. Ca sauvegarde et pas de soucis de sézarilisation/dé-sézarilisation.
J'ai supprimé plusieurs fois le fichier dans le lequel se sauvegardait "paidDisplayPost2" en pensant que le fichier etait corrompu mais le problème persiste.
Du coup, j'ai testé en mettant la sauvegarde et le load d'une donnée (dont la sauvegarde et le load se fait juste avant celle de "paidDisplayPost2") en commentaire mais le problème persiste. Je pensais que "paidDisplayPost2" n'avait pas pleinement le temps de se sauvegarder, donc en supprimant la sauvegarde de la donnée d'avant, peut être que ... mais non. C'est une stupide idée mais je ne sais plus trop quoi faire ... :?

Pourriez-vous m'aider à identifier la source de mon problème ?

Bien à vous,

Bilu
Dernière édition par Bilu le 21 Oct 2021 13:17, édité 1 fois.

Avatar de l’utilisateur
boubouk50
ModoGenereux
ModoGenereux
Messages : 6216
Inscription : 28 Avr 2014 11:57
Localisation : Saint-Didier-en-Bresse (71)

Re: C# problème de sauvegarde : SerializationException

Message par boubouk50 » 21 Oct 2021 10:46

SerializationException: Type 'BuyShip2Button' is not marked as serializable.

Tu ne peux pas serializer un objet qui n'est pas défini comme serializable.
As-tu donc ajouté [Serializable] devant ta classe BuyShip2Button?

Si oui, peux-tu nous donner le code entier de cette classe?
"Ce n'est pas en améliorant la bougie, que l'on a inventé l'ampoule, c'est en marchant longtemps."
Nétiquette du forum
Savoir faire une recherche
Apprendre la programmation

Bilu
Messages : 91
Inscription : 20 Nov 2019 09:24

Re: C# problème de sauvegarde : SerializationException

Message par Bilu » 21 Oct 2021 11:16

Bonjour boubouk50,

Je n'ai pas mis [Serializable] mais je sais sauvegarder une autre donnée, paidDisplayPost1, dans ce script, juste avant paidDisplayPost2 et ça fonctionne sans soucis

Code : Tout sélectionner


using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using UnityEngine;
using UnityEngine.UI;


public class BuyShip2Button : MonoBehaviour
{
    ...
     int money;
    bool paidDisplayPost0 = true;
    public bool paidDisplayPost1 = false;
    public bool paidDisplayPost2 = false;
    public bool paidDisplayPost3 = false;
    public bool paidDisplayPost4 = false;



    ..

    void Start()
    {
        LoadPlayerMoney();
        ....
        SaveSystem.SavePlayerpaidDisplayPost1(this);
        SaveSystem.SavePlayerpaidDisplayPost2(this);

       
       ...
        
        //LoadPlayerpaidDisplayPost1();
       //LoadPlayerpaidDisplayPost2();
       //LoadPlayerpaidDisplayPost3();
       //LoadPlayerpaidDisplayPost4();
       
    }

    // Update is called once per frame
    void Update()
    {
        LoadPlayerFleetSelection();
        LoadPlayerFleetSelection2();
        LoadPlayerpaidDisplayPost1();
        LoadPlayerpaidDisplayPost2();
        //LoadPlayerpaidDisplayPost3();
       // LoadPlayerpaidDisplayPost4();
        


        ..

    }

    public void LoadPlayerMoney()
    {
        PlayerData data = SaveSystem.LoadPlayerMoney();
        money = data.numberOfMoney;
    }

    public void LoadPlayerpaidDisplayPost1()
    {
        PlayerpaidDisplayPost1 datapaidDisplayPost1 = SaveSystem.LoadPlayerpaidDisplayPost1();
        paidDisplayPost1 = datapaidDisplayPost1.paidDisplayPost1;
    }

    public void LoadPlayerpaidDisplayPost2()
    {
        PlayerpaidDisplayPost2 datapaidDisplayPost2 = SaveSystem.LoadPlayerpaidDisplayPost2();
        paidDisplayPost2 = datapaidDisplayPost2.paidDisplayPost2;
    }

    public void LoadPlayerpaidDisplayPost3()
    {
        PlayerpaidDisplayPost3 datapaidDisplayPost3 = SaveSystem.LoadPlayerpaidDisplayPost3();
        paidDisplayPost3 = datapaidDisplayPost3.paidDisplayPost3;
    }

    public void LoadPlayerpaidDisplayPost4()
    {
        PlayerpaidDisplayPost4 datapaidDisplayPost4 = SaveSystem.LoadPlayerpaidDisplayPost4();
        paidDisplayPost4 = datapaidDisplayPost4.paidDisplayPost4;
    }
    public void LoadPlayerFleetSelection()
    {
        PlayerDataFleetSelection dataFleetSelection = SaveSystem.LoadPlayerFleetSelection();

        position = dataFleetSelection.positionInFleetMenu;
    }

    public void LoadPlayerFleetSelection2()
    {
        PlayerDataFleetSelection2 dataFleetSelection = SaveSystem.LoadPlayerFleetSelection2();

        displayedPosition = dataFleetSelection.displayedPositionInFleetMenu;
    }
               
}

J'ai le même soucis avec paidDisplayPost3 et paidDisplayPost4 mais pas avec FleetSelection1, FleetSelection2, PlayerMoney ni avec paidDisplayPost1

Bilu

Avatar de l’utilisateur
boubouk50
ModoGenereux
ModoGenereux
Messages : 6216
Inscription : 28 Avr 2014 11:57
Localisation : Saint-Didier-en-Bresse (71)

Re: C# problème de sauvegarde : SerializationException

Message par boubouk50 » 21 Oct 2021 11:45

Hein?

Donc SavePlayerpaidDisplayPost1 () fonctionne mais pas SavePlayerpaidDisplayPost2 ()?
Dans ce cas, donne moi aussi le code de cette fonction.
"Ce n'est pas en améliorant la bougie, que l'on a inventé l'ampoule, c'est en marchant longtemps."
Nétiquette du forum
Savoir faire une recherche
Apprendre la programmation

Bilu
Messages : 91
Inscription : 20 Nov 2019 09:24

Re: C# problème de sauvegarde : SerializationException

Message par Bilu » 21 Oct 2021 11:55

Oui c'est ça SavePlayerpaidDisplayPost1 () fonctionne mais pas SavePlayerpaidDisplayPost2 ()

Voici le script où est défini les fonctions SavePlayerpaidDisplayPost1 () et SavePlayerpaidDisplayPost2 ()

Code : Tout sélectionner


public static void SavePlayerpaidDisplayPost1(BuyShip2Button paidDisplayPost1)
     {
            BinaryFormatter formatter4 = new BinaryFormatter();
            string path4 = Application.persistentDataPath + "/paidDisplayPost1.fun";
            FileStream stream4 = new FileStream(path4, FileMode.Create);

        PlayerpaidDisplayPost1 datapaidDisplayPost1 = new PlayerpaidDisplayPost1(paidDisplayPost1);

            Debug.Log("sauvegarde paid1");


            formatter4.Serialize(stream4, datapaidDisplayPost1);
            stream4.Close();
     }

     public static PlayerpaidDisplayPost1 LoadPlayerpaidDisplayPost1()
     {
            string path4 = Application.persistentDataPath + "/paidDisplayPost1.fun";
            if (File.Exists(path4))
            {
                BinaryFormatter formatter4 = new BinaryFormatter();
                FileStream stream4 = new FileStream(path4, FileMode.Open);


            Debug.Log("LOAD paid1");
            PlayerpaidDisplayPost1 datapaidDisplayPost1 = formatter4.Deserialize(stream4) as PlayerpaidDisplayPost1;
                stream4.Close();


                return datapaidDisplayPost1;
            }
            else
            {
            Debug.LogError("Save file not found in" + path4);
            return null;
        }
     }

    public static void SavePlayerpaidDisplayPost2(BuyShip2Button paidDisplayPost2)
    {
        BinaryFormatter formatter5 = new BinaryFormatter();
        string path5 = Application.persistentDataPath + "/paidDisplayPost2.fun";
        FileStream stream5 = new FileStream(path5, FileMode.Create);

        PlayerpaidDisplayPost2 datapaidDisplayPost2 = new PlayerpaidDisplayPost2(paidDisplayPost2);

        Debug.Log("sauvegarde paid2");


        formatter5.Serialize(stream5, paidDisplayPost2);
        stream5.Close();
    }

    public static PlayerpaidDisplayPost2 LoadPlayerpaidDisplayPost2()
    {
        string path5 = Application.persistentDataPath + "/paidDisplayPost2.fun";
        if (File.Exists(path5))
        {
            BinaryFormatter formatter5 = new BinaryFormatter();
            FileStream stream5 = new FileStream(path5, FileMode.Open);


            Debug.Log("load paid2");
            PlayerpaidDisplayPost2 datapaidDisplayPost2 = formatter5.Deserialize(stream5) as PlayerpaidDisplayPost2;
            stream5.Close();


            return datapaidDisplayPost2;
        }
        else
        {
            Debug.LogError("Save file not found in" + path5);
            return null;
        }
    }

et voici comment sont défini PlayerpaidDisplayPost1 et PlayerpaidDisplayPost2

Code : Tout sélectionner


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class PlayerpaidDisplayPost1
{
    public bool paidDisplayPost1;

    
    public PlayerpaidDisplayPost1 (BuyShip2Button paid1)
    {
        paidDisplayPost1 = paid1.paidDisplayPost1;
    }
}

Code : Tout sélectionner


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class PlayerpaidDisplayPost2
{
    public bool paidDisplayPost2;

    
    public PlayerpaidDisplayPost2 (BuyShip2Button paid2)
    {
        paidDisplayPost2 = paid2.paidDisplayPost2;
    }
}



Avatar de l’utilisateur
boubouk50
ModoGenereux
ModoGenereux
Messages : 6216
Inscription : 28 Avr 2014 11:57
Localisation : Saint-Didier-en-Bresse (71)

Re: C# problème de sauvegarde : SerializationException

Message par boubouk50 » 21 Oct 2021 13:03

Code : Tout sélectionner

formatter5.Serialize(stream5, paidDisplayPost2);
ici tu serializes paidDisplayPost2, tu ne devrais pas plutôt serializer datapaidDisplayPost2 ?
"Ce n'est pas en améliorant la bougie, que l'on a inventé l'ampoule, c'est en marchant longtemps."
Nétiquette du forum
Savoir faire une recherche
Apprendre la programmation

Bilu
Messages : 91
Inscription : 20 Nov 2019 09:24

Re: C# problème de sauvegarde : SerializationException

Message par Bilu » 21 Oct 2021 13:08

Si tu as entièrement raison !! :rouge:

Je vais tester ça tout de suite

Edit : C'était bien de là que venait le problème !!!
C'était vraiment stupide et pourtant j'ai relus plusieurs fois !
Merci beaucoup boubouk50

Répondre

Revenir vers « Scripting »