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

时间:2017-05-31 15:23:25

标签: c# unity3d unity5

我在Unity制作RPG。还是很新,所以我按照their教程创建了保存/加载功能。

在本教程中,他们使用了2个变量,为此它工作得很好。 但我想用沙盒创建一个大型游戏。这意味着我需要保存我世界上所有东西的位置,最重要的是我需要很多玩家统计数据,解锁,任务,npc进度等等。

到目前为止,我有一个保存/加载我的玩家初始统计数据的工作实现,而且仅此一点已经变成了很多:

的DataManager

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;
        }
    }
}

PlayerData

[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;
    }
}

}

1 个答案:

答案 0 :(得分:3)

每次要保存时,都不必创建新的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");

中执行此操作