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
{
///
/// 延迟执行某个方法
///
///
///
///
public static IEnumerator WaitForFunc(float time, System.Action action)
{
yield return new WaitForSeconds(time);
action();
}
///
/// Tet2d转精灵图片
///
///
///
public static Sprite Texture2Sprite(Texture2D img)
{
return Sprite.Create(img, new Rect(0, 0, img.width, img.height), Vector2.zero);
}
///
/// 精灵图片转txt
///
///
///
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;
}
///
/// Texture转换成Texture2D
///
///
///
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;
}
///
/// [颜色:16进制转成RGB]
///
/// 设置16进制颜色 [返回RGB]
///
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;
}
///
/// 打印某个方法执行时间
///
///
public static void PrintExecuteTime(Action call)
{
System.Diagnostics.Stopwatch timer = System.Diagnostics.Stopwatch.StartNew();
timer.Start();
call?.Invoke();
timer.Stop();
UnityEngine.Debug.Log($"{timer.Elapsed.TotalMilliseconds}");
}
///
/// 异步加载场景
///
///
public static void LoadSceneAsync(string sceneName)
{
GameObject loadSceneUIPrefab = Resources.Load("LoadScenePanelPrefab");
GameObject parentGeo = GameObject.Find("Canvas");
if (parentGeo == null)
{
Debug.LogError("场景中没找到Canvas画布无法创建弹窗!!");
return;
}
GameObject loadSceneGeo = GameObject.Instantiate(loadSceneUIPrefab, parentGeo.transform);
loadSceneGeo.GetComponent().LoadSceneAsync(sceneName);
}
///
/// 同步加载场景
///
///
public static void LoadScene(string sceneName)
{
SceneManager.LoadScene(sceneName);
}
///
/// 应用退出的几种方式【根据需要自行选择】
///
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();// 强制立刻回收
}
}
}