2022-05-23

How to store a binary file in an ios app made with unity?

I'm making a game with levels, to store which level the user has reached, I used a binary file. The problem is, when I run it inside of unity, it works perfectly fine.

However, when I build it for IOS, there appears to be an error saying that my binary file has not been found:

Save file not found in /var/mobile/Containers/Data/Application/35DB4F8D-9528-4178-908F-7C3F5F7E6A69/Documents/binary.file

This is the code that writes this file:

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

public static class SaveSystem
{
    public static void SavePlayer (Player player)
    {
        BinaryFormatter formatter = new BinaryFormatter();
        string path = Application.persistentDataPath + "/binary.file";

        FileStream stream = new FileStream(path, FileMode.Create);

        PlayerData data = new PlayerData(player);

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

    }

    public static PlayerData LoadPlayer ()
    {
        string path = Application.persistentDataPath + "/binary.file";
        if (File.Exists(path))
        {
            BinaryFormatter formatter = new BinaryFormatter();
            FileStream stream = new FileStream(path, FileMode.Open);

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

            return data;


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

Does anyone know how to fix this error because I would really like to publish my game when it is finished and I won't be able to do that until this error is fixed.

Thank you in advance

Emiel



No comments:

Post a Comment