Делал сохранение по уроку: https://www.youtube.com/watch?v=5YXGtXwxaIM&t=2s
Вот скрипт
Синтаксис:
Используется csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
public class GameInstance : MonoBehaviour {
private void Awake()
{
Load();
}
public int _result;
[System.Serializable]
public class Saver
{
public int _currentResult;
}
public void Save()
{
Saver _saver = new Saver();
_saver._currentResult = _result;
if (!Directory.Exists(Application.dataPath + "/saves"))
{
Directory.CreateDirectory(Application.dataPath + "/saves");
}
FileStream fs = new FileStream(Application.dataPath + "/saves/save.gs", FileMode.Create);
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(fs, _saver);
fs.Close();
}
public void Load()
{
if (File.Exists(Application.dataPath + "/saves/save.gs"))
{
FileStream fs = new FileStream(Application.dataPath + "/saves/save.gs", FileMode.Open);
BinaryFormatter formatter = new BinaryFormatter();
try
{
Saver _saver = (Saver)formatter.Deserialize(fs);
_result = _saver._currentResult;
}
catch (System.Exception e)
{
Debug.Log(e.Message);
}
finally
{
fs.Close();
}
}
else
{
Debug.Log("Save doesn't exist");
}
}
}
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
public class GameInstance : MonoBehaviour {
private void Awake()
{
Load();
}
public int _result;
[System.Serializable]
public class Saver
{
public int _currentResult;
}
public void Save()
{
Saver _saver = new Saver();
_saver._currentResult = _result;
if (!Directory.Exists(Application.dataPath + "/saves"))
{
Directory.CreateDirectory(Application.dataPath + "/saves");
}
FileStream fs = new FileStream(Application.dataPath + "/saves/save.gs", FileMode.Create);
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(fs, _saver);
fs.Close();
}
public void Load()
{
if (File.Exists(Application.dataPath + "/saves/save.gs"))
{
FileStream fs = new FileStream(Application.dataPath + "/saves/save.gs", FileMode.Open);
BinaryFormatter formatter = new BinaryFormatter();
try
{
Saver _saver = (Saver)formatter.Deserialize(fs);
_result = _saver._currentResult;
}
catch (System.Exception e)
{
Debug.Log(e.Message);
}
finally
{
fs.Close();
}
}
else
{
Debug.Log("Save doesn't exist");
}
}
}
В редакторе сохранение работает, в билде (под Виндоус 32 и 64) - не работает.