Search code examples
c#unity-game-enginedeserializationbinaryformatter

BinaryFormatter No map for object


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!


Solution

  • Solved problem. Use Resource.Load<TextAsset>(path).bytes to get byte array and do deserialize like normal.