Unity Serializable

Мне надо объект класса записать в файл, а unity бросает исключение введите сюда описание изображения

Сохранить мне надо вот этот класс:

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

public class Game
{
    public static Game current;
    public string name;

    public Vector2 playerPosition;

    public static void CreateNewWorld(string name)
    {

    }
}

Вот код, который вызывается при нажатии на кнопку:

        //этот код чисто для теста
        Game game = new Game();
        game.name = "new world";
        Game.current = game;


        savedGames.Add(Game.current);

        BinaryFormatter bf = new BinaryFormatter();

        if (!Directory.Exists(Application.persistentDataPath + "/Worlds/" + Game.current.name))
        {
            Directory.CreateDirectory(Application.persistentDataPath + "/Worlds/" + Game.current.name);
        }

        FileStream file = File.Create(Application.persistentDataPath + "/Worlds/" + Game.current.name + "/save.gd");
        bf.Serialize(file, Game.current);
        file.Close();

т.е. Код верный, но c# не хочет сохранять мой класс.

upd: Забыл сказать, что этот код создает вот такой файл: введите сюда описание изображения

upd2: Я написал вот такой класс:

public static class SaveLoad
{
    public static List<World> savedWorlds = new List<World>();

    public static void SaveObject(string Name, System.Object Object)
    {
        IFormatter formatter = new BinaryFormatter();

        if (!Directory.Exists(Application.persistentDataPath + "/Worlds/" + World.current.name))
        {
            Directory.CreateDirectory(Application.persistentDataPath + "/Worlds/" + World.current.name);
        }

        using (Stream stream = new FileStream(Application.persistentDataPath + "/Worlds/" + World.current.name + "/" + Name + ".save", FileMode.Create, FileAccess.Write, FileShare.None))
        {
            formatter.Serialize(stream, Object);
        }
    }

    public static void SaveWorld()
    {
        IFormatter formatter = new BinaryFormatter();

        if (!Directory.Exists(Application.persistentDataPath + "/Worlds/" + World.current.name))
        {
            Directory.CreateDirectory(Application.persistentDataPath + "/Worlds/" + World.current.name);
        }

        using (Stream stream = new FileStream(Application.persistentDataPath + "/Worlds/" + World.current.name + "/" + "world.save", FileMode.Create, FileAccess.Write, FileShare.None))
        {
            formatter.Serialize(stream, World.current);
        }
    }

    public static object Load(string Name)
    {
        object Object = null;
        IFormatter formatter = new BinaryFormatter();
        using (Stream stream = new FileStream(Application.persistentDataPath + "/Worlds/" + World.current.name + "/" + Name + ".save", FileMode.Open, FileAccess.Read, FileShare.Read))
        {
            Object = formatter.Deserialize(stream);
        }
        return Object;
    }

    public static void LoadWorlds()
    {
        object Object = null;
        IFormatter formatter = new BinaryFormatter();
        string[] worldsPath = {};

        try
        {
            worldsPath = Directory.GetDirectories(Application.persistentDataPath + "/Worlds");
        }
        catch
        {
            Debug.LogError("Error");
        }

        foreach (string path in worldsPath)
        {
            try
            {
                using (Stream stream = new FileStream(Application.persistentDataPath + "/Worlds/" + World.current.name + "/" + "world.save", FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    Object = formatter.Deserialize(stream);
                }
            }
            catch
            {
                Debug.LogError("SaveLoad: возможно файла world.save не существует!");
            }
            savedWorlds.Add(Object as World);
        }
        foreach(World world in savedWorlds)
        {
            Debug.Log(world.name);
        }

    }
}

Потом просто сделал вот такую штуку ДЛЯ ТЕСТА:

World world = new World();
world.name = "new world";
World.current = world;

SaveLoad.SaveWorld();

Что тут происходит? Создаем объект World и сохраняем его. И мне падает тоже самое исключение!!!!введите сюда описание изображения


Ответы (0 шт):