- Home /
Serialization of Dictionary> in Unity 4,6
I have this Dictionary that I want to serialize with a BinaryFormatter. I have already used this line in the Awake method of my Singleton : System.Environment.SetEnvironmentVariable("MONO_REFLECTION_SERIALIZER", "yes"); This allowed me to serialzie every other object that I have in my code. But when I added a Dictionary yesterday the code stopped working and I get a ExecitonEngineException: Attempting to JIT compile method 'System.Collections.Generic.Dictionary'. This is my entire code:
 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.IO;
 using System.Security;
 using System.Security.Cryptography;
 using System.Text;
 using System.Runtime.InteropServices;
 using UnityEngine.UI;
 public class DataHolder : MonoBehaviour {
     public static DataHolder holder;
     public string selectedChar;
     public RectTransform mapTransform;
     public int coinAmount, upgradeLevel ;
     public int levelIndex;
     private Vector2 tempVect;
     public float spawnTime;
     public Level_Data[] level_data;
    // public UpgradeData[] upgrade_data;
     public CharacterUpgradeList charUpgradeList;
     private GameObject[] level_plates;
     private GameObject[] upgrade_plates;
 
 
     void OnLevelWasLoaded(int level)
     {
         if (level == 0)
             mapTransform = GameObject.FindGameObjectWithTag("MapHolder").GetComponent<RectTransform>();
     }
  
     void Update()
     {
         if (Application.platform == RuntimePlatform.Android)
         {
             int i = 0;
             if (Input.GetKeyDown(KeyCode.Escape))
                 i++;// maybe display  a msg here for android users that they need to press back again to exit.
             if (i == 2)
                 Application.Quit();
         }
         if(Application.loadedLevel == 0 && mapTransform !=null)
         {
         tempVect = mapTransform.anchoredPosition;
         }
     
     }
     void OnEnable()
     {
         System.Environment.SetEnvironmentVariable("MONO_REFLECTION_SERIALIZER", "yes");
        if (mapTransform == null && Application.loadedLevel == 0)
         {
             mapTransform = GameObject.FindGameObjectWithTag("MapHolder").GetComponent<RectTransform>();
         }
         if (File.Exists(Application.persistentDataPath + "/savedgame.dat"))
         {
             GameData data = new GameData();
             BinaryFormatter binFormatter = new BinaryFormatter();
             string filename = Application.persistentDataPath + "/savedgame.dat";
             FileStream file = File.Open(filename, FileMode.Open,FileAccess.Read,FileShare.ReadWrite);
             data = (GameData)binFormatter.Deserialize(file) ;
             file.Close();
             for (int i=0; i < data._level_Data.Length; i++)
             {
                 level_data[i] = data._level_Data[i];
 
             }
             //for (int i = 0; i < data._upgrade_Data.Length; i++)
             //{
             //    upgrade_data[i] = data._upgrade_Data[i];
             //}
             foreach (KeyValuePair<string, List<UpgradeData>> entry in data._charUpgradeList.upgradeList)
             {
                 for (int i = 0; i < data._charUpgradeList.upgradeList[entry.Key].Count; i++)
                 {
                     charUpgradeList.upgradeList[entry.Key][i] = entry.Value[i];
                 }
             }
             mapTransform.anchoredPosition =data.serialVector.returnVector2();
             coinAmount = data._coinAmount;
             upgradeLevel = data._upgradeLevel;
             
             
         }
 
     }
     void Awake()
     {
         selectedChar = "Man";
         InitiliazeLevelDataArray();
         //InitiliazeUpgradeDataArray();
         upgradeLevel = -1;
         Application.runInBackground = false;
         if (Application.platform == RuntimePlatform.IPhonePlayer)
         {
             System.Environment.SetEnvironmentVariable("MONO_REFLECTION_SERIALIZER", "yes");
         }
         if (holder == null)
         {
             DontDestroyOnLoad(gameObject);
             holder = this;
         }
         else if (holder != this)
         {
             Destroy(gameObject);
         }
 
     }
     void OnDisable()
     {
 
         
         BinaryFormatter binFormatter = new BinaryFormatter();
         string filename = Application.persistentDataPath + "/savedgame.dat";
         FileStream file = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
 
 
         GameData data = new GameData();
         data._level_Data = new Level_Data[level_data.Length];
       //  data._upgrade_Data = new UpgradeData[upgrade_data.Length];
         for (int i = 0; i < level_data.Length; i++)
         {
             data._level_Data[i] = level_data[i];
         }
         //for (int i = 0; i < upgrade_data.Length; i++)
         //{
         //    data._upgrade_Data[i] = upgrade_data[i];
         //    if (upgrade_data[i].purchased == true)
         //        upgradeLevel = i;
             
         //}
         foreach(KeyValuePair<string , List<UpgradeData>> entry in  charUpgradeList.upgradeList)
         {
             for (int i = 0; i < charUpgradeList.upgradeList[entry.Key].Count; i++ )
             {
                 data._charUpgradeList.upgradeList[entry.Key][i] = entry.Value[i];
 
             }
         }
         data._coinAmount = coinAmount;
         data.serialVector = new SerialVector2(tempVect);
         data._upgradeLevel = upgradeLevel;
        
         binFormatter.Serialize(file, data);
         file.Close();
     }
    
     void OnApplicationPause(bool pauseStatus)
     {
 
         BinaryFormatter binFormatter = new BinaryFormatter();
         string filename = Application.persistentDataPath + "/savedgame.dat";
         FileStream file = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
 
 
         GameData data = new GameData();
         data._level_Data = new Level_Data[level_data.Length];
       //  data._upgrade_Data = new UpgradeData[upgrade_data.Length];
         for (int i = 0; i < level_data.Length; i++)
         {
             data._level_Data[i] = level_data[i];
         }
         //for (int i = 0; i < upgrade_data.Length; i++)
         //{
         //    data._upgrade_Data[i] = upgrade_data[i];
         //    if (upgrade_data[i].purchased == true)
         //        upgradeLevel = i ;
          
         //}
         foreach (KeyValuePair<string, List<UpgradeData>> entry in charUpgradeList.upgradeList)
         {
             for (int i = 0; i < charUpgradeList.upgradeList[entry.Key].Count; i++)
             {
                 data._charUpgradeList.upgradeList[entry.Key][i] = entry.Value[i];
 
             }
         }
         data._coinAmount = coinAmount;
         data._upgradeLevel = upgradeLevel;
         data.serialVector = new SerialVector2(tempVect);
         binFormatter.Serialize(file, data);
         file.Close();
     }
 
 
     private void InitiliazeLevelDataArray()
     {
         bool flag = false;
         if (flag == false)
         {
             level_plates = GameObject.FindGameObjectsWithTag("Level_Tag");
             level_data = new Level_Data[level_plates.Length];
             for (int i = 0; i < level_plates.Length; i++)
             {
                 level_data[i] = new Level_Data();
             }
             flag = true;
             try
             {
                 if (level_data[0].completion_status == 0)
                     level_data[0].completion_status = 2;
             }
             catch (IndexOutOfRangeException ) { }
         }
        
     }
 
     //private void InitiliazeUpgradeDataArray()
     //{
     //   bool flag = false;
     //   if (flag == false)
     //   {
     //       flag = true;
     //       upgrade_plates = GameObject.FindGameObjectsWithTag("Upgrade_Tag");
     //       upgrade_data = new UpgradeData[upgrade_plates.Length];
     //       for (int i = 0; i < upgrade_plates.Length; i++)
     //       {
     //           upgrade_data[i] = new UpgradeData();
     //       }
     //   }
     //}
 
 
 
     
 }
 
 [Serializable]
 class GameData
 {
     public int _coinAmount, _upgradeLevel;
     public Level_Data[] _level_Data;
     public CharacterUpgradeList _charUpgradeList;
     public SerialVector2 serialVector;
 
     public GameData()
     {
         _charUpgradeList = new CharacterUpgradeList(); 
     }
 
 }
  [Serializable]
     public class CharacterUpgradeList
     {
 
         private UpgradeData[] _upgrade_Data;
         private List<UpgradeData>[] upgData;
         
         public Dictionary<string, List<UpgradeData>> upgradeList;
         public CharacterUpgradeList()
         {
             upgData = new List<UpgradeData>[4];
             for (int i = 0; i < upgData.Length; i++)
             {
                 upgData[i] = new List<UpgradeData> { 
                         new UpgradeData(),
                         new UpgradeData(),
                         new UpgradeData(),
                         new UpgradeData(),
                         new UpgradeData(),
                         new UpgradeData()
                    };
             }
 
             upgradeList = new Dictionary<string, List<UpgradeData>>
         {
             {"Man",upgData[0]},
             {"Woman",upgData[1]},
             {"Boy",upgData[2]},
             {"Girl",upgData[3]}
             
         };
         }
     }
 
 
 [Serializable]
 public class Level_Data
 {
    public int completion_status;
    public int star_Rating;
 }
 [Serializable]
 public class UpgradeData
 {
    public bool lockStatus;
    public bool purchased;
  
 }
 [Serializable]
 public struct SerialVector2
 {
     public float x;
     public float y;
     public SerialVector2(Vector2 vect)
     {
         x = vect.x;
         y = vect.y;
     }
     public Vector2 returnVector2()
     {
     return new Vector2(x,y);
     }
 }
               Comment
              
 
               
              Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                