1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using System;
- using System.Runtime.Serialization;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.IO;
- public class CommonManager : MonoSingleton<AudioManager>
- {
- public static string Serialize<T>(T obj)
- {
- try
- {
- IFormatter formatter = new BinaryFormatter();
- MemoryStream stream = new MemoryStream();
- formatter.Serialize(stream, obj);
- stream.Position = 0;
- byte[] buffer = new byte[stream.Length];
- stream.Read(buffer, 0, buffer.Length);
- stream.Flush();
- stream.Close();
- return System.Convert.ToBase64String(buffer);
- }
- catch (System.Exception ex)
- {
- throw new System.Exception("序列化失败,原因:" + ex.Message);
- }
- }
- public static T Desrialize<T>(T obj, string str)
- {
- try
- {
- obj = default(T);
- IFormatter formatter = new BinaryFormatter();
- byte[] buffer = System.Convert.FromBase64String(str);
- MemoryStream stream = new MemoryStream(buffer);
- obj = (T)formatter.Deserialize(stream);
- stream.Flush();
- stream.Close();
- }
- catch (System.Exception ex)
- {
- throw new System.Exception("反序列化失败,原因:" + ex.Message);
- }
- return obj;
- }
- }
|