using System; using UnityEngine; using UnityEngine.SceneManagement; namespace AlicizaX { /// /// 相机帮助类 /// [UnityEngine.Scripting.Preserve] public static class CameraHelper { /// /// 获取相机快照 /// /// 相机 /// 缩放比 public static Texture2D GetCaptureScreenshot(Camera main, float scale = 0.5f) { Rect rect = new Rect(0, 0, Screen.width * scale, Screen.height * scale); string name = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss"); RenderTexture renderTexture = RenderTexture.GetTemporary((int)rect.width, (int)rect.height, 0); renderTexture.name = SceneManager.GetActiveScene().name + "_" + renderTexture.width + "_" + renderTexture.height + "_" + name; main.targetTexture = renderTexture; main.Render(); RenderTexture.active = renderTexture; Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false) { name = renderTexture.name }; screenShot.ReadPixels(rect, 0, 0); screenShot.Apply(); main.targetTexture = null; RenderTexture.active = null; RenderTexture.ReleaseTemporary(renderTexture); return screenShot; } } }