Unity游戏在Android和iOS上存储数据

时间:2018-09-12 19:42:04

标签: android ios unity3d

我正在用统一游戏引擎编写一个简单的游戏,它由运行和避免障碍组成。在android平台上,我可以存储和保存数据(分数,硬币...),但在iOS上,我不能。 每当我关闭游戏并重新打开游戏(就像第一次一样)时,就会发生这种情况。 我将Windows与统一2017.3.1f1一起使用,并且在具有Mac OS的虚拟机上生成了.ipa文件。

谢谢

代码:

   using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

public class GameManager : MonoBehaviour {

    public static GameManager instance;

    private GameData gameData;

    [HideInInspector]
    public int starScore, score_Count, selected_Index;

    [HideInInspector]
    public bool[] heroes;

    [HideInInspector]
    public bool playSound = true;

    private string data_Path = "GameData.dat";

    void Awake () {
        MakeSingleton ();

        InitializeGameData ();
    }

    void Start() {
        //print (Application.persistentDataPath + data_Path);
    }

    void MakeSingleton() {
        if (instance != null) {
            Destroy (gameObject);
        } else if(instance == null) {
            instance = this;
            DontDestroyOnLoad (gameObject);
        }
    }

    void InitializeGameData() {
        LoadGameData ();

        if (gameData == null) {
            // we are running our game for the first time
            // set up initial values

            starScore = 0;
            score_Count = 0;
            selected_Index = 0;

            heroes = new bool[6];
            heroes [0] = true;

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

            gameData = new GameData ();

            gameData.StarScore = starScore;
            gameData.ScoreCount = score_Count;
            gameData.Heroes = heroes;
            gameData.SelectedIndex = selected_Index;

            SaveGameData ();

        }

    }

    public void SaveGameData() {
        FileStream file = null;

        try {

            BinaryFormatter bf = new BinaryFormatter();

            file = File.Create(Application.persistentDataPath + data_Path);

            if(gameData != null) {

                gameData.Heroes = heroes;
                gameData.StarScore = starScore;
                gameData.ScoreCount = score_Count;
                gameData.SelectedIndex = selected_Index;

                bf.Serialize(file, gameData);
            }

        } catch(Exception e) {

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

    }

    void LoadGameData() {

        FileStream file = null;

        try {

            BinaryFormatter bf = new BinaryFormatter();

            file = File.Open(Application.persistentDataPath + data_Path, FileMode.Open);

            gameData = (GameData)bf.Deserialize(file);

            if(gameData != null) {
                starScore = gameData.StarScore;
                score_Count = gameData.ScoreCount;
                heroes = gameData.Heroes;
                selected_Index = gameData.SelectedIndex;
            }

        } catch(Exception e) {

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

} // class

0 个答案:

没有答案