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();
  //  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值