change.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.IO;
  4. using System.Text;
  5. public class change
  6. {
  7. // 添加一个右键菜单。
  8. // % 按下ctrl时显示菜单。(Windows: control, macOS: command)
  9. // & 按下alt时显示菜单。(Windows/Linux: alt, macOS: option)
  10. // _ 按下shift时显示菜单。(Windows/Linux/macOS: shift)
  11. [MenuItem("Assets/脚本改格式:GB2312->UTF8无BOM %g", false, 100)]
  12. private static void CustomMenu()
  13. {
  14. Object[] selectedObjects = Selection.objects;
  15. for(int i=0;i<selectedObjects.Length;i++)
  16. {
  17. // 例如: 获取Project视图中选定的对象
  18. Object selectedObject = selectedObjects[i];
  19. if (selectedObject != null)
  20. {
  21. // 获取选定对象的相对路径
  22. string relativeAssetPath = AssetDatabase.GetAssetPath(selectedObject);
  23. // 获取项目根目录路径
  24. string projectPath = Path.GetDirectoryName(Application.dataPath);
  25. // 获取选定对象的绝对路径
  26. string absoluteAssetPath = Path.Combine(projectPath, relativeAssetPath);
  27. // 获取选定对象的文件名(包括后缀)
  28. string fileName = Path.GetFileName(relativeAssetPath);
  29. Debug.Log("执行自定义操作: " + selectedObject.name +
  30. ", 相对路径: " + relativeAssetPath +
  31. ", 绝对路径: " + absoluteAssetPath +
  32. ", 文件名: " + fileName);
  33. //判断是否是CSharp文件
  34. if (IsCSharpFile(fileName))
  35. {
  36. Debug.Log("这是一个csharp文件");
  37. ChangeFormat(absoluteAssetPath);
  38. }
  39. else
  40. {
  41. Debug.Log("兄弟,这不是一个csharp文件啊~~~~~~~~~~~");
  42. }
  43. }
  44. }
  45. }
  46. // 如果项目视图中有选中的对象,则启用右键菜单项
  47. [MenuItem("Assets/脚本改格式:GB2312->UTF8无BOM %g", true)]
  48. private static bool ValidateCustomMenu()
  49. {
  50. return Selection.activeObject != null;
  51. }
  52. /// <summary>
  53. /// 判断该文件是否是CSharp文件
  54. /// </summary>
  55. /// <param name="fileName"></param>
  56. /// <returns></returns>
  57. private static bool IsCSharpFile(string fileName)
  58. {
  59. // 获取文件扩展名(包括点)
  60. string fileExtension = Path.GetExtension(fileName);
  61. // 将扩展名转换为小写并与 ".cs" 进行比较
  62. if (fileExtension.ToLower() == ".cs")
  63. {
  64. return true;
  65. }
  66. return false;
  67. }
  68. /// <summary>
  69. /// 文件格式转码:GB2312转成UTF8
  70. /// 读取指定的文件,转换成UTF8(无BOM标记)格式后,回写覆盖原文件
  71. /// </summary>
  72. /// <param name="sourceFilePath">文件路径</param>
  73. public static void ChangeFormat(string sourceFilePath)
  74. {
  75. Encoding ed =GetType(sourceFilePath);
  76. if( ed is System.Text.ASCIIEncoding )
  77. {
  78. ed =Encoding.GetEncoding("GB2312");
  79. string fileContent = File.ReadAllText(sourceFilePath, ed);
  80. File.WriteAllText(sourceFilePath, fileContent, Encoding.UTF8);
  81. }
  82. Debug.Log("处理结束!");
  83. }
  84. public static System.Text.Encoding GetType(string FILE_NAME)
  85. {
  86. FileStream fs = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read);
  87. Encoding r = GetType(fs);
  88. fs.Close();
  89. return r;
  90. }
  91. /// <summary>
  92. /// 通过给定的文件流,判断文件的编码类型
  93. /// </summary>
  94. /// <param name="fs">文件流</param>
  95. /// <returns>文件的编码类型</returns>
  96. public static System.Text.Encoding GetType(FileStream fs)
  97. {
  98. byte[] Unicode = new byte[] { 0xFF, 0xFE, 0x41 };
  99. byte[] UnicodeBIG = new byte[] { 0xFE, 0xFF, 0x00 };
  100. byte[] UTF8 = new byte[] { 0xEF, 0xBB, 0xBF }; //带BOM
  101. Encoding reVal = Encoding.Default;
  102. BinaryReader r = new BinaryReader(fs, System.Text.Encoding.Default);
  103. int i;
  104. int.TryParse(fs.Length.ToString(), out i);
  105. byte[] ss = r.ReadBytes(i);
  106. if (IsUTF8Bytes(ss) || (ss[0] == 0xEF && ss[1] == 0xBB && ss[2] == 0xBF))
  107. {
  108. reVal = Encoding.UTF8;
  109. }
  110. else if (ss[0] == 0xFE && ss[1] == 0xFF && ss[2] == 0x00)
  111. {
  112. reVal = Encoding.BigEndianUnicode;
  113. }
  114. else if (ss[0] == 0xFF && ss[1] == 0xFE && ss[2] == 0x41)
  115. {
  116. reVal = Encoding.Unicode;
  117. }
  118. r.Close();
  119. return reVal;
  120. }
  121. /// <summary>
  122. /// 判断是否是不带 BOM 的 UTF8 格式
  123. /// </summary>
  124. /// <param name="data"></param>
  125. /// <returns></returns>
  126. private static bool IsUTF8Bytes(byte[] data)
  127. {
  128. int charByteCounter = 1;
  129. //计算当前正分析的字符应还有的字节数
  130. byte curByte; //当前分析的字节.
  131. for (int i = 0; i < data.Length; i++)
  132. {
  133. curByte = data[i];
  134. if (charByteCounter == 1)
  135. {
  136. if (curByte >= 0x80)
  137. {
  138. //判断当前
  139. while (((curByte <<= 1) & 0x80) != 0)
  140. {
  141. charByteCounter++;
  142. }
  143. //标记位首位若为非0 则至少以2个1开始 如:110XXXXX...........1111110X
  144. if (charByteCounter == 1 || charByteCounter > 6)
  145. {
  146. return false;
  147. }
  148. }
  149. }
  150. else
  151. {
  152. //若是UTF-8 此时第一位必须为1
  153. if ((curByte & 0xC0) != 0x80)
  154. {
  155. return false;
  156. }
  157. charByteCounter--;
  158. }
  159. }
  160. if (charByteCounter > 1)
  161. {
  162. // throw new Exception("非预期的byte格式");
  163. }
  164. return true;
  165. }
  166. }