Beispiel #1
0
    /// <summary>
    /// Добовляем компоненту в список сохраняемых/загружемых. Добовляем хранилище в словарь сохроняемых/загружемых
    /// </summary>
    /// <param name="key">ключь по которому идентифицируем переменную</param>
    public static void Set(SaveLoadComponent component, string key, LocalStorage localStorage)
    {
        //если компоненты нет среди сохраняемых, добавляем её
        if (!listComponents.Contains(component))
        {
            listComponents.Add(component);
        }

        //Проверяем наличие ключае и либо добаляем ключ с хранилищем, либо перезаписываем значение по ключу
        key = component + key;
        if (dictionaryComponentAndLocalStorage.ContainsKey(key))
        {
            dictionaryComponentAndLocalStorage[key] = localStorage;
        }
        else
        {
            dictionaryComponentAndLocalStorage.Add(key, localStorage);
        }
    }
    public static void RequestLoad()
    {
        DestroyAllGameobjects();
        if (!File.Exists(savePath))
        {
            Debug.Log("There's no save to load");
            return;
        }
        BinaryFormatter bf   = CreateBinaryFormatter();
        FileStream      file = File.Open(savePath, FileMode.Open);
        List <SaveLoadComponent.GameobjectSaveData> saves = bf.Deserialize(file) as List <SaveLoadComponent.GameobjectSaveData>;

        Debug.Log("Size is " + saves.Count);
        foreach (var save in saves)
        {
            try
            {
                string     path = "Prefabs/" + save.prefabPath;
                GameObject obj  = PrefabLoader.GetPrefab <GameObject>(path);
                if (obj == null)
                {
                    throw new Exception("Could not load prefab " + path);
                }
                SaveLoadComponent comp = GameObject.Instantiate(obj).GetComponent <SaveLoadComponent>();
                if (comp == null)
                {
                    throw new Exception("No SaveLoadComponent on gameobject " + obj.name);
                }
                comp.Load(save);
            }
            catch (Exception e)
            {
                Debug.LogError("Error when loading: " + e.ToString());
            }
        }

        file.Close();
    }
 public static void Unregister(SaveLoadComponent comp)
 {
     components.Remove(comp);
 }
 public static void Register(SaveLoadComponent comp)
 {
     components.Add(comp);
 }
Beispiel #5
0
 /// <summary>
 /// Удаляем компонент из сохраняемых
 /// </summary>
 public static void RemoveDictionary(SaveLoadComponent component, string key)
 {
     listComponents.Remove(component);
     dictionaryComponentAndLocalStorage.Remove(component + key);
 }
Beispiel #6
0
 /// <summary>
 /// Получаем хранилище из словаря сохроняемых/загружемых
 /// </summary>
 /// <param name="key">ключь по которому идентифицируем переменную</param>
 public static LocalStorage Get(SaveLoadComponent component, string key)
 {
     return(dictionaryComponentAndLocalStorage[component + key]);
 }