• 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 /
avatar image
Question by DavidButtress · Jan 20, 2012 at 06:13 PM · ioslistserialize

Serialize a list on iPhone

Hey, I'm having trouble with serializing on iPhone, specifically a generic list (List(float)). I've hunted around the forums and as far as I can tell this should work. But it's throwing an exception when trying it:

     Couldn't write gameCenterCache: Exception caught: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ExecutionEngineException: Attempting to JIT compile method 'List`1__TypeMetadata:.ctor ()' while running with --aot-only.
 
   at System.Reflection.MonoCMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0 
   --- End of inner exception stack trace ---
   at System.Reflection.MonoCMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0 
   at System.Reflection.MonoCMethod.Invoke (BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0 
   at System.Reflection.ConstructorInfo.Invoke (System.Object[] parameters) [0x00000] in <filename unknown>:0 
   at System.Activator.CreateInstance (System.Type type, Boolean nonPublic) [0x00000] in <filename unknown>:0 
   at System.Activator.CreateInstance (System.Type type) [0x00000] in <filename unknown>:0 
   at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.CreateMemberTypeMetadata (System.Type type) [0x00000] in <filename unknown>:0 
   at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.GetObjectData (System.Object obj, System.Runtime.Serialization.Formatters.Binary.TypeMetadata& metadata, System.Object& data) [0x00000] in <filename unknown>:0 
   at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteObject (System.IO.BinaryWriter writer, Int64 id, System.Object obj) [0x00000] in <filename unknown>:0 
   at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteObjectInstance (System.IO.BinaryWriter writer, System.Object obj, Boolean isValueObject) [0x00000] in <filename unknown>:0 
   at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteQueuedObjects (System.IO.BinaryWriter writer) [0x00000] in <filename unknown>:0 
   at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteObjectGraph (System.IO.BinaryWriter writer, System.Object obj, System.Runtime.Remoting.Messaging.Header[] headers) [0x00000] in <filename unknown>:0 
   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize (System.IO.Stream serializationStream, System.Object graph, System.Runtime.Remoting.Messaging.Header[] headers) [0x00000] in <filename unknown>:0 
   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize (System.IO.Stream serializationStream, System.Object graph) [0x00000] in <filename unknown>:0 
   at Serializer.SerializeObject (System.String filename, .GameCenterCache gameCenterCache) [0x0001f] in /Users/teacup42729/Dropbox/development/TouchScreenTest/Assets/Wind/Scripts/IO/Serializer.cs:24 

It seems to work fine in the editor on windows and mac, the file is created and can be read back again. I've also done tests with other object types, my own custom class works fine and any base classes, and basic arrays, and also lists and dictionaries, everything works fine in editor, but Lists and dictionaries won't on the iphone.

Anyone got any ideas? Thanks!

This is my code:

 using UnityEngine;
 using System;
 using System.Collections;
 using System.Collections.Generic;
 using System.Runtime.Serialization;
 using System.Runtime.Serialization.Formatters.Binary;
 
 [Serializable()]
 public class GameCenterCache : ISerializable
 {
     GameCenterAchievementSerial single;
     List<float> testList;
     float[] testArray;
 
     public GameCenterCache()
     {
         single = new GameCenterAchievementSerial("woop", 56f);
         testList = new List<float>();
         testList.Add(43.4f);
         testList.Add(23.3f);
         testArray = new float[]{4f, 2f, 9f};
     }
 
     public GameCenterCache(SerializationInfo info, StreamingContext context)
     {
         single = (GameCenterAchievementSerial)info.GetValue("single", typeof(GameCenterAchievementSerial));
         testList = (List<float>)info.GetValue("testList", typeof(List<float>));
         testArray = (float[])info.GetValue("testArray", typeof(float[]));
     }
 
     public void GetObjectData(SerializationInfo info, StreamingContext context)
     {
         info.AddValue("single", single);
         info.AddValue("testList", testList);
         info.AddValue("testArray", testArray);
     }
 }


and

 using System.IO;
 using System.Runtime.Serialization;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.Collections;
 using System;
 using System.Reflection;
 
 public class Serializer
 {
     public Serializer()
     {
 
     }
 
     public void SerializeObject(string filename, GameCenterCache gameCenterCache)
     {
         Debug.Log(filename);
 
         try
         {
             Stream stream = File.Open(filename, FileMode.Create);
             BinaryFormatter bFormatter = new BinaryFormatter();
             bFormatter.Binder = new VersionDeserializationBinder();
             bFormatter.Serialize(stream, gameCenterCache);
             stream.Close();
         }
         catch (Exception e)
         {
             Debug.LogWarning("Couldn't write gameCenterCache: Exception caught: " + e);
         }
     }
 
     public GameCenterCache DeSerializeObject(string filename)
     {
         Debug.Log(filename);
         
         try
         {
             Stream stream = File.OpenRead(filename);
             BinaryFormatter bFormatter = new BinaryFormatter();
             bFormatter.Binder = new VersionDeserializationBinder();
             GameCenterCache gameCenterCache = (GameCenterCache)bFormatter.Deserialize(stream);
 
             stream.Close();
             return gameCenterCache;
         }
         catch (Exception e)
         {
             Debug.LogWarning("Couldn't read gameCenterCache: Exception caught: " + e);
             return new GameCenterCache();
         }
     }
     
 }
 
 public sealed class VersionDeserializationBinder : SerializationBinder
 {
     public override Type BindToType( string assemblyName, string typeName)
     {
         if ( !string.IsNullOrEmpty(assemblyName) && !string.IsNullOrEmpty(typeName))
         {
             Type typeToDeserialize = null;
             assemblyName = Assembly.GetExecutingAssembly().FullName;
             
             typeToDeserialize = Type.GetType(String.Format("{0}, {1}", typeName, assemblyName));
             
             return typeToDeserialize;
         }
         return null;
     }
 }
Comment
haim96
baumak

People who like this

2 Show 2
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 bzvestey · Apr 10, 2012 at 09:53 PM 0
Share

I am seeing this same issue, I am interested to see if anyone has figured out a fix for this yet.

avatar image DavidButtress · Apr 10, 2012 at 09:59 PM 0
Share

I never found a real solution to this, I ended up creating functions that converted my lists and dictionaries into basic arrays. So when I want to save I convert to array, and when I load I convert back into dictionaries/lists. This works fine for me at the moment, but I wish I had a proper solution.

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by antonholmquist · Dec 03, 2013 at 08:04 PM

This issue can be fixed by adding this line before doing any serialization:

``` Environment.SetEnvironmentVariable("MONO_REFLECTION_SERIALIZER", "yes"); ```

Comment
haim96
baumak

People who like this

2 Show 3 · 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 grzunov · Jun 14, 2014 at 03:33 PM 0
Share

You sir are a god, thanx, that's exactly what I needed

avatar image NobleAnarchist · Jan 15, 2015 at 07:33 PM 0
Share

Holy shmokes, I wouldn't have believed it if I didn't see it with my own eyes!

Good knowledge, antonholmquist.

avatar image baumak · Jan 26, 2016 at 10:08 AM 0
Share

Thank you!!! It was very helpful)))

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

7 People are following this question.

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

Related Questions

A node in a childnode? 1 Answer

BinaryFormatter - saving and loading a list containing sprites. 0 Answers

Allow users to input a list? 0 Answers

Serialize on iOS , that is AOT safe. 2 Answers

C# list bug using addRange after call toString method only on unity ios 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