- Home /
How to change size of camera preview in Editor?
Hi,
When you have a camera selected within the editor, a small screen is shown with the "Camera Preview".
I was wondering if it's possible to change the size of that window, because it's really small for what I have to do.
Thanks in advance
Answer by save · Sep 03, 2012 at 02:09 PM
I'm not sure if you can resize it, it seems rather static. You could however script your own camera preview.
Make a folder called Editor, create a Javascript within that folder and name it CameraPreview.
Paste this code:
#pragma strict
class CameraPreview extends EditorWindow {
var camera : Camera = Camera.main;
var renderTexture : RenderTexture;
@MenuItem("Window/Preview Selected Camera #c")
static function Init () {
var editorWindow : EditorWindow = GetWindow(typeof(CameraPreview));
editorWindow.autoRepaintOnSceneChange = true;
editorWindow.Show();
editorWindow.title = "Camera Preview";
}
function Awake () {
CreateRenderTexture();
}
function Update () {
if(camera != null) {
camera.targetTexture = renderTexture;
camera.Render();
camera.targetTexture = null;
}
if(renderTexture.width != position.width || renderTexture.height != position.height)
renderTexture = new RenderTexture(position.width, position.height, RenderTextureFormat.ARGB32);
}
function OnSelectionChange () {
var obj : GameObject = Selection.activeGameObject;
if (obj!=null && obj.camera) camera = obj.camera;
CreateRenderTexture();
}
function CreateRenderTexture () {
renderTexture = new RenderTexture(position.width, position.height, RenderTextureFormat.ARGB32);
}
function OnGUI () {
GUI.DrawTexture(new Rect(.0, .0, position.width, position.height), renderTexture);
}
}
You will find the new window by pressing Shift+C or in Window/Preview Selected Camera.
$$anonymous$$ade some small changes as the render texture went unassigned when playing/stopping. Good luck!
Answer by ChrisVD · Sep 05, 2013 at 07:28 PM
Why not just undock the "Game" view tab, which always shows the camera view, and resize it to as large or small as you wish or move it to another monitor, etc.?
This is not an effective solution if you are running multiple cameras in your game and especially if you are generating a lot of things at runtime and need to make edits outside of runtime.
Answer by Aranda · Mar 29, 2016 at 12:14 PM
Thanks @save for the solution. I've made it resize correctly and converted to C# in case somebody prefers that:
using UnityEngine;
using UnityEditor;
class CameraPreview : EditorWindow
{
Camera camera;
RenderTexture renderTexture;
[MenuItem("Custom Menu/Preview Camera")]
static void Init ()
{
var editorWindow = (EditorWindow)GetWindow<CameraPreview>(typeof(CameraPreview));
editorWindow.autoRepaintOnSceneChange = true;
editorWindow.titleContent = new GUIContent("Cam Preview");
editorWindow.Show();
}
void Awake()
{
camera = Camera.main;
}
void Update()
{
if (camera != null)
{
EnsureRenderTexture();
camera.renderingPath = RenderingPath.UsePlayerSettings;
camera.targetTexture = renderTexture;
camera.Render();
camera.targetTexture = null;
}
}
void OnSelectionChange()
{
var obj = Selection.activeGameObject;
if (obj == null)
return;
var cam = obj.GetComponent<Camera>();
if (cam == null)
return;
camera = cam;
}
void EnsureRenderTexture()
{
if (renderTexture == null
|| (int)position.width != renderTexture.width
|| (int)position.height != renderTexture.height)
{
renderTexture = new RenderTexture((int)position.width, (int)position.height, 24, RenderTextureFormat.ARGB32, RenderTextureReadWrite.sRGB);
}
}
void OnGUI()
{
if (renderTexture != null)
GUI.DrawTexture(new Rect(0.0f, 0.0f, position.width, position.height), renderTexture);
}
}
Answer by stefanseibert · Nov 25, 2014 at 09:12 AM
If you want to use that solution today, be aware that you have to add a depth argument for constructing a RenderTexture. See http://docs.unity3d.com/ScriptReference/RenderTexture-ctor.html
Answer by Torx · May 05, 2015 at 03:13 AM
Hi, i don't know if it's your solution, it's not perfect but very simple (it's an exemple for phone application between portrait and landspace) :
void Start()
{
#if UNITY_EDITOR
Camera.main.rect = new Rect(0.3f, 0.0f, 0.4f, 1.0f);
#else
Screen.orientation = ScreenOrientation.Portrait;
#endif
}
void OnDestroy()
{
#if UNITY_EDITOR
Camera.main.rect = new Rect(0.0f, 0.0f, 1.0f, 1.0f);
#else
Screen.orientation = ScreenOrientation.LandSpace;
#endif
}