Writing an Editor Window that mimics the Scene View

Forgot the screen shot. Note if I stretch the EditorWindow vertically the camera render also does not completely fill it in that axis, even though the DrawCamera call is using the current postion rect of the window which should be its full size.

Update: Resizing the Game Window changes the size of the render in my Editor Window, so for some reason it is using The Game View size instead of my Editor Windows size. In fact it just seems to be rendering the Game View instead of the camera I have added to my preview scene.

Another update. I got the camera to take up the whole EditorWindow by setting its hide flags to HideAndDontSave. However, this camera is still not displaying the PreviewScene, so I am left to believe that MoveGameObjectToScene is not working correctly.

The Handles.BeginGUI call should be unnecessary, since you’re already in a GUI context in OnGUI.

Are you sure that it is actually drawing the game view camera and not just that the game view and editor view cameras are in the same position/rotation?

I will attempt to make a test editor and troubleshoot this myself, because I don’t see anything obviously wrong with your code.

Hey, thank you but I think I figured it out. The issue was that the camera I created via code was still displaying the open scene rather than the preview scene. I just had to set camera.scene to the preview scene. I still have some stuff to work out but it seems to be working!

Also, you are right about Handles.BeginGUI. This is just leftover from the way I had been doing it before (without an editor).

Thank you so much for the help. It’s people like you that make this community great. I am going to try and exhaust all options the next time an issue comes up before posting, lol.

It could be some interplay with some other part of my code, but it looks like when using Handles.DrawCamera, you do have to include the Handles.BeginGUI. Not sure why, but without it everything gets messed up.

Yep, I was just about to post that I got it working with these parameters:

        previewScene = EditorSceneManager.NewPreviewScene();

        var camObj = new GameObject( "Camera" );
        cam = camObj.AddComponent<Camera>();
        cam.transform.position = new Vector3( 0, 0, -10 );
        cam.hideFlags = HideFlags.HideAndDontSave;
        cam.scene = previewScene;
        cam.enabled = false;
        SceneManager.MoveGameObjectToScene( camObj, previewScene );

Also, I just tripped across the undocumented class PreviewRenderUtility. Check it out, it will probably save you a lot of trouble:

PreviewRenderUtility renderUtils;

private void OnEnable () {
    renderUtils = new PreviewRenderUtility();
    renderUtils.AddSingleGO( GameObject.CreatePrimitive( PrimitiveType.Cube ) );
    renderUtils.camera.transform.position = new Vector3( 0, 0, -10 );
}

private void OnDisable () {
    renderUtils.Cleanup();
}

public void OnGUI () {
    var pos = position;
    pos.x = 0;
    pos.y = 0;
    renderUtils.BeginPreview( pos, EditorStyles.helpBox );
    renderUtils.Render();
    renderUtils.EndAndDrawPreview( pos );
}

Cool, I will take a look at it. This would be used instead of the PreviewScene, correct?

Your code still helped, I was not disabling the camera and it was causing the preview camera to be rendered in the Game View. So thanks again!

Yep, this is a wrapper around a PreviewScene. Takes care of the camera, and has its own lights as well.

ahhhh yes PreviewRenderUtility this is what was ringing a bell when I linked my previous reply

Well, I have everything displaying nicely in the window now. Thanks so much for your help!!

Now I just need to figure out how to recreate the scene view movement controls, i.e., panning, camera rotation, and all that jazz. I am trying to decipher the SceneViewMotion class to get some clues, but if anyone has a tutorial or some other aid to give please feel free to share!!

Bumping this thread once again.

I was previously making extensive use of the Handles class to draw some manipulable controls in the Scene View, however Handles is really meant to be used with the Scene View. With that said, I found that I can draw the handles in my custom editor by using the Handles.SetCamera method (and using the camera from the PreviewRenderUtility).

This works, but only when the editor window is a certain size. Anything smaller and the handles do not show up in the correct spot.

