Rendering a Camera in an Editor window displays its RenderTexture automatically?

After a few hours of experimenting with ReadPixels() and GUI.DrawTexture() to render a Camera to an EditorWindow I was frustrated and not getting anywhere. There were 2 Textures being rendered! So I deleted all of the calls to the above mentioned functions and it works perfectly.

Now I’m no expert in magic, but this looks like magic to me. Here is the script:


using UnityEditor;
using UnityEngine;
using System.Collections;

public class CollisionPainter : EditorWindow 
{
	//game object that holds the camera
	private GameObject gameObject;

	//direct reference to the game objects camera
	private Camera cam;

	static CollisionPainter window;

	[MenuItem("Custom Windows/Collision Painter")]
	static void ShowWindow() 
	{
		window = 
			(CollisionPainter)EditorWindow.GetWindow(typeof(CollisionPainter));
		window.init();
	}

	public void init()
	{
		//set game object's attribs
		//====================================================================
		gameObject = new GameObject("Collision Painter Camera");
		gameObject.transform.position = 
			new Vector3( 0.0f, 0.0f ,
			            GameObject.FindGameObjectWithTag("MainCamera")
			            .transform.position.z);
		gameObject.AddComponent<Camera>();

		//set camera's attribs
		//===================================================================
		cam = gameObject.camera;
		cam.backgroundColor = Color.green;
		cam.enabled = false;
		cam.orthographic = true;
		cam.orthographicSize = 
			GameObject.FindGameObjectWithTag("MainCamera")
				.GetComponent<Camera>().orthographicSize;
	}
	
	void OnGUI() 
	{   

		cam.targetTexture = new RenderTexture((int)window.position.width,
		                                      (int)window.position.height,24,
		                                      RenderTextureFormat.ARGB32);
		cam.Render();
	}
	
	void OnDestroy()
	{
		DestroyImmediate(gameObject);
	}
}

And Its resulting Window:

Is this normal behavior? How is the RenderTexture being displayed in the window?

thanks - Ross.

This is magic and it solved a problem I was stuck on for 6 hours =)

I did have to make some changes in the code for it to work on 5.1

I added

GUI.DrawTexture(new Rect(0, 0, position.width, position.height), cam.targetTexture);

and changed out the deprecated call to the camera with the current .GetComponent(); call

using UnityEditor;
using UnityEngine;
using System.Collections;
using Assets.Editor;

public class CollisionPainter : EditorWindow
{
	//game object that holds the camera
	private GameObject gameObject;

	//direct reference to the game objects camera
	private Camera cam;

	static CollisionPainter window;

	[MenuItem("Custom Windows/Collision Painter")]
	static void ShowWindow()
	{
		window =
			(CollisionPainter)EditorWindow.GetWindow(typeof(CollisionPainter));
		window.init();
	}

	public void init()
	{
		//set game object's attribs
		//====================================================================
		gameObject = new GameObject("Collision Painter Camera");
		gameObject.transform.position =
			new Vector3(0.0f, 0.0f,
						GameObject.FindGameObjectWithTag("MainCamera")
						.transform.position.z);
		gameObject.AddComponent<Camera>();

		//set camera's attribs
		//===================================================================
		cam = gameObject.GetComponent<Camera>();//.camera;
		cam.backgroundColor = Color.green;
		cam.enabled = false;
		cam.orthographic = true;
		cam.orthographicSize =
			GameObject.FindGameObjectWithTag("MainCamera")
				.GetComponent<Camera>().orthographicSize;
	}

	void OnGUI()
	{
		if (window == null)
		{
			window =
			(CollisionPainter)EditorWindow.GetWindow(typeof(CollisionPainter));
		}

		cam.targetTexture = new RenderTexture((int)window.position.width,
											  (int)window.position.height, 24,
											  RenderTextureFormat.ARGB32);		

		cam.Render();

		GUI.DrawTexture(new Rect(0, 0, position.width, position.height), cam.targetTexture);		
	}

	void OnDestroy()
	{
		DestroyImmediate(gameObject);
	}
}