DCS/ruiyiweiUX/Assets/GeneralTools/Scripts/Always/UtilityTool.cs

174 lines
5.8 KiB
C#
Raw Normal View History

2026-06-09 13:59:11 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace GeneralTools
{
/// <summary>
/// 工程里 通用的方法写这里
/// </summary>
public class UtilityTool : MonoBehaviour
{
/// <summary>
/// 删除transform下所有子物体
/// </summary>
/// <param name="tran"></param>
public static void DeleteChild(Transform tran)
{
for (int i = tran.childCount - 1; i >= 0; i--)
{
DestroyImmediate(tran.GetChild(i).gameObject);
}
}
/// <summary>
/// 截取某摄像机画面
/// </summary>
/// <param name="camera"></param>
/// <param name="rect"></param>
/// <returns></returns>
public static Texture2D GetScreenShot(Camera camera, Rect rect)
{
if (camera == null)
{
Debug.LogError("camera is null");
return null;
}
// 创建一个RenderTexture对象
RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, 0);
// 临时设置相关相机的targetTexture为rt, 并手动渲染相关相机
camera.targetTexture = rt;
camera.Render();
//ps: --- 如果这样加上第二个相机,可以实现只截图某几个指定的相机一起看到的图像。
//ps: camera2.targetTexture = rt;
//ps: camera2.Render();
//ps: -------------------------------------------------------------------
// 激活这个rt, 并从中中读取像素。
RenderTexture.active = rt;
Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);
screenShot.ReadPixels(rect, 0, 0);// 注这个时候它是从RenderTexture.active中读取像素
screenShot.Apply();
// 重置相关参数以使用camera继续在屏幕上显示
camera.targetTexture = null;
//ps: camera2.targetTexture = null;
RenderTexture.active = null; // JC: added to avoid errors
Destroy(rt);
return screenShot;
}
public enum TexStyle
{
JPG,
PNG,
EXR
}
/// <summary>
/// Texture2D 转换为 byte[]
/// </summary>
/// <param name="tex"></param>
/// <param name="style"></param>
/// <returns></returns>
public static byte[] TexConvertBytes(Texture2D tex, TexStyle style)
{
byte[] bytes = null; ;
switch (style)
{
case TexStyle.JPG:
bytes = tex.EncodeToJPG();
break;
case TexStyle.PNG:
bytes = tex.EncodeToPNG();
break;
case TexStyle.EXR:
bytes = tex.EncodeToEXR();
break;
}
return bytes;
}
/// <summary>
/// bytes数组 转 Texture2D
/// </summary>
/// <param name="bytes"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public static Texture2D BytesConvertTex2D(byte[] bytes, int width, int height)
{
Texture2D tex = new Texture2D(width, height);
try
{
tex.LoadImage(bytes);
}
catch (Exception)
{
Debug.LogError("bytes转图片失败");
return null;
}
return tex;
}
//寻找子物体,用于初始化
public static Transform FindChild(Transform trans, string childName)
{
Transform child = trans.Find(childName);
if (child != null)
{
return child;
}
int count = trans.childCount;
Transform go = null;
for (int i = 0; i < count; ++i)
{
child = trans.GetChild(i);
go = FindChild(child, childName);
if (go != null)
return go;
}
return null;
}
public static T FindChild<T>(Transform trans, string childName) where T : Component
{
Transform go = FindChild(trans, childName);
if (go == null)
return null;
return go.GetComponent<T>();
}
/// <summary>
/// 返回trans下所有子物体的T集合
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="trans"></param>
/// <returns></returns>
public static List<T> CollectComponent<T>(Transform trans)
{
List<T> tempList = new List<T>();
for (int i = 0; i < trans.childCount; i++)
{
T _t = trans.GetChild(i).GetComponent<T>();
if (_t != null)
{
tempList.Add(_t);
}
if (trans.GetChild(i).childCount > 0)
{
tempList = tempList.Concat(CollectComponent<T>(trans.GetChild(i))).ToList();
}
}
return tempList;
}
public static IEnumerator DelayToInvokeDo(Action action, float delaySeconds)
{
yield return new WaitForSeconds(delaySeconds);
action();
}
public static IEnumerator DelayToInvokeDo(Action<object[]> action, float delaySeconds, params object[] objs)
{
yield return new WaitForSeconds(delaySeconds);
action(objs);
}
}
}