为什么我的保存系统无法在Android上运行?

时间:2019-12-24 14:08:48

标签: c# android unity3d

我创建了一个保存游戏进度的代码。在Unity中,它可以完美运行-在指定的文件夹中创建保存文件,但是当我将应用程序导入Android时,它不起作用。有什么线索吗?

这是我第一次进行保存/加载,因此可能有点麻烦。别对我太苛刻。

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

public static class SaveSystem
{
    public static void Save(Helper help)
    {
        BinaryFormatter formatter = new BinaryFormatter();
        string path = Application.persistentDataPath + "/savefile.gut";
        FileStream stream = new FileStream(path, FileMode.Create);

        SaveState data = new SaveState(help);

        formatter.Serialize(stream, data);
        stream.Close();
    }

    public static SaveState Load()
    {
        string path = Application.persistentDataPath + "/savefile.gut";
        if (File.Exists(path))
        {
            BinaryFormatter formatter = new BinaryFormatter();
            FileStream stream = new FileStream(path, FileMode.Open);

            SaveState data = formatter.Deserialize(stream) as SaveState;
            stream.Close();

            return data;
        }
        else
        {
            Debug.LogError("Save file not found in " + path);
            return null;
        }
    }

}
[System.Serializable]
public class SaveState
{
    public long[] points;
    public int[] level;
    public long[][] cost;
    public long[][] costAdd;

    public SaveState(Helper all)
    {
        points = new long[all.points.Length];
            points = all.points;

        level = new int[all.levelButton.Length];
        cost = new long[all.clickable.Length][];
        costAdd = new long[all.clickable.Length][];

        for (int i = 0; i < all.clickable.Length; i++)
        {
            level[i] = all.clickable[i].level;
            cost[i] = all.levelButton[i].cost;
            costAdd[i] = all.levelButton[i].costAdd;
        }
    }
}

enter image description here

1 个答案:

答案 0 :(得分:0)

我建议您将其另存为JSON对象,这将为您提供帮助

https://learn.unity.com/tutorial/live-session-quiz-game-2#5c7f8528edbc2a002053b637