https://msdn.microsoft.com/zh-cn/library/System.IO(v=vs.110).aspx
using UnityEngine;
using UnityEditor;using System.Collections;
using System.IO;
using System;
using System.Text;
public class XmlToBinary : EditorWindow
{
private ArrayList readXml;
private byte[] readXmlByte;
private string suffixXml = ".xml";
private string suffixBytes = ".Bytes";
//suffix后缀
private string localPath = Application.dataPath + "/StreamingAssets" + "/";
private string convertToLocalPath = Application.dataPath + "/StreamingAssets" + "/BinaryConfig" + "/";
private byte[] xmlContent;
[MenuItem ("ConvertTools/XmlToBinary")]
public static void ShowWindow ()
{
EditorWindow.GetWindow (typeof(XmlToBinary));
}
void OnGUI ()
{
GUILayout.Label ("输入转换XMl路径:", EditorStyles.boldLabel);
localPath = EditorGUILayout.TextField ("XML文件名:", localPath);
if (GUILayout.Button ("读取XML内容", GUILayout.Width (100))) {
// readXml = new ArrayList ();
// readXml = LoadFile (localPath);
readXmlByte = LoadFile (localPath + suffixXml);
Debug.Log ("************读取XML内容完成!");
}
GUILayout.Label ("转换成.Bytes后路径:", EditorStyles.boldLabel);
convertToLocalPath = EditorGUILayout.TextField ("Bytes输入文件名:", convertToLocalPath);
if (GUILayout.Button ("转换成.Bytes", GUILayout.Width (100))) {
CreatBinaryFile (readXmlByte, convertToLocalPath + suffixBytes);
Debug.Log ("************生成Binary文件!");
}
AssetDatabase.Refresh ();
}
/**
* path:读取文件的路径
*/
byte[] LoadFile (string path)
{
//使用流的形式读取
FileStream fs = null;
BinaryReader br = null;
try {
fs = new FileStream (path, FileMode.Open);
xmlContent = new byte[fs.Length];
br = new BinaryReader(fs);
br.Read(xmlContent, 0, xmlContent.Length);
} catch (Exception e) {
//路径与名称未找到文件则直接返回空
return null;
}
//关闭流
fs.Close ();
//销毁流
fs.Dispose ();
br.Close ();
//将数组链表容器返回
return xmlContent;
}
void CreatBinaryFile (byte[] list, string path)
{
//文件流信息
FileStream t = null;
BinaryWriter bw = null;
try {
t = new FileStream (path, FileMode.Create);
bw = new BinaryWriter(t);
bw.Write(list, 0, list.Length);
} catch (Exception e) {
//路径与名称未找到文件则直接返回空
return;
}
//关闭流
t.Close ();
//销毁流
t.Dispose ();
bw.Close ();
AssetDatabase.Refresh ();
}
// ArrayList LoadFile(string path)
// {
// //使用流的形式读取
// StreamReader sr =null;
// try{
// sr = File.OpenText(path + suffixXml);
//
// }catch(Exception e)
// {
// //路径与名称未找到文件则直接返回空
// return null;
// }
// string line;
// ArrayList arrlist = new ArrayList();
// while ((line = sr.ReadLine()) != null)
// {
// //一行一行的读取
// //将每一行的内容存入数组链表容器中
// arrlist.Add(line);
// }
// //关闭流
// sr.Close();
// //销毁流
// sr.Dispose();
// //将数组链表容器返回
// return arrlist;
// }
// void CreatBinaryFile(ArrayList list,string path){
// //文件流信息
// StreamWriter sw;
// string line = null;
// FileInfo t = new FileInfo(path + suffixBytes);
// if(!t.Exists)
// {
// //如果此文件不存在则创建
// sw = t.CreateText();
// }
// else
// {
// //如果此文件存在则打开
// sw = t.AppendText();
// }
//
// for (int i = 0; i < list.Count; i++) {
// line = list [i].ToString ();
// //以行的形式写入信息
// // sw.WriteLine (line);
// sw.Write (line);
// }
// //关闭流
// sw.Close();
// //销毁流
// sw.Dispose();
// AssetDatabase.Refresh();
// }
}