Search Unity

UWP BinaryFormatter alternatives?

Discussion in 'Windows' started by humweed, Dec 14, 2018.

  1. humweed

    humweed

    Joined:
    Sep 14, 2015
    Posts:
    3
    Getting an error called : Assets\HoloToolkit-Preview\SpectatorView\UnityARKitPlugin\ARKitRemote\ObjectSerializationExtension.cs(3,47): error CS0234: The type or namespace name 'Binary' does not exist in the namespace 'System.Runtime.Serialization.Formatters' (are you missing an assembly reference?)

    with the script from Mapbox

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Runtime.Serialization.Formatters.Binary;
    5. using System.IO;
    6.  
    7. namespace Utils
    8. {
    9.     //Extension class to provide serialize / deserialize methods to object.
    10.     //src: http://stackoverflow.com/questions/1446547/how-to-convert-an-object-to-a-byte-array-in-c-sharp
    11.     //NOTE: You need add [Serializable] attribute in your class to enable serialization
    12.     public static class ObjectSerializationExtension
    13.    {
    14.  
    15.        public static byte[] SerializeToByteArray(this object obj)
    16.        {
    17.            if (obj == null)
    18.            {
    19.                return null;
    20.            }
    21.            var bf = new BinaryFormatter();
    22.            using (var ms = new MemoryStream())
    23.            {
    24.                bf.Serialize(ms, obj);
    25.                return ms.ToArray();
    26.            }
    27.        }
    28.  
    29.        public static T Deserialize<T>(this byte[] byteArray) where T : class
    30.        {
    31.            if (byteArray == null)
    32.            {
    33.                return null;
    34.            }
    35.            using (var memStream = new MemoryStream())
    36.            {
    37.                var binForm = new BinaryFormatter();
    38.                 memStream.Write(byteArray, 0, byteArray.Length);
    39.                 memStream.Seek(0, SeekOrigin.Begin);
    40.                 var obj = (T)binForm.Deserialize(memStream);
    41.                 return obj;
    42.             }
    43.        }
    44.    }
    45. }
    46.  
    47.  
    is there any way to rewrite this so it is not dependent on the System.Runtime.Serialization.Formatters.Binary? sorry i'm not that great with creating scripting in general, was just hoping there was something we could use besides BinaryFormatter, already looked at trying to incorporate JSON or XML instead, just could not understand it tbh :(

    The scripting also requires us to use the .Net instead of the IL2CPP.
     
    Last edited: Dec 14, 2018
  2. humweed

    humweed

    Joined:
    Sep 14, 2015
    Posts:
    3
    We found a workaround to this error, we made a new unity project and just transferred the map from another project since we made the map [executeineditmode] we could transfer it without having to incorporate the map box package to the new unity project :) and avoid all the scripting that caused these errors.