- Home /
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.
I saw this as well, don't quite understand why... were you able to figure it out?
Sorry... I wasnt. I Just ended up accepting it and moving on. I'm still curious however. So if anyone has figured it out I would love to know.
Answer by DStrike · Oct 28, 2015 at 01:20 PM
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);
}
}
I'm glad whatever was here helped :) Am I correct in saying that in 5.1 the editor window is no longer being drawn to without a call to GUI.DrawTexture()?. Can we assume it was unintended then?
Yeah the window didn't draw anything until I added that. It seems to imply what you inferred that it wasn't intentional. I was able to figure out that a texture was being recorded from the camera so then it was trivial to just draw it. None of the other things I tried even got the texture