- Home /
Question by
ealva479 · Jan 15, 2019 at 03:45 AM ·
loadingsavingscriptable object
How do I save a list of scriptable objects?
I tried following a tutorial on saving scriptable objects, and changing the script a bit to save a list of scriptable objects, and it's not working for me. I literally can't find ANYTHING on saving lists of scriptable objects. Does anybody know what I'm doing wrong?
public class ScriptableObjectSave : MonoBehaviour
{
public static ScriptableObjectSave instance;
public Item item;
public List<Item> items;
[SerializeField] Inventory inventory;
void Awake()
{
if(instance == null)
{
instance = this;
}
else if(instance != this)
{
Destroy(this);
}
DontDestroyOnLoad(this);
}
private void Start()
{
}
public bool IsSaveFile()
{
return Directory.Exists(Application.persistentDataPath + "/SO_save");
}
public void SaveGame()
{
items = inventory.items;
if (!IsSaveFile())
{
Directory.CreateDirectory(Application.persistentDataPath + "/SO_save");
}
if(!Directory.Exists(Application.persistentDataPath + "/SO_save/character_data"))
{
Directory.CreateDirectory(Application.persistentDataPath + "/SO_save/character_data");
}
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/SO_save/character_data/character_save.txt");
var json = JsonUtility.ToJson(items);
bf.Serialize(file, json);
file.Close();
}
public void LoadGame()
{
if(!Directory.Exists(Application.persistentDataPath + "/SO_save/character_data"))
{
Directory.CreateDirectory(Application.persistentDataPath + "/SO_save/character_data");
}
BinaryFormatter bf = new BinaryFormatter();
if(File.Exists(Application.persistentDataPath + "/SO_save/character_data/character_save.txt"))
{
FileStream file = File.Open(Application.persistentDataPath + "/SO_save/character_data/character_save.txt", FileMode.Open);
JsonUtility.FromJsonOverwrite((string)bf.Deserialize(file), items);
file.Close();
}
inventory.items = items;
}
Comment
Your answer
Follow this Question
Related Questions
Problem with showing "infinite saves" menu 0 Answers
How to prevent loading from freezing the game ? 2 Answers
Some help with saving and loading in Unity 1 Answer
how to properly save an int value ? 2 Answers
Problem when saving and loading 1 Answer