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'.
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!
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
Solved problem. Use Resource.Load<TextAsset>(path).bytes
to get byte array and do deserialize like normal.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.