Home > Net >  BinaryFormatter No map for object
BinaryFormatter No map for object

Time:08-17

I'm serialized my object and put it in Resources folder in unity. My game should copy this file into Application.persistentDataPath path and Deserialize. There i'm gettimg errror: Failed to serialize. Reason: No map for object '201326592'.

My "copy" code:

public void CopyAndDeserialize()
{
    saveData.itemsOfClothing = new List<ItemsOfClothingData.ItemOfClothing>();
    TextAsset resourcesTextData = (TextAsset)Resources.Load("items");

    path = Path.Combine(StorageManager.Instance.GlobalPath   _saveFileName);

    if(!File.Exists(path))
    {
        File.Create(path);
    }

    File.WriteAllText(path, resourcesTextData.text);

    DeserializeData();
}

DeserializeData:

public void DeserializeData()
{
    path = Path.Combine(StorageManager.Instance.GlobalPath   _saveFileName);

    if (File.Exists(path))
    {
        fileStream = new FileStream(path, FileMode.Open);

        BinaryFormatter binaryFormatter = new BinaryFormatter();
        try
        {
            saveData = (ItemsOfClothingData)binaryFormatter.Deserialize(fileStream);
        }
        catch (SerializationException e)
        {
            MonoBehaviour.print("Failed to serialize. Reason: "   e.Message);
            throw;
        }
        finally
        {
            fileStream.Close();
        }
        fileStream.Close();
    }
}

Thanks for your help!

CodePudding user response:

The file is not a binary format, use System.IO.File.ReadAllLines(string filepath) or System.IO.File.ReadAllText(string filepath) to read it properly.

See the Microsoft documentation for more information: https://docs.microsoft.com/en-us/dotnet/api/system.io.file.readalllines?view=net-6.0

CodePudding user response:

Solved problem. I found solution here: https://forum.unity.com/threads/load-binery-data-from-dat-file-or-if-necessary-txt-on-mobile-phone.258165/

  • Related