Files
JJBB/Assets/Plugins/Script/AssetUpdate/AssetUtils.cs

59 lines
1.6 KiB
C#
Raw Normal View History

2024-08-23 15:49:34 +08:00
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");
}
}