对象与二进制转换脚本
public class BinarySaveLoadCtrl {
/// <summary>
/// 将对象转换为byte数组
/// </summary>
/// <param name="obj">被转换对象</param>
/// <returns>转换后byte数组</returns>
public static byte[] Object2Bytes(object obj) {
byte[] buff;
using (MemoryStream ms = new MemoryStream()) {
IFormatter iFormatter = new BinaryFormatter();
iFormatter.Serialize(ms, obj);
buff = ms.GetBuffer();
}
return buff;
}
/// <summary>
/// 将byte数组转换成对象
/// </summary>
/// <param name="buff">被转换byte数组</param>
/// <returns>转换完成后的对象</returns>
public static object Bytes2Object(byte[] buff) {
object obj;
using (MemoryStream ms = new MemoryStream(buff)) {
IFormatter iFormatter = new BinaryFormatter();
obj = iFormatter.Deserialize(ms);
}
return obj;
}
/// <summary>
/// 将文件转换为byte数组
/// </summary>
/// <param name="path">文件地址</param>
/// <returns>转换后的byte数组</returns>
public static byte[] File2Bytes(string path) {
if (!File.Exists(path)) {
return new byte[0];
}
FileInfo fi = new FileInfo(path);
byte[] buff = new byte[fi.Length];
FileStream fs = fi.OpenRead();
fs.Read(buff, 0, Convert.ToInt32(fs.Length));
fs.Close();
return buff;
}
/// <summary>
/// 将byte数组转换为文件并保存到指定地址
/// </summary>
/// <param name="buff">byte数组</param>
/// <param name="savepath">保存地址</param>
public static void Bytes2File(byte[] buff, string savepath) {
if (File.Exists(savepath)) {
File.Delete(savepath);
}
FileStream fs = new FileStream(savepath, FileMode.CreateNew);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(buff, 0, buff.Length);
bw.Close();
fs.Close();
}
/// <summary>
/// 将对象数据保存为二进制文件
/// </summary>
/// <typeparam name="T">类对象</typeparam>
/// <param name="obj">对象</param>
/// <param name="path">路径</param>
public static void SaveData<T>(T obj, string path) {
FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, obj);
fs.Close();
}
/// <summary>
/// 加载二进制文件返回类对象
/// </summary>
/// <typeparam name="T">类对象</typeparam>
/// <param name="path">路径</param>
/// <returns></returns>
public static T LoadData<T>(string path) {
if (File.Exists(path)) {
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
if (fs.Length != 0) {
BinaryFormatter bf = new BinaryFormatter();
T res = (T)bf.Deserialize(fs);
fs.Close();
return res;
}
fs.Close();
return default(T);
}
return default(T);
}
}
类
[Serializable]
public class TestClass {
public string name; //变量
public char boy;
public int age;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="paraA"></param>
/// <param name="paraB"></param>
/// <param name="paraC"></param>
public TestStructure(string paraA, char paraB, int paraC) {
this.name= paraA;
this.boy= paraB;
this.age= paraC;
}
/// <summary>
/// 输出变量内容
/// </summary>
/// <returns></returns>
public string DisplayInfo() {
return string.Format("name:{0};boy:{1};age:{2}", this.name, this.boy, this.age);
}
}
测试
private void Start() {
TestClass tsA = new TestClass ("1234", '5', 6);
BinarySaveLoadCtrl.SaveData<TestClass >(tsA, Application.streamingAssetsPath + "/Structure.byte");//Structure.dat //Structure.bin
TestClass testStructure= BinarySaveLoadCtrl.LoadData<TestClass>(Application.streamingAssetsPath + "/Structure.byte");//Structure.dat //Structure.bin
Debug.Log(testStructure.A);
}