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
0
Question by Seattlefella · Feb 21, 2013 at 08:15 PM · webplayerserializationdeserialization

Help with Binary serialization/de-serialization of List items and the WEBPLAYER

The simplified code below will successfully serialize and de-serialize (binary format) Lists and objects in the editor and stand alone games.

When run this code on a web player I receive the following error during de-serialization only:

 System.FieldAccessException: Attempt to access a private/protected field failed.

If I rework the code to serialize and de-serialize simple objects it will work on the web player.

From my reading of the mono compatibility table it seems that this should be supported on the web player.

What am I missing? does the webplayer support binary serialization/de-serialization of List items? Must I switch to another format for serialization/de-serialization?

 using UnityEngine;
 using System;
 using System.Collections.Generic;
 using System.Runtime.Serialization;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.IO;
 using System.Text;
 using System.Reflection; 
 using System.Collections; 
 
 public class SerialTestList : MonoBehaviour
 {
 
 // testing using lists
 public List<Player> Players = new List<Player>();
 public List<Player> PlayersDecoded = new List<Player>();
 
 
 public void Start()
 {
 
 // Creat some data in our simple list
    Players.Add(new Player(1, "Tom", "Jones", "SeattleFella", "A cool guy", "", 1));
    Players.Add(new Player(2, "Bob", "Smith", "NYFella", "A Hot guy", "", 2));
    Players.Add(new Player(3, "Jim", "Jones", "BostonFella", "A white guy", "", 3));
 
 // create a stream to hold the serialized data
    System.IO.MemoryStream _MemoryStream = new System.IO.MemoryStream();
 
 // Encode the data into a binary stream
 try {
 // create the binary formatter
    IFormatter formatterEncode = new BinaryFormatter();
    formatterEncode.Binder = new VersionDeserializationBinder();
 
 // serialize the collection
    formatterEncode.Serialize(_MemoryStream, Players);      
 }
 
 catch (Exception e) {
    Debug.LogError(e + "Exception caught in encoding");
 }
 
 
 try
 {
 
 // create the binary formatter
    IFormatter formatterDecode = new BinaryFormatter();
    formatterDecode.Binder = new VersionDeserializationBinder();
 
 // set memory stream position to starting point
    _MemoryStream.Position = 0;
 
 // deserialize the collection from the stream
    PlayersDecoded = (List<Player>)formatterDecode.Deserialize(_MemoryStream); // ERROR HERE
 
 }
 catch (Exception e)
 {
    Debug.LogError(e + "Exception caught in Decoding");
         }
 
 // Display the shrunk and reExpanded list in the debugger
    foreach (Player _plr in PlayersDecoded)
 {
    Debug.Log(_plr.FirstName + " " + _plr.LastName);
 
 }
 
 
 }
 
 }
 
     [System.Serializable]
     public class Player
 {
     //public string FirstName { get; set; }
     //public string LastName { get; set; }
     //public string Handle { get; set; }
     //public string About { get; set; }
     //public string AvatarURL { get; set; }
     //public int PlayerID { get; set; }
     //public int Id { get; set; }
 
     public string FirstName ;
     public string LastName ;
     public string Handle ;
     public string About;
     public string AvatarURL ;
     public int PlayerID ;
     public int Id;
     public Player(int _id = 0, string _fn = null, string _ln = null, String _hdl = null, string _abt = null, string _avaUrl = null, int _pid = 0)
     {
         this.Id = _id;
         this.FirstName = _fn;
         this.LastName = _ln;
         this.Handle = _hdl;
         this.About = _abt;
         this.AvatarURL = _avaUrl;
         this.PlayerID = _pid;
     }
 }
 
 // === This is required to guarantee a fixed serialization assembly name, which Unity likes to randomize on each compile
 // Do not change this
 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; 
             
             // The following line of code returns the type.             
             typeToDeserialize = Type.GetType(String.Format("{0}, {1}", typeName, assemblyName));
             return typeToDeserialize;       
         }       
         return null; 
     }
 }
Comment
Add comment · Show 1
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 appearance · May 25, 2013 at 03:43 PM 0
Share

I am facing the same problem. Did you find a solution/work around this ? Please share. Thanks in advance.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Seattlefella · May 25, 2013 at 04:14 PM

Yes I did get all to work.

The error comes from unity producing a differently named assembly on each build which causes problems with serialisation of contained classes.

A workaround is to define all data classes for serialisation in external assemblies.

A more complete answer can be found on this thread http://forum.unity3d.com/threads/16281-Unity-BinaryFormatter-Deserialization-Problem

Comment
Add comment · Show 1 · 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 appearance · May 28, 2013 at 08:12 AM 0
Share

thanks for your reply. I too solved my problem, not through different assemblies but by using ISerializable interface and providing GetObjectData() and serializing constructor() and handling List and Dictionary myself.

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

10 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

Related Questions

Webplayer deserialization fails 1 Answer

Serialization of Game State Into Web Server 0 Answers

EndOfStreamException : Failed to read past end of stream 1 Answer

EndOfStreamException : Failed to read past end of stream 1 Answer

Does protobuf-net work in the Unity web player? 4 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