153 lines
5.8 KiB
C#
Raw Normal View History

2025-02-12 08:43:33 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.SceneManagement;
/********************************************************************************
*Create By CG
*Function
*********************************************************************************/
namespace ZXK.UTility
{
public static class UtilitiesMng
{
/// <summary>
/// 应用退出的几种方式【根据需要自行选择】
/// </summary>
public static void CustomQuit()
{
Resources.UnloadUnusedAssets();// 卸载未使用的资产
Application.Quit();//正常退出(在打包后使用,不能再编译状态下使用)
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;// 在编译状态游戏退出
#endif
System.Diagnostics.Process.GetCurrentProcess().Kill();// 关掉与当前活动相关的进程
System.Environment.Exit(0);// 终止当前进程并为基础操作系统提供指定的退出代码。
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)
{
Debug.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="call"></param>
public static void PrintExecuteTime(Action call)
{
System.Diagnostics.Stopwatch timer = System.Diagnostics.Stopwatch.StartNew();
timer.Start();
call?.Invoke();
timer.Stop();
UnityEngine.Debug.Log($"<color=#0000ff>{timer.Elapsed.TotalMilliseconds}</color>");
}
public static void StartScreenshot(string pictureTempFile, Action<string> callBack)
{
Framework.MonoManager.Instance.StartCoroutine(CaptureScreenshot(pictureTempFile, callBack));
}
private static IEnumerator CaptureScreenshot(string pictureTempFile, Action<string> callBack)
{
// 等待渲染结束
yield return new WaitForEndOfFrame();
// 等待渲染结束
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 = pictureTempFile;
Stream stream = new MemoryStream(bytes);
ZXK.BYSS.AppManagement._ImgDataArray.Add(Path.GetFileName(pictureTempFile), stream);
callBack?.Invoke(pictureTempFile);
#elif UNITY_WEBGL
//生成Base64
string base64 = "data:image/png;base64," + Texture2DToBase64(screenImage);
callBack?.Invoke(base64);
#endif
}
public static string Texture2DToBase64(Texture2D texture)
{
byte[] data = texture.EncodeToPNG();
return Convert.ToBase64String(data);
}
}
}