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 Kulgann · Mar 12, 2015 at 02:57 PM · c#iosjit

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
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 RakshithAnand · Feb 21, 2016 at 07:19 AM 0
Share

Did you find an answer for this?

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

HttpWebResponse and JIT on iOS 2 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

My Generic function using LINQ(orderBy thenBy)Not working on IOS 1 Answer

Unity Cloud Build. Player export failed.Reason: 'pod' command not found; 2 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