330 lines
12 KiB
C#
Raw Permalink Normal View History

2025-03-11 16:24:25 +08:00
using UnityEngine;
using LitJson;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System;
using System.IO;
using System.Threading;
using System.Diagnostics;
using Debug = UnityEngine.Debug;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
namespace ZXKFramework
{
public class UnityTools : MonoBehaviour
{
public static Sprite ToSprite(Texture2D self)
{
var rect = new Rect(0, 0, self.width, self.height);
var pivot = Vector2.one * 0.5f;
var newSprite = Sprite.Create(self, rect, pivot);
return newSprite;
}
public static string GetJson(object self)
{
string loRes = JsonMapper.ToJson(self);
return StringToUTF8(loRes);
}
public static string StringToUTF8(string self)
{
Regex reg = new Regex(@"(?i)\\[uU]([0-9a-f]{4})");
self = reg.Replace(self, delegate (Match m)
{
return ((char)Convert.ToInt32(m.Groups[1].Value, 16)).ToString();
});
return self;
}
/// <summary>
/// <20><><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD>ýǶ<C3BD>
/// </summary>
/// <param name="DicPosAndRotation"><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD></param>
/// <param name="key">ʵ<><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģ<EFBFBD><C4A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
/// <param name="vectorPos">λ<><CEBB></param>
/// <param name="vectorRotation"><3E>Ƕ<EFBFBD></param>
/// <param name="obj"></param>
public static void AddPosAndRotation(Dictionary<string, List<Vector3>> DicPosAndRotation, string key, Vector3 vectorPos, Vector3 vectorRotation, GameObject gameObj)
{
List<Vector3> listVec = new List<Vector3>();
listVec.Add(vectorPos);
listVec.Add(vectorRotation);
if (!DicPosAndRotation.ContainsKey(key))
{
DicPosAndRotation.Add(key, listVec);
}
else
{
if (gameObj != null)
{
gameObj.transform.localPosition = DicPosAndRotation[key][0];
gameObj.transform.localEulerAngles = DicPosAndRotation[key][1];
}
}
}
//<2F><><EFBFBD><EFBFBD>Ŀ¼<C4BF>ļ<EFBFBD><C4BC><EFBFBD>ָ<EFBFBD><D6B8>Ŀ¼
public static void CopyDirIntoDestDirectory(string sourceFileName, string destFileName, bool overwrite)
{
if (!Directory.Exists(destFileName))
Directory.CreateDirectory(destFileName);
foreach (var file in Directory.GetFiles(sourceFileName))
File.Copy(file, Path.Combine(destFileName, Path.GetFileName(file)), overwrite);
foreach (var d in Directory.GetDirectories(sourceFileName))
CopyDirIntoDestDirectory(d, Path.Combine(destFileName, Path.GetFileName(d)), overwrite);
}
//<2F><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>
public static void CreateDirectory(string destFileName)
{
if (!Directory.Exists(destFileName))
Directory.CreateDirectory(destFileName);
}
//<2F><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>
public static List<string> GetFile(string path, List<string> FileList)
{
DirectoryInfo dir = new DirectoryInfo(path);
FileInfo[] fil = dir.GetFiles();
DirectoryInfo[] dii = dir.GetDirectories();
foreach (FileInfo f in fil)
{
long size = f.Length;
FileList.Add(Path.GetFileNameWithoutExtension(f.Name));
}
foreach (DirectoryInfo d in dii)
{
GetFile(d.Name, FileList);
}
return FileList;
}
//<2F><>ȡָ<C8A1><D6B8>·<EFBFBD><C2B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD>ļ<EFBFBD> Ȼ<><C8BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɾ<EFBFBD><C9BE>
public static bool DeleteAllFile(string fullPath)
{
if (Directory.Exists(fullPath))
{
DirectoryInfo direction = new DirectoryInfo(fullPath);
FileInfo[] files = direction.GetFiles("*", SearchOption.AllDirectories);
for (int i = 0; i < files.Length; i++)
{
string FilePath = fullPath + "/" + files[i].Name;
File.Delete(FilePath);
}
return true;
}
return false;
}
//ɾ<><C9BE><EFBFBD>ļ<EFBFBD>
public static void DeleteFile(string path)
{
if (File.Exists(path))
{
File.Delete(path);
}
}
//<2F><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>
public static void CreateFile(string path)
{
if (!File.Exists(path))
{
File.Create(path);
}
}
public static void OpenDirectory(string path)
{
if (string.IsNullOrEmpty(path)) return;
path = path.Replace("/", "\\");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
// <20>¿<EFBFBD><C2BF>̷߳<DFB3>ֹ<EFBFBD><D6B9><EFBFBD><EFBFBD>
Thread newThread = new Thread(new ParameterizedThreadStart(CmdOpenDirectory));
newThread.Start(path);
//<2F><><EFBFBD><EFBFBD>360<36><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//System.Diagnostics.Process.Start("explorer.exe", path);
}
private static void CmdOpenDirectory(object obj)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/c start " + obj.ToString();
Debug.Log(p.StartInfo.Arguments);
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.WaitForExit();
p.Close();
}
public void FullSceneSet()
{
Screen.fullScreen = !Screen.fullScreen;
if (Screen.fullScreen) Screen.SetResolution(1920, 1080, false);
else Screen.SetResolution(1280, 720, false);
}
/// <summary>
/// <20><>ȡһ<C8A1><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD><CBB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
/// <param name="rangNum">ȡ<><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>ж<EFBFBD><D0B6>ٸ<EFBFBD><D9B8><EFBFBD></param>
/// <param name="endNum"><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ķ<EFBFBD>Χ(<28><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>)</param>
/// <param name="startNum"><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ķ<EFBFBD>Χ(<28><>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>)</param>
public static int[] GetRandomSequence(int rangNum, int endNum, int startNum = 0)
{
try
{
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int[] sequence = new int[endNum - startNum + 1];
//ȡ<><C8A1><EFBFBD>IJ<EFBFBD><C4B2>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><E9B3A4>
int[] output = new int[rangNum];
for (int i = startNum; i < endNum + 1; i++)
{
sequence[i - startNum] = i;
}
for (int i = 0; i < rangNum; i++)
{
//<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD>һ<EFBFBD>Σ<EFBFBD><CEA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>-1
int Num = UnityEngine.Random.Range(startNum, endNum + 1);
output[i] = sequence[Num - startNum];
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
sequence[Num - startNum] = sequence[endNum - startNum];
endNum--;
//ִ<><D6B4>һ<EFBFBD><D2BB>Ч<EFBFBD><D0A7><EFBFBD>磺1<E7A3BA><31>2<EFBFBD><32>3<EFBFBD><33>4<EFBFBD><34>5 ȡ<><C8A1>2
//<2F><><EFBFBD>´<EFBFBD><C2B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ1,5,3,4;
}
return output;
}
catch (Exception e)
{
Debug.LogError($"GetRandomSequence is Error:{e.ToString()}");
return null;
}
}
/// <summary>
/// <20><>ȡʱ<C8A1><CAB1><EFBFBD><EFBFBD>13λ
/// </summary>
public static string GetTimeStamp()
{
return DateTimeOffset.Now.ToUnixTimeSeconds() + "000";
}
/// <summary>
/// <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
public static int TimeStampDiffSeconds(string t1, string t2)
{
return Mathf.Abs(Convert.ToInt32((ConvertToTime(t1) - ConvertToTime(t2)).TotalSeconds));
}
/// <summary>
/// ʱ<><CAB1><EFBFBD><EFBFBD>ת<EFBFBD><D7AA><EFBFBD>ɱ<EFBFBD>׼ʱ<D7BC><CAB1>
/// </summary>
public static DateTime ConvertToTime(string timeStamp)
{
string ID = TimeZoneInfo.Local.Id;
DateTime start = new DateTime(1970, 1, 1) + TimeZoneInfo.Local.GetUtcOffset(DateTime.Now);
DateTime dtStart = TimeZoneInfo.ConvertTime(start, TimeZoneInfo.FindSystemTimeZoneById(ID));
long lTime = long.Parse(timeStamp + "0000");
TimeSpan toNow = new TimeSpan(lTime);
DateTime time = dtStart.Add(toNow);
return time;
}
/// <summary>
/// JSON<4F>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD>
/// </summary>
public static string JsonTree(string json)
{
int level = 0;
var jsonArr = json.ToArray(); // Using System.Linq;
string jsonTree = string.Empty;
for (int i = 0; i < json.Length; i++)
{
char c = jsonArr[i];
if (level > 0 && '\n' == jsonTree.ToArray()[jsonTree.Length - 1])
{
jsonTree += TreeLevel(level);
}
switch (c)
{
case '[':
jsonTree += c + "\n";
level++;
break;
case ',':
jsonTree += c + "\n";
break;
case ']':
jsonTree += "\n";
level--;
jsonTree += TreeLevel(level);
jsonTree += c;
break;
default:
jsonTree += c;
break;
}
}
return jsonTree;
}
/// <summary>
/// <20><><EFBFBD>ȼ<EFBFBD>
/// </summary>
private static string TreeLevel(int level)
{
string leaf = string.Empty;
for (int t = 0; t < level; t++)
{
leaf += "\t";
}
return leaf;
}
/// <summary>
/// <20><><EFBFBD><EFBFBD>URL<52>еIJ<D0B5><C4B2><EFBFBD>
/// </summary>
public static string GetURLParameters(string url)
{
if (string.IsNullOrEmpty(url)) return null;
var paramStr = url.Split('?')[1];
var paramList = paramStr.Split('&');
if (paramList == null || paramList.Count() <= 0) return null;
return paramList.ToDictionary(e => Regex.Split(e, "=")[0], e => Regex.Split(e, "=")[1])["ticket"];
}
/// <summary>
/// MD5 32λ <20><>д<EFBFBD><D0B4><EFBFBD><EFBFBD>
/// </summary>
public static string MD5Encrypt32Big(string encryptContent)
{
string content = encryptContent;
//<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>MD5CryptoServiceProvider<65><72><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD>
MD5 md5 = MD5.Create();
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>ת<EFBFBD><D7AA>Ϊһ<CEAA><D2BB><EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD><EFBFBD><EFBFBD><E9B2A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϣֵ<CFA3><D6B5>
byte[] data = md5.ComputeHash(Encoding.UTF8.GetBytes(content));
//<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>StringBuilder<65><72><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ռ<EFBFBD><D5BC>ֽ<EFBFBD><D6BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>ÿһ<C3BF><D2BB><EFBFBD>ֽڣ<D6BD>Ȼ<EFBFBD>󴴽<EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>
StringBuilder sb = new StringBuilder();
//<2F><><EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD><EFBFBD><EFBFBD><E9A3AC>ÿһ<C3BF><D2BB><EFBFBD>ֽ<EFBFBD>ת<EFBFBD><D7AA>Ϊʮ<CEAA><CAAE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>׷<EFBFBD>ӵ<EFBFBD>StringBuilderʵ<72><CAB5><EFBFBD>Ľ<EFBFBD>β
for (int i = 0; i < data.Length; i++)
{
sb.Append(data[i].ToString("X2"));
}
//<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>ʮ<EFBFBD><CAAE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
content = sb.ToString();
if (content.Length < 32)
Debug.LogError("32λ <20><><EFBFBD>ܴ<EFBFBD><DCB4>󣡣<EFBFBD><F3A3A1A3><EFBFBD><EFBFBD>Ȳ<EFBFBD><C8B2><EFBFBD>");
return content;
}
}
}