I’m new to Unity and I have been hardstucked for a couple of days trying to figure out what I’m doing wrong.
My purpose is to take a screenshot of a “minimap” (actually is turretCamera in the code) that is in the top right corner of the screen. It is 768 pixels width and 432 pixels height.
I leave below my script, any help is appreciated:
void Start()
{
// Configurar el modelo y el trabajador
Model model = ModelLoader.Load(modelAsset);
worker = WorkerFactory.CreateWorker(WorkerFactory.Type.ComputePrecompiled, model);
// Configurar la render texture
cameraTopLeftCorner = turretCamera.ViewportToScreenPoint(new Vector3(0, 0, turretCamera.nearClipPlane));
cameraBottomRightCorner = turretCamera.ViewportToScreenPoint(new Vector3(1, 1, turretCamera.nearClipPlane));
targetTextureWidth = cameraBottomRightCorner.x - cameraTopLeftCorner.x;
targetTextureHeight = cameraBottomRightCorner.y - cameraTopLeftCorner.y;
renderTexture = new RenderTexture(Mathf.CeilToInt(targetTextureWidth), Mathf.CeilToInt(targetTextureHeight), 24);
tmpRenderTexture = turretCamera.targetTexture;
turretCamera.targetTexture = renderTexture;
// cameraa.targetTexture = null;
}
private void OnGUI()
{
// ************Screenshot camera************
Texture2D texture2D = new Texture2D(renderTexture.width, renderTexture.height, TextureFormat.RGB24, false);
RenderTexture.active = renderTexture;
Rect RoI = new Rect(cameraTopLeftCorner.x, cameraTopLeftCorner.y, targetTextureWidth, targetTextureHeight);
Debug.Log("ROI");
Debug.Log(RoI.xMin +", " + RoI.yMin);
Debug.Log(RoI.xMax + ", " + RoI.yMax);
texture2D.ReadPixels(RoI, 0, 0);
texture2D.Apply();
// ************Screenshot camera************
// rest of code...