void OnGUI()
 {
     FileOperation.showIOPath("文本文件所在目录:", ref textFolderPath, true);
     lineEndType       = (FileOperation.ELineEnd)EditorGUILayout.EnumPopup("行尾希望转换成:", lineEndType);
     searchPatternType = EditorGUILayout.Popup("希望处理的文件类型:", searchPatternType, SEARCH);
     if (GUILayout.Button("执行"))
     {
         if (EditorUtility.DisplayDialog("请确认", "确定执行该目录吗:\n" + textFolderPath, "OK", "Cancle"))
         {
             DoConvert();
         }
     }
 }
示例#2
0
    //设置文本文件的行尾特征
    public static void SetFileLineEnding(string filePath, FileOperation.ELineEnd endType)
    {
        StreamReader sr         = null;
        string       lineEndStr = "\r\n";

        if (endType == ELineEnd.linux)
        {
            lineEndStr = "\n";
        }
        else if (endType == ELineEnd.Mac)
        {
            lineEndStr = "\r";
        }
        try
        {
            FileStream fileStream = new FileStream(filePath, FileMode.Open);
            Encoding   encoding   = GetEncoding(fileStream);
            fileStream.Close();
            sr = new StreamReader(filePath, encoding);
            String text = "";
            String line;
            //--------------判断,如不需要转换则跳过该文件-----------------
            if (true)            //减少变量生存期
            {
                char[] buff    = new char[251];
                string strbuff = "";
                if (sr.BaseStream.Length > 251)
                {
                    sr.Read(buff, 0, 250);
                    strbuff = new string(buff);
                }
                else
                {
                    strbuff = sr.ReadToEnd();
                }

                if (strbuff.Contains("\r\n"))
                {
                    if (endType == ELineEnd.Windows)
                    {
                        //Debug.Log("Already Windows:\n"+filePath);
                        return;
                    }
                }
                else if (strbuff.Contains("\r"))
                {
                    if (endType == ELineEnd.Mac)
                    {
                        //Debug.Log("Already Mac:\n" + filePath);
                        return;
                    }
                }
                else if (strbuff.Contains("\n"))
                {
                    if (endType == ELineEnd.linux)
                    {
                        //Debug.Log("Already Linux:\n" + filePath);
                        return;
                    }
                }
            }
            sr.BaseStream.Seek(0, SeekOrigin.Begin);
            sr.DiscardBufferedData();
            //-------------------------------------------------------------
            bool first = true;
            while ((line = sr.ReadLine()) != null)
            {
                if (!first)
                {
                    text += lineEndStr;
                }
                else
                {
                    first = false;
                }

                text += line;
            }
            sr.BaseStream.Seek(-1, SeekOrigin.Current);
            string checkFileEnd = sr.ReadToEnd();                           //用于检测文件结尾有没有换行符
            if (checkFileEnd.Contains("\r") || checkFileEnd.Contains("\n")) //文件确实是用换行符做结尾的
            {
                text += lineEndStr;
            }
            sr.Close();
            Save(filePath, text, encoding);
        }
        catch (Exception e)
        {
                        #if UNITY_EDITOR
            EditorUtility.DisplayDialog("File Save Error!", filePath + "\n" + e.Message, "OK");
                        #endif
        }
        finally
        {
            if (sr != null)
            {
                sr.Close();
            }
        }
    }