相关API的使用测试
1) 测试Camera.Render()是裁剪还是缩放
using UnityEngine; using UnityEngine.UI; public class CaptureCameraTest : MonoBehaviour { private RenderTexture m_TempRT; void Start() { m_TempRT = RenderTexture.GetTemporary(900, 800, 24); Camera.main.targetTexture = m_TempRT; Camera.main.Render(); //不是相机的整个画面绘制到RT上, 只有RT宽x高那么大的区域绘制在RT上 Camera.main.targetTexture = null; var rawImg = GetComponent<RawImage>(); rawImg.texture = m_TempRT; rawImg.SetNativeSize(); } void OnDestroy() { if (null != m_TempRT) { RenderTexture.ReleaseTemporary(m_TempRT); } } }
2) 测试Graphics.Blit是裁剪还是缩放
using UnityEngine; using UnityEngine.UI; public class CaptureCameraTest : MonoBehaviour { private RenderTexture m_TempRT; void Start() { m_TempRT = RenderTexture.GetTemporary(1280, 800, 24); Camera.main.targetTexture = m_TempRT; Camera.main.Render(); //不是相机的整个画面绘制到RT上, 只有RT宽x高那么大的区域绘制在RT上 Camera.main.targetTexture = null; var rt2 = RenderTexture.GetTemporary(600, 800, 24); Graphics.Blit(m_TempRT, rt2); //1280的画面填充在600宽的贴图上, 会被压扁 RenderTexture.ReleaseTemporary(m_TempRT); m_TempRT = rt2; var rawImg = GetComponent<RawImage>(); rawImg.texture = m_TempRT; rawImg.SetNativeSize(); } void OnDestroy() { if (null != m_TempRT) { RenderTexture.ReleaseTemporary(m_TempRT); } } }
截屏
using UnityEngine; using UnityEngine.UI; public class CaptureCameraTest : MonoBehaviour { void Start() { var rawImg = GetComponent<RawImage>(); rawImg.texture = CaptureCamera(Camera.main, new Rect(0, 0, 1050, 800)); rawImg.SetNativeSize(); } public static Texture2D CaptureCamera(Camera srcCamera, Rect captureRect) { var tempCameraOutputRT = RenderTexture.GetTemporary(srcCamera.pixelWidth, srcCamera.pixelHeight, 24); var oldTargetTex = srcCamera.targetTexture; srcCamera.targetTexture = tempCameraOutputRT; srcCamera.Render(); srcCamera.targetTexture = oldTargetTex; var oldActiveRT = RenderTexture.active; Graphics.SetRenderTarget(tempCameraOutputRT); var resultTex = new Texture2D((int)captureRect.width, (int)captureRect.height); resultTex.ReadPixels(captureRect, 0, 0); //读取RT上要裁剪的区域, 写到Texture2D的(0, 0)处 resultTex.Apply(); Graphics.SetRenderTarget(oldActiveRT); RenderTexture.ReleaseTemporary(tempCameraOutputRT); return resultTex; } }
可以看到是从相机的左下角开始截的
标签:Camera,RenderTexture,TempRT,rawImg,截屏,targetTexture,var From: https://www.cnblogs.com/sailJs/p/16473285.html