Hello,
Can’t find any good source to solve my problem. On my mac I can load and save to the xml file in StreamingAssets folder. But when I build the file is unauthorized. I can make a new one and start from there but I would like to load in data from the xml file when the app first starts.
CODE:
void Start () {
//DESKTOP
if(SystemInfo.deviceType == DeviceType.Desktop){
XmlSerializer serializer = new XmlSerializer(typeof(ItemDatabase));
FileStream stream = new FileStream (Application.dataPath + "/StreamingAssets/Xml/item_data.xml", FileMode.Open);
itemDB = serializer.Deserialize (stream) as ItemDatabase;
stream.Close ();
}
//HANDHELD
else if(SystemInfo.deviceType == DeviceType.Handheld){
XmlSerializer serializer = new XmlSerializer(typeof(ItemDatabase));
FileStream stream = new FileStream (Application.dataPath + "/Raw/Xml/item_data.xml", FileMode.Open);
itemDB = serializer.Deserialize (stream) as ItemDatabase;
stream.Close ();
}
ERROR in Xcode:
UnauthorizedAccessException: Access to the path "/var/containers/Bundle/Application/1A86B856-950E-4501-A033-4E202BF0AF02/test.app/Data/Raw/Xml/item_data.xml" is denied
SOLVED:
First I check if the file exists in the StreamingAssets folder. If it doesn’t, open the file with “File.OpenRead”. After this point if the user adds an item by pressing a button and saving it to a new file (Application.persistentDataPath + “/item_data.xml”) in the StreamingAssets folder. From that point the file exists when it starts and the data is read from the StreamingAssets folder where it doesn’t have permission issues anymore.
If its overkill please let me know to clean up the code. Thanks
//Check if file exists
FileInfo info = new FileInfo(Application.persistentDataPath + "/item_data.xml");
if (info == null || info.Exists == false) {
//DESKTOP
if (SystemInfo.deviceType == DeviceType.Desktop) {
XmlSerializer serializer = new XmlSerializer (typeof(ItemDatabase));
FileStream stream = new FileStream (Application.dataPath + "/StreamingAssets/Xml/item_data.xml", FileMode.Open);
itemDB = serializer.Deserialize (stream) as ItemDatabase;
stream.Close ();
}
//HANDHELD
else if (SystemInfo.deviceType == DeviceType.Handheld) {
XmlSerializer serializer = new XmlSerializer (typeof(ItemDatabase));
//FileStream stream = new FileStream (Application.dataPath + "/Raw/Xml/item_data.xml", FileMode.Open);
FileStream stream = File.OpenRead (Application.dataPath + "/Raw/Xml/item_data.xml");
itemDB = serializer.Deserialize (stream) as ItemDatabase;
stream.Close ();
}
} else {
//DESKTOP
if (SystemInfo.deviceType == DeviceType.Desktop) {
XmlSerializer serializer = new XmlSerializer (typeof(ItemDatabase));
FileStream stream = new FileStream (Application.dataPath + "/StreamingAssets/Xml/item_data.xml", FileMode.Open);
itemDB = serializer.Deserialize (stream) as ItemDatabase;
stream.Close ();
}
//HANDHELD
else if (SystemInfo.deviceType == DeviceType.Handheld) {
XmlSerializer serializer = new XmlSerializer (typeof(ItemDatabase));
FileStream stream = new FileStream (Application.persistentDataPath + "/item_data.xml", FileMode.Open);
itemDB = serializer.Deserialize (stream) as ItemDatabase;
stream.Close ();
}
}