using System; using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; namespace GeneralTools { /// /// 工程里 通用的方法写这里 /// public class UtilityTool : MonoBehaviour { /// /// 删除transform下所有子物体 /// /// public static void DeleteChild(Transform tran) { for (int i = tran.childCount - 1; i >= 0; i--) { DestroyImmediate(tran.GetChild(i).gameObject); } } /// /// 截取某摄像机画面 /// /// /// /// 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 } /// /// Texture2D 转换为 byte[] /// /// /// /// 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; } /// /// bytes数组 转 Texture2D /// /// /// /// /// 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(Transform trans, string childName) where T : Component { Transform go = FindChild(trans, childName); if (go == null) return null; return go.GetComponent(); } /// /// 返回trans下所有子物体的T集合 /// /// /// /// public static List CollectComponent(Transform trans) { List tempList = new List(); for (int i = 0; i < trans.childCount; i++) { T _t = trans.GetChild(i).GetComponent(); if (_t != null) { tempList.Add(_t); } if (trans.GetChild(i).childCount > 0) { tempList = tempList.Concat(CollectComponent(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 action, float delaySeconds, params object[] objs) { yield return new WaitForSeconds(delaySeconds); action(objs); } } }