2024-08-23 15:49:34 +08:00

59 lines
1.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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");
}
}