序列化工具类:xml 序列化,反序列化,二进制序列化,反序列化
BinarySerializeOpt :xml 序列化,反序列化,运行时反序列化,二进制序列化,运行时反序列化
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml.Serialization;
using UnityEngine;
public class BinarySerializeOpt : MonoBehaviour
{
/// <summary>
/// 序列化:将对象转换成xml,生成xml文件
/// </summary>
/// <param name="path"></param>
/// <param name="obj"></param>
/// <returns></returns>
public static bool Xmlserialize(string path, System.Object obj)
{
try
{
using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
{
using (StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8))
{
//XmlSerializerNamespaces namespaces= new XmlSerializerNamespaces(); //可以将xml的命名空间去掉,如果有需求
//namespaces.Add(string.Empty, string.Empty);
XmlSerializer xs = new XmlSerializer(obj.GetType());
xs.Serialize(sw, obj);
}
}
}
catch (Exception e)
{
Debug.LogError("此类无法转换成xml" + obj.GetType() + "," + e);
}
return false;
}
/// <summary>
/// Xml 的反序列化
/// </summary>
/// <param name="path"></param>
/// <param name="type"></param>
/// <returns></returns>
public static System.Object XmlDeserialize(string path,Type type)
{
System.Object obj = null;
try
{
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
{
XmlSerializer xs = new XmlSerializer(type);
obj = xs.Deserialize(fs); //反序列化出来成为对象
}
}
catch (Exception e)
{
Debug.LogError("此类无法转换成xml" + obj.GetType() + "," + e);
}
return obj;
}
/// <summary>
/// xml反序化成对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="Path"></param>
/// <returns></returns>
public static T XmlDeserialize<T>(string path) where T : class
{
T t = default(T);
TextAsset textAsset = Resources.Load<TextAsset>(path); //载入xml
try
{
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
{
XmlSerializer xs = new XmlSerializer(typeof(T));
t = (T)xs.Deserialize(fs); //反序列化出来
}
}
catch (Exception e)
{
Debug.LogError("此类无法转换成二进制文件: " + path + "," + e);
}
return t;
}
/// <summary>
/// 运行时xml的写
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="Path"></param>
/// <returns></returns>
public static T XmlDeserializeRun<T>(string path) where T : class
{
T t = default(T);
TextAsset textAsset = Resources.Load<TextAsset>(path); //载入xml
if (textAsset == null)
{
UnityEngine.Debug.LogError("cant load TextAsset: " + path);
return null;
}
try
{
using (MemoryStream stream = new MemoryStream(textAsset.bytes)) //读取文件到内存
{
XmlSerializer xs = new XmlSerializer(typeof(T)); //创建xml序列化对象
t = (T)xs.Deserialize(stream); //反序列化为对象
}
//TODO资源卸载掉 之后建议使用AssetBundle
Resources.UnloadAsset(textAsset);
}
catch (Exception e)
{
Debug.LogError("load TextAsset exception: " + path + ",");
}
return t;
}
/// <summary>
/// 将对象序列化成二进制
/// </summary>
/// <param name="path"></param>
/// <param name="obj"></param>
/// <returns></returns>
public static bool BinarySerilize(string path, System.Object obj)
{
try
{
using(FileStream fs=new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, obj);
}
return false;
}
catch (Exception e)
{
Debug.LogError("此类无法转换为二进制 " + obj.GetType() + "," + e);
}
return false;
}
/// <summary>
/// 读取二进制文件
/// </summary>
/// <param name="path"></param>
/// <param name="obj"></param>
/// <returns></returns>
public static T BinaryDeserilizeRun<T>(string path)where T:class
{
T t = default(T);
TextAsset textAsset = Resources.Load<TextAsset>(path); //TODO 加载text文件
if(textAsset == null)
{
UnityEngine.Debug.LogError("cant load TextAsset: " + path);
return null;
}
try
{
using (MemoryStream stream = new MemoryStream(textAsset.bytes)) //读取文件到内存
{
BinaryFormatter bf = new BinaryFormatter(); //创建xml序列化对象
t = (T)bf.Deserialize(stream); //反序列化为对象
}
//TODO资源卸载掉 之后建议使用AssetBundle
Resources.UnloadAsset(textAsset);
}
catch(Exception e)
{
Debug.LogError("Load TextAsset exception: " + path + "," + e);
}
return t;
}
}