PathHelper.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using System.IO;
  2. using UnityEngine;
  3. /// <summary>
  4. /// 路径帮助类
  5. /// </summary>
  6. public class PathHelper
  7. {
  8. /// <summary>
  9. /// 根目录
  10. /// </summary>
  11. public static readonly string m_AssetRootPath = Application.dataPath;
  12. /// <summary>
  13. /// 只读根目录(streamingAssetsPath)
  14. /// </summary>
  15. public static readonly string m_ReadOnlyPath = Application.streamingAssetsPath;
  16. /// <summary>
  17. /// 可读可写根目录(persistentDataPath)
  18. /// </summary>
  19. public static readonly string m_WriteAndReadPath = Application.persistentDataPath;
  20. /// <summary>
  21. /// 获取标准路径
  22. /// </summary>
  23. /// <param name="_path"></param>
  24. /// <returns></returns>
  25. public static string GetStandardPath(string _path)
  26. {
  27. if (string.IsNullOrEmpty(_path))
  28. {
  29. return string.Empty;
  30. }
  31. return _path.Trim().Replace("\\", "/");
  32. }
  33. /// <summary>
  34. /// 获取指定路径
  35. /// </summary>
  36. /// <param name="_path"></param>
  37. /// <returns></returns>
  38. public static string GetSpecifiedPath(string _path)
  39. {
  40. if (string.IsNullOrEmpty(_path))
  41. {
  42. return null;
  43. }
  44. string result =
  45. #if UNITY_STANDALONE_WIN
  46. GetStandardPath(m_ReadOnlyPath + "/" + _path);
  47. #else
  48. GetStandardPath(m_WriteAndReadPath + "/" + _path);
  49. #endif
  50. return result;
  51. }
  52. /// <summary>
  53. /// 获取文件后缀
  54. /// </summary>
  55. /// <param name="_fileName">带有后缀的文件名</param>
  56. /// <returns>返回带点的文件后缀</returns>
  57. public static string GetFileSuffix(string _fileName)
  58. {
  59. if(string.IsNullOrEmpty(_fileName))
  60. {
  61. return string.Empty;
  62. }
  63. return "." + _fileName.Split('.')[1];
  64. }
  65. /// <summary>
  66. /// 根据路径获取文件扩展名
  67. /// </summary>
  68. /// <param name="_path"></param>
  69. /// <returns></returns>
  70. public static string GetExtensionBasedOnPath(string _path)
  71. {
  72. if (string.IsNullOrEmpty(_path))
  73. {
  74. return string.Empty;
  75. }
  76. return Path.GetFileName(_path);
  77. }
  78. /// <summary>
  79. /// 获取文件名
  80. /// </summary>
  81. /// <param name="_fileName">带有后缀的文件名</param>
  82. /// <returns></returns>
  83. public static string GetFileName(string _fileName)
  84. {
  85. if (string.IsNullOrEmpty(_fileName))
  86. {
  87. return string.Empty;
  88. }
  89. return _fileName.Split('.')[0];
  90. }
  91. /// <summary>
  92. /// 根据路径获取不带扩展名的文件名
  93. /// </summary>
  94. /// <param name="_path"></param>
  95. /// <returns></returns>
  96. public static string GetFileNameWithoutExtensionOnPath(string _path)
  97. {
  98. if (string.IsNullOrEmpty(_path))
  99. {
  100. return string.Empty;
  101. }
  102. return Path.GetFileNameWithoutExtension(_path);
  103. }
  104. /// <summary>
  105. /// 根据路径获取文件名,带扩展名
  106. /// </summary>
  107. /// <param name="_path"></param>
  108. /// <returns></returns>
  109. public static string GetFileNameBasedOnPath(string _path)
  110. {
  111. if (string.IsNullOrEmpty(_path))
  112. {
  113. return string.Empty;
  114. }
  115. return Path.GetFileName(_path);
  116. }
  117. /// <summary>
  118. /// 合并路径
  119. /// </summary>
  120. /// <param name="_paths"></param>
  121. /// <returns></returns>
  122. public static string CombineFilePath(params string[] _paths)
  123. {
  124. if (_paths.Length <= 0)
  125. {
  126. return null;
  127. }
  128. string result = "";
  129. for (int i = 0; i < _paths.Length; i++)
  130. {
  131. result = Path.Combine(result, _paths[i]);
  132. }
  133. return result;
  134. }
  135. }