- Home /
Question by
manuelmangual · Aug 23, 2016 at 07:40 AM ·
c#jsonfile-ioserializable
Issues saving file in iOS
I am getting this issue when I try to save my game in iOS
UnauthorizedAccessException: Access to the path "/var/mobile/Containers/Bundle/Application/E5970DAC-7463-4F12-A96B-1A9D524F3459/Documents" is denied.
at GameData.CreateFile () [0x00000] in <filename unknown>:0
at GameData.Save (Int32 _collectibleCount, Int32 _starCount, Int32 _minutes, Boolean _isLocked) [0x00000] in <filename unknown>:0
at UnityEngine.Events.UnityAction.Invoke () [0x00000] in <filename unknown>:0
at UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) [0x00000] in <filename unknown>:0
at MenuItmBehavior+<WaitForAnimation>c__Iterator13.MoveNext () [0x00000] in <filename unknown>:0
at UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) [0x00000] in <filename unknown>:0
(Filename: currently not available on il2cpp Line: -1)
I have no idea why I am getting this. Here is the code I am using to save/load my file:
using UnityEngine;
using System;
using System.IO;
using UnityEngine.SceneManagement;
using System.Collections.Generic;
public class GameData: MonoBehaviour
{
private static GameData _instance;
public static GameData Instance
{
get
{
if (_instance == null)
{
var go = new GameObject("GameData");
_instance = go.AddComponent<GameData>();
DontDestroyOnLoad(go);
}
return _instance;
}
}
string _FileLocation;
LevelCompleted _levelData;
void Awake()
{
#if UNITY_IPHONE
string fileNameBase = Application.dataPath.Substring(0, Application.dataPath.LastIndexOf('/'));
_FileLocation = fileNameBase.Substring(0, fileNameBase.LastIndexOf('/')) + "/Documents/" + "SaveData.json";
Debug.Log(_FileLocation);
#elif UNITY_ANDROID
_FileLocation = Application.persistentDataPath + "/" + ""SaveData.json"";
#else
_FileLocation = Application.dataPath + "/" + "SaveData.json";
#endif
// we need soemthing to store the information into
_levelData = new LevelCompleted();
}
public void Load()
{
// notice how I use a reference to type (UserData) here, you need this
// so that the returned object is converted into the correct type
_levelData = LoadData();
}
public void Save(int _collectibleCount, int _starCount, int _minutes, bool _isLocked)
{
var level = new Level();
_levelData.Level.Add(level);
level.LevelName = SceneManager.GetActiveScene().name;
level.CollectibleCount = _collectibleCount;
level.GoldStars = _starCount;
level.Minutes = _minutes;
level.NextLevelLocked = _isLocked;
level.lastLevelPlayed++;
// This is the final resulting JSON from the serialization process
CreateFile();
}
// Finally our save and load methods for the file itself
void CreateFile()
{
if (!Directory.Exists(Path.GetDirectoryName(_FileLocation)))
Directory.CreateDirectory(Path.GetDirectoryName(_FileLocation));
if (File.Exists(_FileLocation))
{
File.Delete(_FileLocation);
}
File.WriteAllText(_FileLocation, JsonUtility.ToJson(_levelData, true));
Debug.Log("File written.");
}
public LevelCompleted LoadData()
{
if (File.Exists(_FileLocation))
{
var tr = File.ReadAllText(_FileLocation);
var jsonObj = JsonUtility.FromJson<LevelCompleted>(tr);
return jsonObj;
}
return null;
}
}
[Serializable]
public class PlayerSkins
{
public string name;
public bool unlocked;
}
[Serializable]
public class LevelCompleted
{
public List<Level> Level = new List<Level>();
}
[Serializable]
public class Level
{
public string LevelName;
public int CollectibleCount;
public int GoldStars;
public int Minutes;
public bool NextLevelLocked;
public int lastLevelPlayed;
}
Comment