EditorHelper.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using UnityEditor;
  7. using UnityEngine;
  8. using Debug = UnityEngine.Debug;
  9. namespace ET.PackageManager.Editor
  10. {
  11. /// <summary>
  12. /// 编辑器工具可能会用到的各种工具
  13. /// </summary>
  14. public static class EditorHelper
  15. {
  16. private static string m_ProjFolder;
  17. /// <summary>
  18. /// 得到项目绝对路径
  19. /// Application.dataPath = D:\XX\Assets
  20. /// 这里去掉最后的Assets,得到D:\XX
  21. /// </summary>
  22. /// <returns></returns>
  23. public static string GetProjPath(string relativePath = "")
  24. {
  25. if (string.IsNullOrEmpty(m_ProjFolder))
  26. {
  27. var projFolder = Application.dataPath;
  28. m_ProjFolder = projFolder.Substring(0, projFolder.Length - 7);
  29. }
  30. if (relativePath == null)
  31. {
  32. relativePath = "";
  33. }
  34. relativePath = relativePath.Trim();
  35. if (!string.IsNullOrEmpty(relativePath))
  36. {
  37. if (relativePath.Contains("\\"))
  38. {
  39. relativePath = relativePath.Replace("\\", "/");
  40. }
  41. if (!relativePath.StartsWith("/"))
  42. {
  43. relativePath = $"/{relativePath}";
  44. }
  45. }
  46. return $"{m_ProjFolder}{relativePath}";
  47. }
  48. /// <summary>
  49. /// 打开文件或文件夹
  50. /// </summary>
  51. /// <param name="path"></param>
  52. public static void OpenFileOrFolder(string path)
  53. {
  54. Process.Start("explorer.exe", path.Replace("/", "\\"));
  55. }
  56. /// <summary>
  57. /// 用于查找某个只知道名字的文件在什么位置
  58. /// </summary>
  59. /// <param name="name"></param>
  60. /// <returns></returns>
  61. public static string[] GetAssetPaths(string name)
  62. {
  63. var paths = AssetDatabase.FindAssets(name);
  64. for (int i = 0; i < paths.Length; i++)
  65. {
  66. paths[i] = AssetDatabase.GUIDToAssetPath(paths[i]);
  67. }
  68. return paths;
  69. }
  70. /// <summary>
  71. /// 在项目文件内写入文件
  72. /// </summary>
  73. public static bool WriteAllText(string path, string contents, bool log = false)
  74. {
  75. try
  76. {
  77. path = GetProjPath(path);
  78. var dir = Path.GetDirectoryName(path);
  79. if (dir == null)
  80. {
  81. Debug.LogError("dir == null");
  82. return false;
  83. }
  84. if (!Directory.Exists(dir))
  85. {
  86. Directory.CreateDirectory(dir);
  87. }
  88. File.WriteAllText(path, contents, Encoding.UTF8);
  89. if (log)
  90. Debug.Log(path + "创建成功");
  91. return true;
  92. }
  93. catch (Exception e)
  94. {
  95. Debug.LogError("写入文件失败: path =" + path + ", err=" + e);
  96. return false;
  97. }
  98. }
  99. public static string ReadAllText(string path)
  100. {
  101. try
  102. {
  103. path = GetProjPath(path);
  104. if (!File.Exists(path))
  105. {
  106. return null;
  107. }
  108. return File.ReadAllText(path);
  109. }
  110. catch (Exception e)
  111. {
  112. Debug.LogError("读取文件失败: path =" + path + ", err=" + e);
  113. return null;
  114. }
  115. }
  116. public static bool WriteAllBytes(string path, byte[] bytes, bool log = false)
  117. {
  118. try
  119. {
  120. path = GetProjPath(path);
  121. var dir = Path.GetDirectoryName(path);
  122. if (dir == null)
  123. {
  124. Debug.LogError("dir == null");
  125. return false;
  126. }
  127. if (!Directory.Exists(dir))
  128. {
  129. Directory.CreateDirectory(dir);
  130. }
  131. File.WriteAllBytes(path, bytes);
  132. if (log)
  133. Debug.Log(path + "创建成功");
  134. return true;
  135. }
  136. catch (Exception e)
  137. {
  138. Debug.LogError("写入文件失败: path =" + path + ", err=" + e);
  139. return false;
  140. }
  141. }
  142. public static byte[] ReadAllBytes(string path)
  143. {
  144. try
  145. {
  146. path = GetProjPath(path);
  147. if (!File.Exists(path))
  148. {
  149. return null;
  150. }
  151. return File.ReadAllBytes(path);
  152. }
  153. catch (Exception e)
  154. {
  155. Debug.LogError("读取文件失败: path =" + path + ", err=" + e);
  156. return null;
  157. }
  158. }
  159. /// <summary>
  160. /// 是否忽略
  161. /// </summary>
  162. /// <param name="path"></param>
  163. /// <returns></returns>
  164. public static bool IsFileIgnore(string path)
  165. {
  166. return path.EndsWith(".meta")
  167. || path.EndsWith(".manifest")
  168. || path.Contains(".DS_Store");
  169. }
  170. // ----------------------------------------------------------------------------------------
  171. /// <summary>
  172. /// 进度条界面更新
  173. /// </summary>
  174. /// <param name="title">标题</param>
  175. /// <param name="message">内容</param>
  176. /// <param name="current">当前进度</param>
  177. /// <param name="total">总进度</param>
  178. public static void DisplayProgressBar(string title, string message, int current, int total)
  179. {
  180. float progress = 0;
  181. if (total != 0)
  182. {
  183. progress = Mathf.InverseLerp(0, total, current);
  184. message = string.Format("{0} {1}/{2}", message, current + 1, total);
  185. }
  186. EditorUtility.DisplayProgressBar(title, message, progress);
  187. }
  188. /// <summary>
  189. /// 关闭进度
  190. /// </summary>
  191. public static void ClearProgressBar()
  192. {
  193. EditorUtility.ClearProgressBar();
  194. }
  195. //检查目标路径文件夹是否存在
  196. public static bool ExistsDirectory(string path)
  197. {
  198. return Directory.Exists(GetProjPath(path));
  199. }
  200. //检查文件夹 不存在则创建
  201. public static void CreateExistsDirectory(string path, bool checkDirectory = false)
  202. {
  203. path = GetProjPath(path);
  204. if (checkDirectory)
  205. {
  206. path = Path.GetDirectoryName(path);
  207. if (path == null)
  208. {
  209. Debug.LogError($" {path} dir == null");
  210. return;
  211. }
  212. }
  213. if (!Directory.Exists(path))
  214. {
  215. Directory.CreateDirectory(path);
  216. }
  217. }
  218. }
  219. }