Another update: while this code works to eliminate objects from getting cutoff, Event.current.mousePosition is also incorrect when the editor window’s width is less than 1024, so this is not a great solution.
All right, since this was a bug and there doesn’t appear to be a work around, I am giving up on this issue.
1024 is hardcoded in PreviewRenderUtility
https://github.com/Unity-Technologies/UnityCsReference/blob/27ca1bdc448e90c197772ab37d789dfb40d8145b/Editor/Mono/Inspector/PreviewRenderUtility.cs#L330
Here is a bit simplified version of what I wrote. Maybe it will help you.
m_previewRect = EditorGUILayout.GetControlRect(false, 256, GUIStyle.none);
// Store preview rect area for Layout pass
if (Event.current.type == EventType.Repaint)
{
m_handlesRect = m_previewRect;
m_screenRect = EditorGUIUtility.GUIToScreenRect(m_previewRect);
}
// Draw preview only in Repaint
if (Event.current.type == EventType.Repaint)
m_previewRender.BeginPreview(m_previewRect, GUIStyle.none);
else
{
// Required to make handles work properly
m_previewRender.camera.pixelRect = new Rect(0,0, m_handlesRect.width,m_handlesRect.height);
m_previewRender.camera.aspect = m_handlesRect.width / m_handlesRect.height;
}
// Draw preview only in Repaint
if (Event.current.type == EventType.Repaint)
{
DrawMeshesAndStuff();
m_previewRender.Render(true, false);
}
Rect windowRect = EditorWindow.position; // Rect from window that preview is displayed in
Vector2 mouseOffset = new Vector2(m_screenRect.xMin, m_screenRect.yMax) -
new Vector2(windowRect.xMin, windowRect.yMax) - OFFSET_BY_HEIGHT_OF_TAB_BAR;
// Offset mouse position to make handle interaction correct
Event.current.mousePosition -= mouseOffset;
Handles.SetCamera(m_previewRender.camera);
DrawHandles();
// Clear previously added offset
Event.current.mousePosition += mouseOffset;
// Draw preview only in Repaint
if (Event.current.type == EventType.Repaint)
m_previewRender.EndAndDrawPreview(m_previewRect);
I also made handles to be interactive, if mouse is in preview area, but left it to make code cleaner.
The only problem I have is when preview is scaled down to below 1024 width, handles that uses
HandleUtility.GetHandleSize are drawn smaller than their interactive area.
That is great, I will try to put this to use in my code, thank you so much!
It’s so strange that they have hard coded 1024 into it. What could possibly be the reason for doing that?
Edit:
Actually, after investigating the code further I see that 1024 is there just to stop the scale from endlessly increasing or decreasing. The real issue here is that scaleFacX is not being applied properly somewhere.
float scaleFacX = Mathf.Max(Mathf.Min(width * 2, 1024), width) / width;
float scaleFacY = Mathf.Max(Mathf.Min(height * 2, 1024), height) / height;
What is the value of OFFSET_BY_HEIGHT_OF_TAB_BAR? Is this something specific to your project or is this the height of the little part that has the window title?
Any idea on how to get rid of the space above my inner windows at the top of my main editor window? I’m pretty sure it’s a gap for the toolbar, but I don’t need the toolbar so would like to remove it. Thanks!!
Two other things maybe you can help with. As you can see I have some windows drawn over the preview scene. I’m not sure why but the mouse position is off for all of the selectable elements/buttons. I can tell it’s off by the height of the toolbar space at the top of the window but I can’t seem to correct it. setting the Event.current.mousePosition does nothing.
The other things is, setting the mousePosition as you did screwed up the selection of the handles in the preview scene view. disabling it fixed that, so for whatever reason my handles are working correctly without changing the mouse position.
Any ideas?
This is height of that part that has the window title.
For that I would just offset rect for drawing that window up a bit. Or if you are using
EditorGUILayout.GetControlRect to get rect for preview area, try getting it with style set to GUIStyle.none. Some styles have offsets.
I did that offset, because I have regular editor gui stuff with small preview area on a side, or that offset might be caused by Odin. But I had offset of Handles interactive area, so I countered that.
I had no experience with such issue. Sorry. Maybe fixing that offset of windows will fix that.
I wonder if drawing those little windows in preview is necessary. I would draw those as regular gui in window and fill remaining space with preview. Since they take top and left part of area, they are essentially normal gui with extra steps that have to be maintained. Though at this point it might be to much work to change it.
This is one of windows I did with regular gui and preview. It has simmilar layout to yours.

