150 lines
5.6 KiB
C#
150 lines
5.6 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using UnityEngine.SceneManagement;
|
||
/********************************************************************************
|
||
*Create By CG
|
||
*Function 通用工具方法
|
||
*********************************************************************************/
|
||
namespace ZXK.UTility
|
||
{
|
||
public static class UtilitiesMng
|
||
{
|
||
/// <summary>
|
||
/// 延迟执行某个方法
|
||
/// </summary>
|
||
/// <param name="time"></param>
|
||
/// <param name="action"></param>
|
||
/// <returns></returns>
|
||
public static IEnumerator WaitForFunc(float time, System.Action action)
|
||
{
|
||
yield return new WaitForSeconds(time);
|
||
action();
|
||
}
|
||
/// <summary>
|
||
/// Tet2d转精灵图片
|
||
/// </summary>
|
||
/// <param name="img"></param>
|
||
/// <returns></returns>
|
||
public static Sprite Texture2Sprite(Texture2D img)
|
||
{
|
||
return Sprite.Create(img, new Rect(0, 0, img.width, img.height), Vector2.zero);
|
||
}
|
||
/// <summary>
|
||
/// 精灵图片转txt
|
||
/// </summary>
|
||
/// <param name="sprite"></param>
|
||
/// <returns></returns>
|
||
public static Texture2D Sprite2Texture(Sprite sprite)
|
||
{
|
||
var targetTex = new Texture2D((int)sprite.rect.width, (int)sprite.rect.height);
|
||
var pixels = sprite.texture.GetPixels(
|
||
(int)sprite.textureRect.x,
|
||
(int)sprite.textureRect.y,
|
||
(int)sprite.textureRect.width,
|
||
(int)sprite.textureRect.height);
|
||
targetTex.SetPixels(pixels);
|
||
targetTex.Apply();
|
||
return targetTex;
|
||
}
|
||
/// <summary>
|
||
/// Texture转换成Texture2D
|
||
/// </summary>
|
||
/// <param name="texture"></param>
|
||
/// <returns></returns>
|
||
public static Texture2D TextureToTexture2D(Texture texture)
|
||
{
|
||
Texture2D texture2D = new Texture2D(texture.width, texture.height, TextureFormat.RGBA32, false);
|
||
RenderTexture currentRT = RenderTexture.active;
|
||
RenderTexture renderTexture = RenderTexture.GetTemporary(texture.width, texture.height, 32);
|
||
Graphics.Blit(texture, renderTexture);
|
||
|
||
RenderTexture.active = renderTexture;
|
||
texture2D.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
|
||
texture2D.Apply();
|
||
|
||
RenderTexture.active = currentRT;
|
||
RenderTexture.ReleaseTemporary(renderTexture);
|
||
|
||
return texture2D;
|
||
}
|
||
/// <summary>
|
||
/// [颜色:16进制转成RGB]
|
||
/// </summary>
|
||
/// <param name="strColor">设置16进制颜色 [返回RGB]</param>
|
||
/// <returns></returns>
|
||
public static Color ColorHx16toRGB(string strHxColor)
|
||
{
|
||
|
||
Color outColor = new Color();
|
||
try
|
||
{
|
||
int r = System.Convert.ToInt32(strHxColor.Substring(0, 2), 16);
|
||
int g = System.Convert.ToInt32(strHxColor.Substring(2, 2), 16);
|
||
int b = System.Convert.ToInt32(strHxColor.Substring(4, 2), 16);
|
||
outColor = new Color(r / 255.0f, g / 255.0f, b / 255.0f, 1.0f);
|
||
}
|
||
catch
|
||
{//设为黑色
|
||
outColor = Color.black;
|
||
}
|
||
return outColor;
|
||
}
|
||
/// <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>");
|
||
}
|
||
|
||
/// <summary>
|
||
/// 异步加载场景
|
||
/// </summary>
|
||
/// <param name="sceneName"></param>
|
||
public static void LoadSceneAsync(string sceneName)
|
||
{
|
||
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<LoadScenes>().LoadSceneAsync(sceneName);
|
||
}
|
||
/// <summary>
|
||
/// 同步加载场景
|
||
/// </summary>
|
||
/// <param name="sceneName"></param>
|
||
public static void LoadScene(string sceneName)
|
||
{
|
||
SceneManager.LoadScene(sceneName);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 应用退出的几种方式【根据需要自行选择】
|
||
/// </summary>
|
||
public static void CustomQuit()
|
||
{
|
||
Resources.UnloadUnusedAssets();// 卸载未使用的资产
|
||
#if UNITY_EDITOR
|
||
UnityEditor.EditorApplication.isPlaying = false;// 在编译状态游戏退出
|
||
#else
|
||
Application.Quit();//正常退出(在打包后使用,不能再编译状态下使用)
|
||
#endif
|
||
//System.Diagnostics.Process.GetCurrentProcess().Kill();// 关掉与当前活动相关的进程
|
||
//System.Environment.Exit(0);// 终止当前进程并为基础操作系统提供指定的退出代码。
|
||
//PlayerPrefs.DeleteAll();//删除储存在注册表中的持久化储存的数据
|
||
//System.GC.Collect();// 强制立刻回收
|
||
}
|
||
}
|
||
}
|