Positioning is wrong


Positioning is right

So this problem appears to be due to the aspect ratio of the preview camera not being updated when the windows width is changed. You will notice in the Scene View that the camera aspect (or maybe it’s field of view?) changes when the window is resized both horizontally (width) or vertically (height). However, with the preview utility, it is only adjusted when the window is resized vertically. This seems like a bug.

iirc its because PRU is intended to work as a square? the previewed object needs to be constrained in the viewport.

So if you grow the width of the preview window past 1:1 (width:height) it cant zoom in the object anymore, because its constrained by the height. likewise for height.

Have you tried making your preview window width shorter than the height of the window, then adjusting the height?

This was over a year ago, I worked on a PRU so my memory is very foggy sorry

1 Like

Thank you for the suggestions/advice. I tested making the window’s width smaller than the height and then adjusting the height. It does not affect anything. The aspect changes properly when the height is adjusted. Adjusting the width simply has no effect on the aspect, regardless of whether the width is smaller/larger/the same as the height.

Thanks again. If you recall anything else I would be delighted to try any other suggestions you might have!

1 Like

I actually just submitted this as a bug yesterday. I didn’t realize it was limited to the PreviewRenderUtility. I believe Handles malfunction when the camera aspect goes below 1.0.

I’m doing something very similar and my current scheme is just to duplicate SceneViewMotion and take out some of the stuff I don’t need, but that’s definitely a hack. Eager to hear if anyone has a better plan.

EDIT: Bug was reproduced and accepted (Case 1134881) so hopefully this will get fixed at some point.

Sorry for bumping this thread, but you may know the answer for my problem.
I’m currently making level editor and I’ve made preview area with Handles in window, but Handles are not interactive. I need that to make editon of 3d array easy for artists. I’ve managed to make them interactive twice by accdent, but after Unity restart it stopped working.
Does anyone know how to make Handles interactive?

I found out that to Event.current.mousePosition must be added an offset from bottom left corner of window to bottom left corner of preview area, also pixelRect and aspect of camera in PreviewRenderUtility must be updated to size of preview area in Layout pass.

I don’t know if you still have a problem with this, but for anyone looking for solution, Render in PreviewRenderUtility have parameter updatefov. Setting it to false will make Handles be in correct position, regardless of window size.

Thank you for the tip however this did not work for me.

Still can’t figure this out. Just to add to the discussion, the objects in my PreviewRenderUtility are getting cutoff, like so:

You can see how the right most cubes are not being shown fully, however anything rendered with Handles is beings shown (though not at the right position).

Again, this is only an issue when the width of the editor window is smaller than a certain value. The height has no effect.

Well, I at least fixed the issue with the cubes getting cut off. This is caused by some kind of error on Unity’s side when the editor’s width is reduced to below 1024. Previously I was using this code:

Handles.SetCamera(pos, SceneCamera);
where pos is the Editor Window’s position.

I changed this to:
Handles.SetCamera(new Rect(0, 0, pos.width + (1024f - pos.width), pos.height, SceneCamera);
when the editor’s width is less than 1024 and it at least appears fixed.

I think the Handle positions being off might be related to this as well, but I will need to investigate further to see if I can solve the problem.

5746126--603997--editorResizeError.gif

Okay, so what appears to be happening is, when the editor is resized below a certain value, the camera automatically adjust it’s aspect ratio, or at least some value is adjusted on the camera that results in the aspect ratio changing.

When the editor height is reduced, this works as expected, but when the editor width is reduced, the aspect doesn’t appear to be changing correctly. You can see this in the GIF above by watching the console log. Only a few aspect ratio changes are recorded when adjusting the width, whereas adjusting the height changes the aspect ratio rapidly.

Edit: Though my GIF would be bigger. Whoops! I know the console log values are hard to see, but trust me, there is like a .1 change in the aspect ratio when changing the editor width, vs a .5+ change when adjusting the height.