2023-08-22 17:02:25 +03:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Runtime.Serialization.Formatters.Binary;
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
public class SaveLoadController : MonoBehaviour
|
|
|
|
{
|
|
|
|
public static SaveLoadController Instance;
|
|
|
|
|
|
|
|
private const string DIR_PATH = "/game_save/players/";
|
|
|
|
|
|
|
|
private void Awake()
|
|
|
|
{
|
2023-10-09 15:12:08 +03:00
|
|
|
transform.parent = null;
|
|
|
|
|
|
|
|
if (Instance == null)
|
2023-08-22 17:02:25 +03:00
|
|
|
{
|
|
|
|
Instance = this;
|
2023-10-09 15:12:08 +03:00
|
|
|
DontDestroyOnLoad(gameObject);
|
2023-08-22 17:02:25 +03:00
|
|
|
}
|
2023-10-09 15:12:08 +03:00
|
|
|
else if (Instance == this)
|
2023-08-22 17:02:25 +03:00
|
|
|
{
|
2023-10-09 15:12:08 +03:00
|
|
|
Destroy(gameObject);
|
2023-08-22 17:02:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-10 15:03:37 +03:00
|
|
|
public bool Save<T>(T so, string filename) where T: ScriptableObject
|
2023-08-22 17:02:25 +03:00
|
|
|
{
|
|
|
|
string fullDirPath = Application.persistentDataPath + DIR_PATH;
|
2023-10-10 15:03:37 +03:00
|
|
|
string fullFilePath = Application.persistentDataPath + DIR_PATH + $"{filename}.txt";
|
2023-08-22 17:02:25 +03:00
|
|
|
|
|
|
|
if (!Directory.Exists(fullDirPath))
|
|
|
|
{
|
|
|
|
Directory.CreateDirectory(fullDirPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
BinaryFormatter bf = new BinaryFormatter();
|
|
|
|
FileStream fs = File.Create(fullFilePath);
|
|
|
|
var json = JsonUtility.ToJson(so);
|
|
|
|
bf.Serialize(fs, json);
|
|
|
|
fs.Close();
|
2023-10-10 15:03:37 +03:00
|
|
|
|
|
|
|
return true;
|
2023-08-22 17:02:25 +03:00
|
|
|
}
|
|
|
|
|
2023-10-10 15:03:37 +03:00
|
|
|
public bool Load<T>(ref T so, string filename) where T: ScriptableObject
|
2023-08-22 17:02:25 +03:00
|
|
|
{
|
|
|
|
string fullDirPath = Application.persistentDataPath + DIR_PATH;
|
2023-10-10 15:03:37 +03:00
|
|
|
string fullFilePath = Application.persistentDataPath + DIR_PATH + $"{filename}.txt";
|
2023-08-22 17:02:25 +03:00
|
|
|
|
|
|
|
Debug.Log(fullFilePath);
|
|
|
|
|
|
|
|
if (!Directory.Exists(fullDirPath))
|
|
|
|
{
|
|
|
|
Directory.CreateDirectory(fullDirPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
BinaryFormatter bf = new BinaryFormatter();
|
|
|
|
|
|
|
|
if (File.Exists(fullFilePath))
|
|
|
|
{
|
|
|
|
FileStream fs = File.Open(fullFilePath, FileMode.Open);
|
|
|
|
so = ScriptableObject.CreateInstance<T>();
|
|
|
|
JsonUtility.FromJsonOverwrite((string)bf.Deserialize(fs), so);
|
|
|
|
|
|
|
|
fs.Close();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
so = ScriptableObject.CreateInstance<T>();
|
|
|
|
}
|
2023-10-10 15:03:37 +03:00
|
|
|
|
|
|
|
return true;
|
2023-08-22 17:02:25 +03:00
|
|
|
}
|
|
|
|
}
|