zrrz111

DictionarySerializer

May 16th, 2016
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.45 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System;
  5. using System.Runtime.Serialization.Formatters.Binary;
  6. using System.IO;
  7.  
  8. [Serializable]
  9. public class Columns
  10. {
  11.     public Columns(int _xPos, int _yPos, List<BlockProperties> _properties) {
  12.         xPos = _xPos;
  13.         yPos = _yPos;
  14.         properties = _properties;
  15.     }
  16.     public int xPos;
  17.     public int yPos;
  18.     public List<BlockProperties> properties = new List<BlockProperties> ();
  19. }
  20.  
  21. [Serializable]
  22. public class BlockProperties
  23. {
  24.     public BlockProperties(int _num, bool _flag) {
  25.         num = _num;
  26.         flag = _flag;
  27.     }
  28.     // Various ints and bools
  29.     public int num;
  30.     public bool flag;
  31. }
  32.  
  33. public class DictionarySerializer<TKey, TValue> : MonoBehaviour {
  34.  
  35.     public static byte[] Save(Dictionary<TKey, TValue> dictionary) {
  36.         var binFormatter = new BinaryFormatter ();
  37.         using (var mStream = new MemoryStream())
  38.         {
  39.             binFormatter.Serialize(mStream, dictionary);
  40.             return mStream.ToArray ();
  41.         }
  42.     }
  43.  
  44.     public static Dictionary<TKey, TValue> Load(byte[] bArray) {
  45.         using (var mStream = new MemoryStream())
  46.         {
  47.             var binFormatter = new BinaryFormatter();
  48.  
  49.             mStream.Write(bArray, 0, bArray.Length);
  50.             mStream.Position = 0;
  51.  
  52.             return binFormatter.Deserialize(mStream) as Dictionary<TKey, TValue>;
  53.         }
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment