Skip to content

Instantly share code, notes, and snippets.

@nirleka
Created November 26, 2015 08:39
Show Gist options
  • Save nirleka/a1e8afcb652ceb710fe3 to your computer and use it in GitHub Desktop.
Save nirleka/a1e8afcb652ceb710fe3 to your computer and use it in GitHub Desktop.
Unity5 Preview Selected Camera
using UnityEngine;
using UnityEditor;
using System.Collections;
// taken from http://answers.unity3d.com/answers/312489/view.html and modified to C# Unity 5
// put it in editor folder
// select a camera then pressing Shift+C or in Window/Preview Selected Camera.
public class CameraPreview : EditorWindow
{
Camera camera;
RenderTexture renderTexture;
[MenuItem("Window/Preview Selected Camera")]
static void Init (){
EditorWindow editorWindow = GetWindow(typeof(CameraPreview));
editorWindow.autoRepaintOnSceneChange = true;
editorWindow.Show();
editorWindow.titleContent.text = "Camera Preview";
}
void Awake ()
{
camera = FindObjectOfType<Camera>();
CreateRenderTexture();
}
void Update (){
if(camera != null) {
camera.targetTexture = renderTexture;
camera.Render();
camera.targetTexture = null;
}
if(renderTexture.width != position.width || renderTexture.height != position.height)
renderTexture = new RenderTexture((int)position.width, (int)position.height, 0, RenderTextureFormat.ARGB32);
}
void OnSelectionChange (){
GameObject obj = Selection.activeGameObject;
if (obj!=null && obj.GetComponent<Camera>()) camera = obj.GetComponent<Camera>();
CreateRenderTexture();
}
void CreateRenderTexture (){
renderTexture = new RenderTexture((int)position.width, (int)position.height, 0, RenderTextureFormat.ARGB32);
}
void OnGUI (){
GUI.DrawTexture(new Rect(.0f, .0f, position.width, position.height), renderTexture);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment