将可编写脚本的对象数组保存到 json 文件时出现问题 UNITY/C#
Problem when saving an array of scriptable objects into a json file UNITY/C#
我试图将可编写脚本的对象数组的数据存储到序列化的 json 文件中,当我只保存 1 个对象时没有问题,但是当我尝试循环并保存所有数组时它只保存数组中的最后一个。知道如何解决这个问题吗?
C# 代码-
public static GameSaveManager instance;
[SerializeField] private ShopItem[] shopitems;
[SerializeField] private BackgroundShopItem[] backgroundShopItems;
private void Awake()
{
if (instance == null)
{
instance = this;
}
else if (instance != this)
{
Destroy(this);
}
DontDestroyOnLoad(this);
}
public bool IsSaveFile()
{
return Directory.Exists(Application.persistentDataPath + "game_save");
}
public void SaveGame()
{
if (!IsSaveFile())
{
Directory.CreateDirectory(Application.persistentDataPath + "/game_save");
}
if (!Directory.Exists(Application.persistentDataPath + "/game_sava/data"))
{
Directory.CreateDirectory(Application.persistentDataPath + "/game_save/data");
}
BinaryFormatter bf = new BinaryFormatter();
for (int i = 0; i < shopitems.Length; i++)
{
ShopItem si = shopitems[i];
FileStream file = File.Create(Application.persistentDataPath + "/game_save/data/skins_save.txt");
var json = JsonUtility.ToJson(si);
bf.Serialize(file, json);
file.Close();
}
}
在您的循环 for (int i = 0; i < shopitems.Length; i++)
中,您将每个文件保存到 相同的 位置 - Application.persistentDataPath + "/game_save/data/skins_save.txt"
,这样您的文件就会被覆盖。只需使用不同的保存文件:Application.persistentDataPath + "/game_save/data/skins_save_{i}.txt"
如果您想将其序列化为一个文件,请考虑为其实施您自己的可序列化 class 并将您的 ShopItem[] 数组保留在那里。
[Serializable]
public class SaveItems
{
public ShopItem[] shopitems;
}
所以在你的 SaveGame() 中你会得到这样的东西:
public void SaveGame()
{
// previous code here
BinaryFormatter bf = new BinaryFormatter();
SaveItems si = new SaveItems();
si.shopitems = shopitems;
FileStream file = File.Create(Application.persistentDataPath + "/game_save/data/skins_save.txt");
var json = JsonUtility.ToJson(si);
bf.Serialize(file, json);
file.Close();
}
我试图将可编写脚本的对象数组的数据存储到序列化的 json 文件中,当我只保存 1 个对象时没有问题,但是当我尝试循环并保存所有数组时它只保存数组中的最后一个。知道如何解决这个问题吗?
C# 代码-
public static GameSaveManager instance;
[SerializeField] private ShopItem[] shopitems;
[SerializeField] private BackgroundShopItem[] backgroundShopItems;
private void Awake()
{
if (instance == null)
{
instance = this;
}
else if (instance != this)
{
Destroy(this);
}
DontDestroyOnLoad(this);
}
public bool IsSaveFile()
{
return Directory.Exists(Application.persistentDataPath + "game_save");
}
public void SaveGame()
{
if (!IsSaveFile())
{
Directory.CreateDirectory(Application.persistentDataPath + "/game_save");
}
if (!Directory.Exists(Application.persistentDataPath + "/game_sava/data"))
{
Directory.CreateDirectory(Application.persistentDataPath + "/game_save/data");
}
BinaryFormatter bf = new BinaryFormatter();
for (int i = 0; i < shopitems.Length; i++)
{
ShopItem si = shopitems[i];
FileStream file = File.Create(Application.persistentDataPath + "/game_save/data/skins_save.txt");
var json = JsonUtility.ToJson(si);
bf.Serialize(file, json);
file.Close();
}
}
在您的循环 for (int i = 0; i < shopitems.Length; i++)
中,您将每个文件保存到 相同的 位置 - Application.persistentDataPath + "/game_save/data/skins_save.txt"
,这样您的文件就会被覆盖。只需使用不同的保存文件:Application.persistentDataPath + "/game_save/data/skins_save_{i}.txt"
如果您想将其序列化为一个文件,请考虑为其实施您自己的可序列化 class 并将您的 ShopItem[] 数组保留在那里。
[Serializable]
public class SaveItems
{
public ShopItem[] shopitems;
}
所以在你的 SaveGame() 中你会得到这样的东西:
public void SaveGame()
{
// previous code here
BinaryFormatter bf = new BinaryFormatter();
SaveItems si = new SaveItems();
si.shopitems = shopitems;
FileStream file = File.Create(Application.persistentDataPath + "/game_save/data/skins_save.txt");
var json = JsonUtility.ToJson(si);
bf.Serialize(file, json);
file.Close();
}