二进制格式化程序节省加载但不购买

时间:2015-11-05 08:57:32

标签: c# unity3d binary formatter

我正在使用二进制格式化程序来保存游戏变量的数据。它节省了高分,硬币和装载非常好。但是当玩家拥有10个硬币时,我希望他能够购买一些商品。它正在编辑完美。但在Android设备中,它无法正常工作。但同样,它是储蓄和装载硬币和高分。这是我的代码:

GameController Class

[SerializeField]
private Text coinText,highScoreText;

public static GameController instance;
public GameData data;
public int currentLevel;
public int currentScore;

public bool isGameStartedFirstTime;
public bool isMusicOn;
public int selectedLevel;
public int coins;
public int highScore;
public bool[] levels;

void Awake () 
{
    MakeSingleton();
    InitializeGameVariables();
    coinText.text=data.getCoins().ToString ();
    highScoreText.text = data.getHighScore().ToString ();
}

void MakeSingleton()
{
    if(instance!=null)
        Destroy (gameObject);
    else
    {
        instance=this;
        DontDestroyOnLoad(gameObject);
    }
}
void InitializeGameVariables()
{
    Load ();
    if(data!=null)
    {
        isGameStartedFirstTime=data.getIsGameStartedFirstTime();
    }
    else
    {
        isGameStartedFirstTime=true;
    }
    if(isGameStartedFirstTime)
    {
        highScore=0;
        coins=0;
        selectedLevel=0;
        isGameStartedFirstTime=false;
        isMusicOn=false;
        levels = new bool[7];

        levels[0] = true;
        for(int i = 1; i<levels.Length;i++)
        {
            levels[i] = false;
        }

        data = new GameData();
        data.setHighScore(highScore);
        data.setCoins(coins);
        data.setIsGameStartedFirstTime(isGameStartedFirstTime);
        data.setLevels(levels);
        data.setMusicOn(isMusicOn);
        data.setSelectedLevel(selectedLevel);

        Save ();
        Load ();
    }
    else
    {
        highScore=data.getHighScore();
        coins = data.getCoins();
        isGameStartedFirstTime = data.getIsGameStartedFirstTime();
        levels=data.getLevels();
        isMusicOn=data.getMusicOn();
        selectedLevel=data.getSelectedLevel();
    }
}
public void  Save()
{
    FileStream file = null;
    try
    {
        BinaryFormatter bf = new BinaryFormatter();
        file = File.Create(Application.persistentDataPath + "/GameData.dat");
        if(data!=null)
        {
            data.setHighScore(highScore);
            data.setCoins(coins);
            data.setIsGameStartedFirstTime(isGameStartedFirstTime);
            data.setLevels(levels);
            data.setMusicOn(isMusicOn);
            data.setSelectedLevel(selectedLevel);

            bf.Serialize(file,data);
        }
    }
    catch(Exception e)
    {

    }
    finally
    {
        if(file!=null)
        file.Close();
    }
}
public void Load()
{
    FileStream file = null;

    try
    {
        BinaryFormatter bf = new BinaryFormatter();

        file = File.Open(Application.persistentDataPath + "/GameData.dat",FileMode.Open);
        data = (GameData)bf.Deserialize(file);
    }
    catch(Exception e)
    {

    }
    finally
    {
        if(file!=null)
            file.Close();
    }
}

GameData Class

private bool isGameStartedFirstTime;
private bool isMusicOn;
private int selectedLevel;
private int coins;
private int highScore;
private bool[] levels;

public void setIsGameStartedFirstTime(bool isGameStartedFirstTime)
{
    this.isGameStartedFirstTime = isGameStartedFirstTime;
}
public bool getIsGameStartedFirstTime()
{
    return this.isGameStartedFirstTime;
}
public void setMusicOn(bool isMusicOn)
{
    this.isMusicOn=isMusicOn;
}
public bool getMusicOn()
{
    return this.isMusicOn;
}
public void setSelectedLevel(int selectedLevel)
{
    this.selectedLevel=selectedLevel;
}
public int getSelectedLevel()
{
    return this.selectedLevel;
}
public void setCoins(int coins)
{
    this.coins=coins;
}
public int getCoins()
{
    return this.coins;
}
public void setHighScore(int highScore)
{
    this.highScore=highScore;
}
public int getHighScore()
{
    return this.highScore;
}
public void setLevels(bool[] levels)
{
    this.levels=levels;
}
public bool[] getLevels()
{
    return this.levels;
}

这是我的购买代码:

public void BuyItem(int index)
{
    if(GameController.instance.levels[index]==true)
    {
        Application.LoadLevel ("Zombies");
    }
    else
    {
        if(GameController.instance.coins>=10)
        {
            BuyImages[index-1].SetActive (false);
            GameController.instance.levels[index]=true;
            GameController.instance.coins-=10;
            coinsText.text="" + GameController.instance.coins;
            GameController.instance.Save ();

        }
    }

}

虽然保存了数据,但它正在编辑器而不是设备。

0 个答案:

没有答案