123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- using System.IO;
- using UnityEngine;
- /// <summary>
- /// 路径帮助类
- /// </summary>
- public class PathHelper
- {
- /// <summary>
- /// 根目录
- /// </summary>
- public static readonly string m_AssetRootPath = Application.dataPath;
- /// <summary>
- /// 只读根目录(streamingAssetsPath)
- /// </summary>
- public static readonly string m_ReadOnlyPath = Application.streamingAssetsPath;
- /// <summary>
- /// 可读可写根目录(persistentDataPath)
- /// </summary>
- public static readonly string m_WriteAndReadPath = Application.persistentDataPath;
- /// <summary>
- /// 获取标准路径
- /// </summary>
- /// <param name="_path"></param>
- /// <returns></returns>
- public static string GetStandardPath(string _path)
- {
- if (string.IsNullOrEmpty(_path))
- {
- return string.Empty;
- }
- return _path.Trim().Replace("\\", "/");
- }
- /// <summary>
- /// 获取指定路径
- /// </summary>
- /// <param name="_path"></param>
- /// <returns></returns>
- public static string GetSpecifiedPath(string _path)
- {
- if (string.IsNullOrEmpty(_path))
- {
- return null;
- }
- string result =
- #if UNITY_STANDALONE_WIN
- GetStandardPath(m_ReadOnlyPath + "/" + _path);
- #else
- GetStandardPath(m_WriteAndReadPath + "/" + _path);
- #endif
- return result;
- }
- /// <summary>
- /// 获取文件后缀
- /// </summary>
- /// <param name="_fileName">带有后缀的文件名</param>
- /// <returns>返回带点的文件后缀</returns>
- public static string GetFileSuffix(string _fileName)
- {
- if(string.IsNullOrEmpty(_fileName))
- {
- return string.Empty;
- }
- return "." + _fileName.Split('.')[1];
- }
- /// <summary>
- /// 根据路径获取文件扩展名
- /// </summary>
- /// <param name="_path"></param>
- /// <returns></returns>
- public static string GetExtensionBasedOnPath(string _path)
- {
- if (string.IsNullOrEmpty(_path))
- {
- return string.Empty;
- }
- return Path.GetFileName(_path);
- }
-
-
- /// <summary>
- /// 获取文件名
- /// </summary>
- /// <param name="_fileName">带有后缀的文件名</param>
- /// <returns></returns>
- public static string GetFileName(string _fileName)
- {
- if (string.IsNullOrEmpty(_fileName))
- {
- return string.Empty;
- }
- return _fileName.Split('.')[0];
- }
- /// <summary>
- /// 根据路径获取不带扩展名的文件名
- /// </summary>
- /// <param name="_path"></param>
- /// <returns></returns>
- public static string GetFileNameWithoutExtensionOnPath(string _path)
- {
- if (string.IsNullOrEmpty(_path))
- {
- return string.Empty;
- }
- return Path.GetFileNameWithoutExtension(_path);
- }
-
- /// <summary>
- /// 根据路径获取文件名,带扩展名
- /// </summary>
- /// <param name="_path"></param>
- /// <returns></returns>
- public static string GetFileNameBasedOnPath(string _path)
- {
- if (string.IsNullOrEmpty(_path))
- {
- return string.Empty;
- }
- return Path.GetFileName(_path);
- }
-
- /// <summary>
- /// 合并路径
- /// </summary>
- /// <param name="_paths"></param>
- /// <returns></returns>
- public static string CombineFilePath(params string[] _paths)
- {
- if (_paths.Length <= 0)
- {
- return null;
- }
- string result = "";
- for (int i = 0; i < _paths.Length; i++)
- {
- result = Path.Combine(result, _paths[i]);
- }
-
- return result;
- }
- }
|