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 /
avatar image
4
Question by Russ Menapace · Oct 21, 2010 at 12:28 AM · aot

Why did my BinarySerialzer stop working?

I have been using the method described here:

http://answers.unity3d.com/questions/971/how-to-scrip-a-save-load-game-option

successfully for many months. Today, I started getting the following error when I try to write a file on the iPod:

ExecutionEngineException: Attempting to JIT compile method 'Serializer__TypeMetadata:.ctor ()' while running with --aot-only.

Any idea why this is happening? The call stack follows:

at System.Reflection.MonoCMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in :0 Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation. at System.Reflection.MonoCMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in :0 at System.Reflection.MonoCMethod.Invoke (BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in :0 at System.Reflection.ConstructorInfo.Invoke (System.Object[] parameters) [0x00000] in :0 at System.Activator.CreateInstance (System.Type type, Boolean nonPublic) [0x00000] in :0 at System.Activator.CreateInstance (System.Type type) [0x00000] in :0 at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.CreateMemberTypeMetadata (System.Type type) [0x00000] in :0 at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.GetObjectData (System.Object obj, System.Runtime.Serialization.Formatters.Binary.TypeMetadata& metadata, System.Object& data) [0x00000] in :0 at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteObject (System.IO.BinaryWriter writer, Int64 id, System.Object obj) [0x00000] in :0 at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteObjectInstance (System.IO.BinaryWriter writer, System.Object obj, Boolean isValueObject) [0x00000] in :0 at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteQueuedObjects (System.IO.BinaryWriter writer) [0x00000] in :0 at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteObjectGraph (System.IO.BinaryWriter writer, System.Object obj, System.Runtime.Remoting.Messaging.Header[] headers) [0x00000] in :0 at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize (System.IO.Stream serializationStream, System.Object graph, System.Runtime.Remoting.Messaging.Header[] headers) [0x00000] in :0 at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize (System.IO.Stream serializationStream, System.Object graph) [0x00000] in :0 at QSerializer.WriteFile[Serializer] (System.String fileName, .Serializer source) [0x00000] in :0 at Player.Write () [0x00000] in :0 at G.Save () [0x00000] in :0 at G.OnLevelWasLoaded () [0x00000] in :0

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

3 Replies

· Add your reply
  • Sort: 
avatar image
28
Best Answer

Answer by Nico de Poel · Jul 24, 2013 at 03:44 PM

We had the same problem with a bit of code that uses a BinaryFormatter to (indirectly) serialize an enum value. The BinaryFormatter (or the ObjectWriter used by it to be more precise) attempts to create its own special brand of type metadata for the value, and for custom value types (like structs or enums defined in your code) it will try to dynamically generate a class for that metadata. Which fails on iOS since it does not support run-time code generation.

Using .NET Reflector, I took a look at the serializer code in question, and found out that the ObjectWriter can use a different code path that doesn't rely on run-time code generation, but on reflection instead. And the code path that it uses is configurable with an environment variable.

Just add the following line to the Awake() function of the MonoBehaviour script that uses the BinaryFormatter:

 // Forces a different code path in the BinaryFormatter that doesn't rely on run-time code generation (which would break on iOS).
 Environment.SetEnvironmentVariable("MONO_REFLECTION_SERIALIZER", "yes");

... and the BinaryFormatter should work fine on iOS. In theory, using reflection for type metadata will be a little bit slower than using a precompiled class, but in practice you probably won't notice any difference. And it's better than not working at all.

Comment
Add comment · Show 10 · 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 SpellTowerCode · Nov 11, 2013 at 02:10 AM 0
Share

The SetEnvironmentVariable technique worked perfectly! $$anonymous$$any thanks.

avatar image arkon · Dec 12, 2013 at 12:19 PM 0
Share

You are a star! this worked for me too and saved me a ton of effort having to convert everything over to some other kind of serialisation.

avatar image Petro19 · Dec 12, 2013 at 05:10 PM 0
Share

thanks! it even works for serialization of generic in generic collections

avatar image samizzo · May 06, 2014 at 03:08 PM 1
Share

This should be the accepted answer. Thank you!

avatar image MarkVanguard · Sep 29, 2014 at 12:14 PM 0
Share

SetEnvironmentVariable worked for me when I had to call

 System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("xx-XX");

on iOS. Thanks!

Show more comments
avatar image
0

Answer by r618 · Nov 22, 2011 at 11:54 PM

not sure about this either
Is there any kind of mono stripping involved ?
If that doesn't help I'd file a bug report and/or implement ISerializer interface ( see e.g. http://forum.unity3d.com/threads/79138-I-love-strippers!-aka-Network-Errors-amp-Build-Stripping-question )
There are many problems/reports like this, I personally ended up using XMLSerializer for now since refactoring or providing custom serializer for all classes would be very tedious and I couldn't find the culprit causing JIT compiling *__TypeMetadata:.ctor () using BinaryFormatter

Comment
Add comment · 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
0

Answer by magavelis · Sep 26, 2017 at 06:29 PM

I also faced similar issue.. Solved it by changing iOS Scripting Backend setting in Player Settings to IL2CPP

Comment
Add comment · 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

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

11 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

Related Questions

Could not serialize text file - out of memory 0 Answers

Saving Data on IOS produces JIT Error in Xcode 2 Answers

How to use Reactive Extensions on iOS ? 1 Answer

Loading scripts dynamically in AoT compilers 1 Answer

Steps to determine cause of AOT Cross Compile Failed 1 Answer


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