gpt4 book ai didi

c# - 如何管理数百个变量的保存/加载

转载 作者:太空宇宙 更新时间:2023-11-03 22:58:28 25 4
gpt4 key购买 nike

我正在 Unity 中制作角色扮演游戏。还是很新所以我关注了their创建保存/加载功能的教程。

在本教程中,他们使用了 2 个变量,并且效果很好。但是我想创建一个带有沙盒的大型游戏。这意味着我需要保存所有东西在我的世界中的位置,除此之外我还需要很多玩家统计数据、可解锁项、任务、NPC 进度等等。

到目前为止,我有一个用于保存/加载我的玩家初始统计数据的有效实现,仅此一项就产生了很多:

数据管理器

public class DataManager : MonoBehaviour {

private string saveLocation = "/data.dat";

public static DataManager manager;

//PLAYER
public int maxHealth;
public int currentHealth;
public int currentLevel;
public int currentExp;
public int [] toLevelUp;

void Awake(){
if (manager == null) {
DontDestroyOnLoad (gameObject);
manager = this;
} else if (manager != this) {
Destroy (gameObject);
}
}


public void Save(){
Debug.Log ("Saving");
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Create (Application.persistentDataPath + saveLocation);

PlayerData playerData = new PlayerData (maxHealth, currentHealth, currentLevel, currentExp, toLevelUp);

bf.Serialize (file, playerData);
file.Close ();
}

public void Load(){
Debug.Log ("Loading");
if (File.Exists (Application.persistentDataPath + saveLocation)) {
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Open (Application.persistentDataPath + saveLocation, FileMode.Open);

PlayerData playerData = (PlayerData)bf.Deserialize (file);
file.Close ();

maxHealth = playerData.maxHealth;
currentHealth = playerData.currentHealth;
currentLevel = playerData.currentLevel;
currentExp = playerData.currentExp;
toLevelUp = playerData.toLevelUp;
}
}
}

玩家数据

[System.Serializable]
public class PlayerData {

public int maxHealth;
public int currentHealth;
public int currentLevel;
public int currentExp;
public int [] toLevelUp;

public PlayerData(int maxHealth, int currentHealth, int currentLevel, int currentExp, int [] toLevelUp){
this.maxHealth = maxHealth;
this.currentHealth = currentHealth;
this.currentLevel = currentLevel;
this.currentExp = currentExp;
this.toLevelUp = toLevelUp;
}

}

这感觉上根本不是很可扩展。有人有为很多变量实现保存/加载的经验,有任何建议/代码示例吗?一定有更好的方法吗?

编辑:

新数据管理器:

public class DataManager : MonoBehaviour {

private string saveLocation = "/data.dat";

public static DataManager manager;

//PLAYER
public PlayerData playerData = new PlayerData ();

void Awake(){
if (manager == null) {
DontDestroyOnLoad (gameObject);
manager = this;
} else if (manager != this) {
Destroy (gameObject);
}

}


public void Save(){
Debug.Log ("Saving");
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Create (Application.persistentDataPath + saveLocation);

bf.Serialize (file, playerData);
file.Close ();
}

public void Load(){
Debug.Log ("Loading");
if (File.Exists (Application.persistentDataPath + saveLocation)) {
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Open (Application.persistentDataPath + saveLocation, FileMode.Open);

PlayerData pd = (PlayerData)bf.Deserialize (file);
file.Close ();

playerData = pd;
}
}

最佳答案

您不必每次都创建新的 PlayerData 来保存它。您不必像在构造函数中那样复制它。创建一个 PlayerData 实例,然后在您的游戏中使用它。就是这样。删除那些局部变量,因为它们已经在 PlayerData 类中。

不要这样做:

[System.Serializable]
public class PlayerData
{

public int maxHealth;
public int currentHealth;
public int currentLevel;
public int currentExp;
public int[] toLevelUp;
}

//Why Why would you need these again?
public int maxHealth;
public int currentHealth;
public int currentLevel;
public int currentExp;
public int[] toLevelUp;

private string saveLocation = "/data.dat";

//PLAYER
public PlayerData playerData = new PlayerData();

public void Save()
{
if (File.Exists(Application.persistentDataPath + saveLocation))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + saveLocation, FileMode.Open);

PlayerData playerData = (PlayerData)bf.Deserialize(file);
file.Close();

//Whyyyyyyyyyyyy Don't do this
maxHealth = playerData.maxHealth;
currentHealth = playerData.currentHealth;
currentLevel = playerData.currentLevel;
currentExp = playerData.currentExp;
toLevelUp = playerData.toLevelUp;
}
}

请注意您是如何重新定义 maxHealthcurrentHealthcurrentLevelcurrentExptoLevelUp再次变量。完全没有必要。

这样做:

[System.Serializable]
public class PlayerData
{

public int maxHealth;
public int currentHealth;
public int currentLevel;
public int currentExp;
public int[] toLevelUp;
}

string saveLocation = "/data.dat";

//Make the PlayerData data part of your game
PlayerData playerData;

void Start()
{

playerData = new PlayerData();
playerData.maxHealth = 100;
playerData.currentLevel = 1;
playerData.currentHealth = 50;
}

public void Save(PlayerData pData)
{
UnityEngine.Debug.Log("Saving");
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + saveLocation);
bf.Serialize(file, pData);
file.Close();
}

当你想保存时,调用Save(playerData);即可。

注意不需要复制数据或执行 PlayerData playerData = new PlayerData (maxHealth, currentHealth, currentLevel, currentExp, toLevelUp);。只需保存您已经在游戏中使用的实例。


最后,用DataSaver ,您可以在一行中完成此操作 DataSaver.saveData(playerData, "players");

关于c# - 如何管理数百个变量的保存/加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44288761/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com