- Home /
ReadPixels is capturing GUI, how to get a screencap without GUI
So I am trying to take a screenshot and then have it display in the top corner in a GUI texture, the 1st person character is basically supposed to be taking a photo with a camera then having that shot displayed in the corner gui.
I have the code working but the screenshot that goes to the gui texture actually contains the gui texture in it (or the previous photo) How do I avoid capturing the gui?
Answer by phodges · Oct 18, 2012 at 08:03 PM
How about rendering the view to a texture, using a camera that contains no GUI?
EDIT: Added a simple RenderTexture demo
I'll add a couple of code snippets to demonstrate something along these lines using RenderTextures. Firstly, some setup information:
Two cameras in the scene: one for your regular view and another to take snapshots.
The snapshot camera should be placed wherever the viewpoint for these photographs is expected to be
The snapshot camera should have irrelevant items (such as GUILayer and AudioListener) removed and the Snapshot component added
Something needs to prompt the snapshots to be taken. I've provided a "Snapper" for this to support the logic: attach that to whichever object you want it on and assign its 'Snapshot' property correctly.
Pressing the 'S' key will cause a snapshot to be taken and then rendered in the OnGUI call. It could equally well be blitted using GL methods if that works better for you, but this seems more relevant to your question.
// Snapshot.cs
using UnityEngine; using System.Collections;
[RequireComponent(typeof(Camera))]
public class Snapshot : MonoBehaviour {public delegate void SnapshotEvent(Texture r); public int texWidth = 256; public int texHeight = 256; RenderTexture _renderTexture; SnapshotEvent OnSnapshotReady; bool _stop; // Use this for initialization void Start () { RenderTexture rt = new RenderTexture(texWidth,texHeight,24,RenderTextureFormat.ARGB32,RenderTextureReadWrite.Default); _renderTexture = rt; camera.targetTexture = rt; camera.enabled = false; } void Update() { if (_stop) { camera.enabled = false; } } void OnPostRender() { _stop = true; if (null != OnSnapshotReady){ OnSnapshotReady(_renderTexture); } } public void TakeSnapshot(SnapshotEvent cb) { OnSnapshotReady = cb; camera.enabled = true; _stop = false; } } // Snapper.cs using UnityEngine; using System.Collections; public class Snapper : MonoBehaviour { public Snapshot snapshot = null; Texture _snap = null; // Update is called once per frame void Update () { if (null != snapshot && Input.GetKeyDown(KeyCode.S)){ snapshot.TakeSnapshot(OnSnapshotReady); } } void OnSnapshotReady(Texture rt){ _snap = rt; } void OnGUI() { if (null != _snap){ GUILayout.Label("Snapshot ready"); Rect r = new Rect(10f,10f,256f,256f); GUI.DrawTexture(r,_snap); }else{ GUILayout.Label("No snapshot available"); } } }
Do you $$anonymous$$d explaining how I specify which camera I want to capture pixels?
Does this work on Unity free? I believe it doesnt, as it uses RenderTexture, right?.
Thats a superb Idea bro. I was searching for something like a different operation than read per pixel. U rock.