Thanks for all the help! I will try to put the GUI stuff outside of the preview scene, that is how I had it before but with your code I was running into some issues, but I think you are right and it may be better to figure out those issues so the GUI code can be outside the preview scene stuff.
So, I made some progress. I moved my window logic to outside of the Preview Scene rendering and now my mouse position on those windows is fixed. I also had to make an adjustment to the mouse position after this, which allows the Handles to be correctly selectable when the preview window is 1024 or greater.
However, I am seeing an issue with the mouse position when the window is smaller than 1024. Do you have any thoughts?
Here’s the basics of my code:
var previewRect = new Rect(300f, 125f, position.width - 300f, position.height - 125f);
var eventType = Event.current.type;
if (eventType == EventType.Repaint)
{
handlesRect = previewRect;
screenRect = new Rect(GUIUtility.GUIToScreenPoint(new Vector2(previewRect.x, previewRect.y)), new Vector2(previewRect.width, previewRect.height));
BeginPreview(previewRect);
Render();
}
else
{
//SceneCamera is from preview scene
SceneCamera.pixelRect = new Rect(0, 0, handlesRect.width, handlesRect.height);
SceneCamera.aspect = handlesRect.width / handlesRect.height;
}
//determines whether the mouse is over one of my windows. Needed this to stop some click through errors.
SetMouseOverWindow();
Rect pos = this.position;
mouseOffset = new Vector2(screenRect.xMin, 0f) - new Vector2(pos.xMin, 0f);
Event.current.mousePosition -= mouseOffset;
Handles.SetCamera(SceneCamera);
DrawSceneHandles();
Event.current.mousePosition += mouseOffset;
resetMouseOffset = false;
if (eventType == EventType.Repaint)
EndAndDrawPreview(previewRect);
//Drawing of my internal windows
DrawWindows();
Sorry about the screenshot, for some reason my mouse cursor doesn’t show up in it so I had to do some drawing. Also, I’m still getting that weird cutoff issue again.
I had that issue with small preview area. That’s why I set preview camera pixelRect and aspect in Layout pass.
screenRect = new Rect(GUIUtility.GUIToScreenPoint(new Vector2(previewRect.x, previewRect.y)),new Vector2(previewRect.width, previewRect.height));```
looks suspicious to me. Try:
```screenRect = EditorGUIUtility.GUIToScreenRect(previewRect);```
I need this code to work on pre 2019.1 where GUIToScreenRect is not available. I looked at the source code and that methdod just calls GUIToScreenPoint internally so I don’t think this is an issue.
I went back to drawing the windows before EndAndDrawPreview is called. It’s the only way I can get the handles to work correctly. I also changed how the previewRect dimensions are calculated to this:
if(position.width < 1024)
previewRect = new Rect(0, 0, position.width + (1024 - position.width), position.height);
else
previewRect = new Rect(0, 0, position.width, position.height);
This fixes the issue with the scene being cut off and the mouse position being off for the handles.
I still have the issue with all my window controls being off by (I’m fairly certain) 22 pixels, due to the toolbar at the top of the screen.
If anyone has any ideas on how to disable the toolbar I’d love to hear it. Adding an offset to Event.Current.mousePosition doesn’t work for whatever reason.
And I figured it out. I didn’t think this would work as previous attempts to provide a negative value for the window position have failed, but this code works:
if(position.width < 1024)
previewRect = new Rect(0, -22, position.width + (1024 - position.width), position.height);
else
previewRect = new Rect(0, -22, position.width, position.height);
Edit: With this change you also have to offset the mouse by 22 pixels before drawing and using the handles, like so:
mouseOffset = new Vector2(0f, 22f);
Event.current.mousePosition += mouseOffset;
Handles.SetCamera(SceneCamera);
DrawHandles();
Event.current.mousePosition -= mouseOffset;
Does anyone know the best way to go about drawing lots of meshes in the scene view created by the PreviewRenderUtility. I know there is a DrawMesh method available, but I would love to use DrawMeshInstanced instead. Unfortunatley, it is not working for me!
Here’s the code I’m trying:
Mesh mesh;
Vector3 size = new Vector3(.9f, .9f, .9f), location = new Vector3(.5f, .5f, 10f);
void OnGUI()
{
//a lot of other code
if(event == EventType.Repaint)
{
BeginPreview(previewRect);
Render();
EndAndDrawPreview(previewRect);
}
//more code . . .
}
void BeginPreview(Rect pos)
{
preview.BeginPreview(pos, GUIStyle.none);
}
void Render()
{
foreach (var light in preview.lights)
light.enabled = true;
var oldAllowPipes = Unsupported.useScriptableRenderPipeline;
Unsupported.useScriptableRenderPipeline = false;
RenderInstanced();
SceneCamera.Render();
Unsupported.useScriptableRenderPipeline = oldAllowPipes;
}
void EndAndDrawPreview(Rect pos)
{
preview.EndAndDrawPreview(pos);
}
void RenderInstanced()
{
if (mesh == null)
mesh = (Mesh)AssetDatabase.LoadAssetAtPath("Assets/TerrainSlicing/EditorResources/Bevel_Cube.fbx", typeof(Mesh));
Matrix4x4[] mArr = new Matrix4x4[1];
Matrix4x4 m = Matrix4x4.TRS(location, Quaternion.identity, size);
mArr[0] = m;
//preview.DrawMesh(mesh, m, SharedLoadingPatternStatics.cellMaterial_InstancedOpaque, 0);
//Graphics.DrawMesh(mesh, m, SharedLoadingPatternStatics.cellMaterial_InstancedOpaque, 1, SceneCamera);
Graphics.DrawMeshInstanced(mesh, 0, SharedLoadingPatternStatics.cellMaterial_InstancedOpaque, mArr, 1, null, UnityEngine.Rendering.ShadowCastingMode.Off, false, 1, SceneCamera);
}
The two lines preview.DrawMesh and Grahics.DrawMesh both work. I don’t know a lot about rendering, I think it’s because DrawMeshInstanced does not get rendered with Camera.Render (which renders to a RenderTexture).
Any help would be great!

