Пятница, 29 Марта 2024, 08:48

Приветствую Вас Гость

[ Новые сообщения · Игроделы · Правила · Поиск ]
  • Страница 1 из 1
  • 1
Форум игроделов » Программирование » Общие обсуждения программистов » Десериализация (Десериализация из бинарного файла)
Десериализация
vova1227Дата: Четверг, 01 Апреля 2021, 20:47 | Сообщение # 1
был не раз
Сейчас нет на сайте
я читал что можно дописывать данные в файл путем FileMode.Append.
Дописать в файл удалось, файл открыл блокнотом и Vector4 там значился, значит данные все таки в файле.
Но вот при возврате возникает ошибка.
При загрузке этого сохранения Unity показывает ошибку в строчке

Код
objectsT = (List<myVector4>)formatter.Deserialize(streamT);


InvalidCastException: Specified cast is not valid. SaveGameToM.LoadT () (at Assets / Script / SaveGameToM.cs: 111)

Что не верно?

Код
[System.Serializable]
    public struct myVector3
    {
        public float x, y, z;
    }
    [System.Serializable]
    public struct myVector4
    {
        public float j, k, l;
    }

    [SerializeField] public List<myVector3> ObjectsPositions = new List<myVector3>();
    [SerializeField] public List<myVector4> ObjectsPositionsT = new List<myVector4>();
    
    public void Start()
    {
        if (File.Exists(Application.persistentDataPath + "/saves/save.sav"))
        {
            BackMyCubes();
            BackMyTriangles();
        }
    }
    public void BackMyCubes()
    {
        ObjectsPositions = Load();
        foreach (var pos in ObjectsPositions)
        {
            GameObject i = Instantiate(Cube1Pref, new Vector3(pos.x, pos.y, pos.z), Quaternion.identity);
        }
        ObjectsPositions.Clear();
    }
    public void BackMyTriangles()
    {
        ObjectsPositionsT = LoadT();
        foreach (var posT in ObjectsPositionsT)
        {
            GameObject t = Instantiate(Triangle1Pref, new Vector4(posT.j, posT.k, posT.l), Quaternion.identity);
        }
        ObjectsPositionsT.Clear();
    }
    public void ManualSaving()
    {
        foreach (var obj in GameObject.FindGameObjectsWithTag("Cube1"))
        {
            myVector3 i = new myVector3();
            i.x = obj.transform.position.x;
            i.y = obj.transform.position.y;
            i.z = obj.transform.position.z;           
            ObjectsPositions.Add(i);
        }
        SaveCu(ObjectsPositions);
    }
    public void ManualSavingT()
    {
        foreach (var objT in GameObject.FindGameObjectsWithTag("Triangle1"))
        {
            myVector4 t = new myVector4();
            t.j = objT.transform.position.x;
            t.k = objT.transform.position.y;
            t.l = objT.transform.position.z;
            ObjectsPositionsT.Add(t);
        }
        SaveT(ObjectsPositionsT);
    }
    public List<myVector3> Load()
    {
        List<myVector3> objects;        
        BinaryFormatter formatter = new BinaryFormatter();
        using (FileStream stream = new FileStream(Application.persistentDataPath + "/saves/save.sav", FileMode.Open))
        {
            objects = (List<myVector3>)formatter.Deserialize(stream);
        }
        return objects;
    }
    public List<myVector4> LoadT()
    {
        List<myVector4> objectsT;
        BinaryFormatter formatter = new BinaryFormatter();
        using (FileStream streamT = new FileStream(Application.persistentDataPath + "/saves/save.sav", FileMode.Open))
        {
            objectsT = (List<myVector4>)formatter.Deserialize(streamT);
        }
        return objectsT;
    }
    void SaveCu(List<myVector3> positions)
    {
        BinaryFormatter formatter = new BinaryFormatter();
        using (FileStream stream = new FileStream(Application.persistentDataPath + "/saves/save.sav", FileMode.Create))
        {
            formatter.Serialize(stream, positions);
            stream.Close();
        }
        positions.Clear();
    }
    void SaveT(List<myVector4> positionsT)
    {
        BinaryFormatter formatter = new BinaryFormatter();
        using (FileStream streamT = new FileStream(Application.persistentDataPath + "/saves/save.sav", FileMode.Append))
        {
            formatter.Serialize(streamT, positionsT);
            streamT.Close();
        }
        positionsT.Clear();
    }
}
Форум игроделов » Программирование » Общие обсуждения программистов » Десериализация (Десериализация из бинарного файла)
  • Страница 1 из 1
  • 1
Поиск:

Все права сохранены. GcUp.ru © 2008-2024 Рейтинг