254 lines
9.5 KiB
C#
254 lines
9.5 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using UnityEngine;
|
||
using UnityEngine.Networking;
|
||
using UnityEngine.SceneManagement;
|
||
/********************************************************************************
|
||
*Create By CG
|
||
*Function 通用工具方法
|
||
*********************************************************************************/
|
||
namespace CG.UTility
|
||
{
|
||
public static class UtilitiesMng
|
||
{
|
||
/// <summary>
|
||
/// 应用退出的几种方式【根据需要自行选择】
|
||
/// </summary>
|
||
public static void CustomQuit()
|
||
{
|
||
|
||
Resources.UnloadUnusedAssets();// 卸载未使用的资产
|
||
|
||
#if UNITY_EDITOR
|
||
UnityEditor.EditorApplication.isPlaying = false;// 在编译状态游戏退出
|
||
#else
|
||
Application.Quit();//正常退出(在打包后使用,不能再编译状态下使用)
|
||
System.Diagnostics.Process.GetCurrentProcess().Kill();// 关掉与当前活动相关的进程
|
||
System.Environment.Exit(0);// 终止当前进程并为基础操作系统提供指定的退出代码。
|
||
#endif
|
||
PlayerPrefs.DeleteAll();//删除储存在注册表中的持久化储存的数据
|
||
System.GC.Collect();// 强制立刻回收
|
||
|
||
}
|
||
|
||
/// <summary>
|
||
/// 计算点到线的距离
|
||
/// </summary>
|
||
/// <param name="point1">point1为线的端点</param>
|
||
/// <param name="point2">point2为线的端点</param>
|
||
/// <param name="position">点的位置</param>
|
||
/// <returns></returns>
|
||
private static float PointToLine(Vector2 point1, Vector2 point2, Vector2 position)
|
||
{
|
||
float space = 0.0f;
|
||
float a, b, c;
|
||
a = Vector2.Distance(point1, point2);// 线段的长度
|
||
b = Vector2.Distance(point1, position);// position到点point1的距离
|
||
c = Vector2.Distance(point2, position);// position到point2点的距离
|
||
if (c <= 0.000001f || b <= 0.000001f)
|
||
{
|
||
space = 0.0f;
|
||
return space;
|
||
}
|
||
if (a <= 0.000001f)
|
||
{
|
||
space = b;
|
||
return space;
|
||
}
|
||
if (c * c >= a * a + b * b)
|
||
{
|
||
space = b;
|
||
return space;
|
||
}
|
||
if (b * b >= a * a + c * c)
|
||
{
|
||
space = c;
|
||
return space;
|
||
}
|
||
float p = (a + b + c) / 2;// 半周长
|
||
float s =Mathf.Sqrt(p * (p - a) * (p - b) * (p - c));// 海伦公式求面积
|
||
space = 2.0f * s / a;// 返回点到线的距离(利用三角形面积公式求高)
|
||
return space;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 异步加载场景
|
||
/// </summary>
|
||
/// <param name="sceneName"></param>
|
||
public static void LoadSceneAsync(string sceneName, System.Action LoadedCall)
|
||
{
|
||
GameObject loadSceneUIPrefab = Resources.Load<GameObject>("LoadScenePanelPrefab");
|
||
GameObject parentGeo = GameObject.Find("Canvas");
|
||
if (parentGeo == null)
|
||
{
|
||
WDebug.LogError("场景中没找到Canvas画布无法创建弹窗!!");
|
||
return;
|
||
}
|
||
GameObject loadSceneGeo = GameObject.Instantiate(loadSceneUIPrefab, parentGeo.transform);
|
||
loadSceneGeo.GetComponent<LoadScenesAsync>().LoadSceneAsync(sceneName, LoadedCall);
|
||
}
|
||
/// <summary>
|
||
/// 同步加载场景
|
||
/// </summary>
|
||
/// <param name="sceneName"></param>
|
||
//public static void LoadScene(string sceneName)
|
||
//{
|
||
// SceneManager.LoadScene(sceneName);
|
||
//}
|
||
|
||
/// <summary>
|
||
/// 深层查找需要的物体
|
||
/// </summary>
|
||
/// <param name="GeoName"></param>
|
||
/// <returns></returns>
|
||
public static GameObject GetGeoByName(Transform[] parentTran, string GeoName,bool findHindAble=false)
|
||
{
|
||
for (int i = 0; i < parentTran.Length; i++)
|
||
{
|
||
foreach (Transform t in parentTran[i].GetComponentsInChildren<Transform>(findHindAble))
|
||
{
|
||
if (t.name.Equals(GeoName))
|
||
{
|
||
return t.gameObject;
|
||
}
|
||
}
|
||
}
|
||
|
||
return null;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 打印某个方法执行时间
|
||
/// </summary>
|
||
/// <param name="call"></param>
|
||
public static void PrintExecuteTime(Action call)
|
||
{
|
||
System.Diagnostics.Stopwatch timer = System.Diagnostics.Stopwatch.StartNew();
|
||
timer.Start();
|
||
call?.Invoke();
|
||
timer.Stop();
|
||
WDebug.Log($"<color=#0000ff>{timer.Elapsed.TotalMilliseconds}</color>");
|
||
}
|
||
|
||
public static void LoadSpriteByURL(string url, Action<Sprite> SetCall)
|
||
{
|
||
if (string.IsNullOrEmpty(url)) return;
|
||
MonoManager.Instance.StartCoroutine(LoadSpriteCoroutine(url, SetCall));
|
||
}
|
||
private static IEnumerator LoadSpriteCoroutine(string url,Action<Sprite> SetSpriteCall)
|
||
{
|
||
// 使用UnityWebRequestTexture来获取图片纹理
|
||
UnityWebRequest m_webrequest = UnityWebRequestTexture.GetTexture(url);
|
||
yield return m_webrequest.SendWebRequest();
|
||
// 检查下载是否成功
|
||
if (m_webrequest.result != UnityWebRequest.Result.Success)
|
||
{
|
||
// 打印错误信息
|
||
WDebug.LogError("Failed to download image");
|
||
}
|
||
else
|
||
{
|
||
// 从下载处理器获取纹理
|
||
Texture2D tex = ((DownloadHandlerTexture)m_webrequest.downloadHandler).texture;
|
||
Sprite createSprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
|
||
}
|
||
}
|
||
|
||
public static void StartScreenshot(string pictureTempFile, Action callBack)
|
||
{
|
||
MonoManager.Instance.StartCoroutine(CaptureScreenshot(pictureTempFile, callBack));
|
||
}
|
||
private static IEnumerator CaptureScreenshot(string pictureTempFile, Action callBack)
|
||
{
|
||
|
||
// 等待渲染结束
|
||
yield return new WaitForEndOfFrame();
|
||
|
||
// 创建一个新的Texture2D对象,尺寸为屏幕当前分辨率
|
||
Texture2D screenImage = new Texture2D(Screen.width, Screen.height);
|
||
|
||
// 读取屏幕上当前帧的像素
|
||
screenImage.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
|
||
screenImage.Apply();
|
||
|
||
// 如果需要保存截图到文件
|
||
byte[] bytes = screenImage.EncodeToPNG();
|
||
#if UNITY_EDITOR || UNITY_STANDALONE
|
||
// 保存至本地(适用于 PC 端)
|
||
//string filePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "/实训报告.png";
|
||
//string filePath = pictureTempFile;
|
||
//File.WriteAllBytes(filePath, bytes);
|
||
//WDebug.Log("Screenshot saved to: " + filePath);
|
||
FileHandle.CreateFile(pictureTempFile, bytes);
|
||
#elif UNITY_WEBGL
|
||
// 将截图转换为 Base64 字符串
|
||
string base64 = Convert.ToBase64String(bytes);
|
||
string js = "var a = document.createElement('a'); document.body.appendChild(a); a.download = '实训报告.png'; a.href = 'data:image/png;base64," + base64 + "'; a.click();";
|
||
Application.ExternalEval(js);
|
||
#endif
|
||
callBack?.Invoke();
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 字符串统计
|
||
/// 一个汉字算两个字符,其他算一个
|
||
/// </summary>
|
||
/// <param name="text"></param>
|
||
/// <returns></returns>
|
||
public static int GetTransCharNum(string text)
|
||
{
|
||
int totalNum = 0;
|
||
char[] alrText = text.ToCharArray();
|
||
foreach (var item in alrText)
|
||
{
|
||
totalNum += SingleCharTrans(item);
|
||
}
|
||
return totalNum;
|
||
}
|
||
/// <summary>
|
||
/// 单个字符判断
|
||
/// </summary>
|
||
/// <param name="singChar"></param>
|
||
/// <returns></returns>
|
||
private static int SingleCharTrans(char singChar)
|
||
{
|
||
int leng = System.Text.Encoding.UTF8.GetBytes(singChar.ToString()).Length;
|
||
//WDebug.Log(singChar.ToString() + "_" + System.Text.Encoding.UTF8.GetBytes(singChar.ToString()).Length);
|
||
if (leng >= 2)
|
||
{
|
||
leng = 2;
|
||
}
|
||
return leng;
|
||
}
|
||
|
||
public static void LoadAudio(string filePath, Action<AudioClip> callBack)
|
||
{
|
||
MonoManager.Instance.StartCoroutine(LoadAudioIE(filePath, callBack));
|
||
}
|
||
|
||
private static IEnumerator LoadAudioIE(string filePath, Action<AudioClip> callBack)
|
||
{
|
||
using (UnityWebRequest www = UnityWebRequestMultimedia.GetAudioClip(filePath, AudioType.MPEG))
|
||
{
|
||
yield return www.SendWebRequest();
|
||
|
||
if (www.result != UnityWebRequest.Result.Success)
|
||
{
|
||
WDebug.LogError(www.error);
|
||
callBack?.Invoke(null);
|
||
}
|
||
else
|
||
{
|
||
AudioClip audioClip = DownloadHandlerAudioClip.GetContent(www);
|
||
callBack?.Invoke(audioClip);
|
||
//audioSource.clip = audioClip;
|
||
//audioSource.Play();
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|