59 lines
1.6 KiB
C#
59 lines
1.6 KiB
C#
using System.IO;
|
||
using System.Linq;
|
||
using System.Security.Cryptography;
|
||
using System.Text;
|
||
using System.Text.RegularExpressions;
|
||
|
||
public static class AssetUtils
|
||
{
|
||
private static readonly MD5 _md5 = MD5.Create();
|
||
|
||
private static readonly char[] _seperateChars = {'/', '\\'};
|
||
|
||
/// <summary>
|
||
/// Unity文件路径向上一级,可用于取得文件路径
|
||
/// </summary>
|
||
public static string MoveUp(this string path)
|
||
{
|
||
path = path.TrimEnd(_seperateChars);
|
||
var index = path.LastIndexOfAny(_seperateChars);
|
||
path = index >= 0 ? path.Remove(index) : string.Empty;
|
||
return path;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Unity文件路径到下一级
|
||
/// </summary>
|
||
public static string Open(this string path, string next)
|
||
{
|
||
return string.IsNullOrEmpty(next) ? path : path.TrimEnd(_seperateChars) + "/" + next;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 路径转化为超链接路径(Unity资源路径)格式
|
||
/// </summary>
|
||
public static string ToUrl(this string path)
|
||
{
|
||
return path.Replace("\\", "/");
|
||
}
|
||
|
||
public static string GetFileMd5(string filePath)
|
||
{
|
||
return GetMd5FromBytes(File.ReadAllBytes(filePath));
|
||
}
|
||
|
||
public static string GetTextMd5(string text)
|
||
{
|
||
return GetMd5FromBytes(Encoding.UTF8.GetBytes(text));
|
||
}
|
||
|
||
private static string GetMd5FromBytes(byte[] bytes)
|
||
{
|
||
return string.Concat(_md5.ComputeHash(bytes).Select(x => x.ToString("x2")).ToArray()).ToLower();
|
||
}
|
||
|
||
public static string[] TextToLines(string text)
|
||
{
|
||
return Regex.Split(text, "\\r\\n|\\r|\\n");
|
||
}
|
||
} |