- Home /
IOS Serialization How To
I have the following code and this works on all platforms except iOS, does anyone know how I can get the list of classes stored in iOS in a similar fashion as below?
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
public class SettingsControl : MonoBehaviour {
public static SettingsControl control;
[System.Serializable]
public class Setting{
public string Language;
public string defaultlanguage;
public string Omschrijving;
public string Actievoor;
public string Zoeken;
public string NoDesription;
public string panel2;
public string panel3;
public string panel4;
public string panel5;
public string disclaimer;
public string twoagree;
}
[System.Serializable]
public class ListContainer
{
public List<Setting> settings;
}
public List<Setting> settings=new List<Setting>();
void Awake () {
// there can be only ONE ! across scenes
if (control == null)
{
DontDestroyOnLoad(gameObject);
control = this;
}
else if(control != this)
{
Destroy(gameObject);
}
}
public bool FirstTime()
{
if (File.Exists (Application.persistentDataPath + "/SettingsData.dat"))
{
return false;
}
else
{
return true;
}
}
public void Save()
{
// Initialize
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/SettingsData.dat");
ListContainer lc=new ListContainer();
lc.settings=settings;
//UnitySerializer.SerializeToFile (lc.settings,Application.persistentDataPath + "/SettingsData.dat");
// Write Data
bf.Serialize(file,lc.settings);
file.Close ();
}
public void Load()
{
if (File.Exists(Application.persistentDataPath + "/SettingsData.dat"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open (Application.persistentDataPath + "/SettingsData.dat", FileMode.Open);
settings = (List<Setting>)bf.Deserialize(file);
file.Close();
//settings = (List<Setting>)UnitySerializer.DeserializeFromFile (Application.persistentDataPath + "/SettingsData.dat");
//Debug.Log("Data Loaded");
}
else
{
//Debug.Log("File not found Data not loaded");
}
}
public bool SetAllDefaultLanguage(string sSelectedLanguage)
{
//Debug.Log("SetAllDefaultLanguage");
foreach (Setting s in settings)
{
s.defaultlanguage = sSelectedLanguage;
}
//(from s in settings where s.defaultlanguage == "*" select s).First().defaultlanguage = sSelectedLanguage;
return true;
}
public bool SettingExists(string Language)
{
return settings.Exists(myObj => myObj.Language == Language );
}
}
Answer by CorwinOfAmberX · Aug 06, 2014 at 02:08 PM
Changing my answer from before it seems that your approach with the path is correct.
This leaves two suspects that I can see:
The serializer usage of the binary - I have encountered problems with that in the past.
The reflection of the list of structures.
I am sorry I cannot help more - I attach below a simple code that works for me with a single structure:
public void SaveXML()
{
string localfileLocation = GetFilePathAndName();
// Initialize
Debug.Log("-------------------------------- About to Save Data File (" + localfileLocation + ") --------------------------------");
XmlSerializer serializer = new XmlSerializer(typeof(ApplicationData));
// Debug.Log("-------------------------------- About to Create Data File --------------------------------");
// FileStream file = File.Create( localfileLocation );
FileStream file = new FileStream( localfileLocation, FileMode.Create );
// Write Data
// Debug.Log("-------------------------------- About to Serialize Data File --------------------------------");
serializer.Serialize( file, ApplicationSavedData );
file.Close ();
}
@CorwinOfAmberX IF im doing a development build and don't have an app name how Can I do it?
Thanks,
Hey there,
I want to change my answer because following Apple guidelines and with Unity latest releases our friend is doing the right thing and the problem probably lies with Unity being very peculiar with the reflection of list of structures when saving / reading. That you be my guess and I will check it by following the Unity log on the XCode when running on the device.
I also add here a piece of code that I use as the utilty save of the settings file - that will help you:
protected string GetFilePathAndName( string baseFileName ) { string Prefix;
if UNITY_EDITOR
Prefix = "Editor-";
elif UNITY_STANDALONE_WIN
Prefix = "Windows-";
elif UNITY_IPHONE
Prefix = "iOS-";
endif
string AppPath = Application.persistentDataPath; //Environment.GetFolderPath (Environment.SpecialFolder.$$anonymous$$yDocuments);
string FileLocation = AppPath + "/" + Prefix + baseFileName;
Debug.Log ("====================================================================");
Debug.Log ("++++ AppPersistantPath: [" + AppPath + "]");
Debug.Log ("++++ FileLocation: [" + FileLocation + "]");
Debug.Log ("====================================================================");
return FileLocation;
}
Finally - with this way you do not need to know your App name (which was a hack) - however, you surely do have an App Name - this is the Apple unique id of your application.
All the best and happy new year
Your answer
Follow this Question
Related Questions
How to create an XML file in Unity 1 Answer
how to change Application.persistentDataPath? 1 Answer
Serializer.Resume() not working for multiple levels 0 Answers
Save and Load Function 2 Answers