Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Issue saving serializable data to iPhone

Discussion in 'iOS and tvOS' started by imparare, Jul 2, 2009.

  1. imparare

    imparare

    Joined:
    Jun 24, 2008
    Posts:
    369
    Hi, I am trying to save data to the iPhone and get the following error message from the console in xcode:

    Code (csharp):
    1. This mono runtime was configured with --enable-minimal=reflection_emit, so System.Reflection.Emit is not supported.
    this is triggered by:

    Code (csharp):
    1.  
    2.  
    3. path = Application.dataPath.Substring(0, Application.dataPath.Length - 4) + "Documents";
    4. Stream fileStream = File.Open(path  + "/" + filename, FileMode.Create);
    5. BinaryFormatter formatter = new BinaryFormatter();
    6. formatter.Serialize(fileStream, obj);
    7. fileStream.Close();
    The error is triggered by the formatter.Serialize(fileStream.obj) line

    Is there a way to save serialized data to the iphone (I have no idea what the error message is telling me)

    It all works fine on the mac (no issues at all via remote)

    thanks
     
  2. Landern

    Landern

    Joined:
    Dec 21, 2008
    Posts:
    354
    iPhone Basic/Advance doesn't include all the classes that are in .net 1.1.

    This would add a lot of overhead(file size) to your project when your are not even using the namespaces.

    System.Reflection.Emit was removed from the deployment using that switch:

    --enable-minimal=reflection_emit

    So the support for anything in the Reflection.Emit namespace(methods, classes, properties, etc) goes out the door.

    Is this one of the using(includes) in your source?

    what is in obj?

    The Emit Namespace does some really advanced stuff.
     
  3. imparare

    imparare

    Joined:
    Jun 24, 2008
    Posts:
    369
    Hey WM,

    Obj is an array which is populated with a custom type.

    using System.IO;
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Formatters.Binary;

    Am now looking at another solution as I doubt if this one will be fixed (report submitted).

    thx
     
  4. Mantas-Puida

    Mantas-Puida

    Joined:
    Nov 13, 2008
    Posts:
    1,864
    Hi,
    BinaryFormatter is not supported on Unity iPhone, because internally it uses Reflection.Emit.
     
  5. imparare

    imparare

    Joined:
    Jun 24, 2008
    Posts:
    369
    Thx for the reply mantasp
     
  6. SoundGuy32

    SoundGuy32

    Joined:
    Oct 30, 2009
    Posts:
    17
    any workaround for serializing stuff in Unity
    (I have 1.7 basic)

    i'm getting the same crash.
     
  7. bernardfrancois

    bernardfrancois

    Joined:
    Oct 29, 2009
    Posts:
    373
    I just had the same error about System.Reflection.Emit, and it was caused by the following line (which I'm using in a cross platform project, to make sure floating point values are saved the same way on any locale):

    Code (csharp):
    1. System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
    I solved it by making sure it isn't compiled in Unity iPhone:

    Code (csharp):
    1.         #if !UNITY_IPHONE
    2.             // force locale to be en-US so conversions between float and string are always done the same way
    3.             System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
    4.         #endif
     
  8. SoundGuy32

    SoundGuy32

    Joined:
    Oct 30, 2009
    Posts:
    17
    and is there anything to do in unity iphone to serialize stuff with floats?
     
  9. bernardfrancois

    bernardfrancois

    Joined:
    Oct 29, 2009
    Posts:
    373
    I didn't test it, but I don't think there are similar issues on the iPhone.
     
  10. MarkVanguard

    MarkVanguard

    Joined:
    Sep 8, 2014
    Posts:
    3
    Just in case anyone stumbles on this thead, the following code fixed this issue for me:

    Code (CSharp):
    1. #if UNITY_IPHONE
    2.             // Forces a different code path in the BinaryFormatter that doesn't rely on run-time code generation (which would break on iOS).
    3.             System.Environment.SetEnvironmentVariable("MONO_REFLECTION_SERIALIZER", "yes");
    4. #endif
    Found from here.
     
    intercodegames likes this.