Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by Cazual · Mar 31, 2014 at 06:07 AM · mobilesavedataserializable

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 );
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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:

  1. The serializer usage of the binary - I have encountered problems with that in the past.

  2. 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 ();
 }





Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image magonicolas · Jan 06, 2015 at 04:36 PM 0
Share

@CorwinOfAmberX IF im doing a development build and don't have an app name how Can I do it?

Thanks,

avatar image CorwinOfAmberX · Jan 06, 2015 at 05:19 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Error when loading saved date : SerializationException: End of Stream encountered before parsing was completed. 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges