Search Unity

Writing an Editor Window that mimics the Scene View

Discussion in 'Immediate Mode GUI (IMGUI)' started by gilley033, Jan 15, 2019.

  1. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,191
    I am writing an editor extension (let's call it EE1) that involves the manipulation of a pattern. The pattern's data is stored in a scriptable object and represented by game objects in the scene view, however, since the data is stored as a scriptable object, I don't actually need to save any data to a scene object. The pattern is just a 2D or 3D array of cells, with some cells being active, and others inactive.

    Currently I just open up a new scene, load the pattern, and the user can then manipulate/change the pattern. The scene is closed once pattern editing is finished.

    The issue is, I have another editor extension (also which utilizes the scene view, let's call it EE2) that uses the pattern, and it would be beneficial to allow both these editor extensions to run at once, as adjusting the pattern to fit the users needs would be easier if they could have EE2 open. Otherwise, to edit the pattern using EE1, I have to close the EE2 scene.

    If the pattern were just two dimensional, I could build EE1 using an Editor Window, however the pattern can be three dimensional, which is why it is easier to use game objects to represent it and also make use of the rotatable camera.

    So, what I really need, is a sort of dummy scene view that allows me to load game objects into it, but doesn't actually utilize a scene asset. This dummy scene view would also need to be usable while an actual scene view is also open, in the same way you can open an editor window while a scene is open.

    Does anyone have any experience with this?
     
  2. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    Is there any reason you can't have both of them in the same scene?
     
  3. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,191
    It would be basically impossible to do the pattern editing while the second editing process is underway. Sort of difficult to explain, but I did consider that possibility and it is not feasible. Thanks for the suggestion though!
     
  4. BinaryCats

    BinaryCats

    Joined:
    Feb 8, 2016
    Posts:
    317
    I had an idea for an editor which was something similar, however I never implemented it. What I planned on doing was changing the culling mask on the scene view camera.

    So in editor 1: it could see objects on
    layer A

    and
    editor 2: could not see objects on
    layer A


    possibly you could do something similar?
     
  5. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,191
    Hmm, I suppose that should be possible, since each scene view should utilize its own camera. I will play around with this and see what I can come up with. Thanks for the suggestion!
     
  6. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    You could still do that without culling masks. The scene is a large place. Just put the components far away from each other.
     
  7. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,191
    Yes, but since the scene in question could basically contain anything (i.e., when the 'editor' is launched, I'd just load it into whatever scene was currently open), calculating a "safe" place where no other objects appear may be ridiculously hard, if not impossible.

    Or am I missing something?
     
  8. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    Would it be possible to provide some more context (screenshots, etc.) as to what you're working with? Might make it possible to give more specific advice.
     
  9. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,191
  10. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    Firstly, you can certainly calculate a 'safe' space for the editor by checking the AABBs of all the renderers in the scene. Not that I would recommend doing that, but it isn't impossible.

    Second, check out this API: https://docs.unity3d.com/ScriptReference/SceneManagement.EditorSceneManager.NewPreviewScene.html

    Make your gameobjects and add it to that scene. You can create a camera for the scene, and then render it with https://docs.unity3d.com/ScriptReference/Handles.DrawCamera.html into an EditorWindow.
     
  11. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,191
    PreviewScene looks like it has some potential. I'll look into it. Thanks!
     
  12. BinaryCats

    BinaryCats

    Joined:
    Feb 8, 2016
    Posts:
    317
    if you look into the preview scene, you might want to look at my post here:
    https://forum.unity.com/threads/modify-gameobject-in-previewgui.556792/#post-3699133
    where I have ripped out the source of previewgui

    ~The reason for this is, because the object is instantiated when you add it to the preview scene, you lose reference to it. the above code solves that~
     
    Last edited: Jan 21, 2019
  13. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    The PreviewScene method is not the same as the OnPreviewGUI method. Using a PreviewScene does not cause you to lose the reference to gameobjects you add to it.
     
    BinaryCats likes this.
  14. BinaryCats

    BinaryCats

    Joined:
    Feb 8, 2016
    Posts:
    317
    ah okay, my bad
     
  15. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,191
    Is doing it with Handles.DrawCamera the only way? I would like the user to make use of some of the functionality that is built into the normal Scene View, such as changing the camera view and dragging the scene camera around. It seems like I would need to re-engineer such functionality doing things this way. Am I wrong?

    And also, how would I go about creating a camera for this preview scene?
     
    Last edited: Jan 23, 2019
  16. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    Yep. It's not overly difficult though.

    You create a camera same way you'd create a camera through code for any other purpose. Create a new GameObject and add a Camera component.
     
  17. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,191
    Okay, thank you!
     
  18. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,191
    Sorry, I must ask for more help, as it seems you are the only one with knowledge of this stuff (if you can point me to instructional material I will be glad to figure all this out on my own).

    In any case, I have created a Preview Scene, added a camera to it (I used EditorSceneManager.MoveGameObjectToScene to make sure it is in the preview scene), and am using Handles.DrawCamera, but it seems to be drawing the camera of whatever scene I have open rather than the one I created for the Preview Scene. Also, I cannot get the camera render to cover the entire editor window. I also tried using Handles.SetCamera first.

    Handles.SetCamera(sceneCamera);
    Handles.DrawCamera(new Rect(0, 0, position.width, position.height), sceneCamera);

    Where position is the EditorWindow.position rect. I am using 0, 0 after looking at the SceneView class on GitHub. sceneCamera is 100% a reference to the camera created procedurally and moved to the PreviewScene.

    I am not sure what I am doing wrong.
     
  19. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    Can you post the entirety of the EditorWindow code as it stands, as well as a screenshot of what you're seeing? It doesn't make any sense for it to be drawing a different camera than you pass it.
     
  20. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,191
    Sure.

    The Preview Scene, Editor Window, and game objects are all created via a button press in the inspector of my pattern scriptable object, through a call to this static method:

    Code (CSharp):
    1.  
    2. static LoadingPatternEditor OpenEditingWindow()
    3. {
    4.     var scene = EditorSceneManager.NewPreviewScene();
    5.     scene.name = "DLK_Loading_Pattern_Editor";
    6.     workingEditor = EditorWindow.GetWindow<LoadingPatternEditor>();
    7.     workingEditor.position = new Rect(Screen.width / 2 + 300, 400, 600, 300);
    8.     var root = new GameObject("DLK_Loading_Pattern_Editor_Root");
    9.     root.transform.position = new Vector3(.5f, .5f, .5f);
    10.     EditorSceneManager.MoveGameObjectToScene(root, scene);
    11.     var camGO = new GameObject("Camera");
    12.     Camera cam = camGO.AddComponent<Camera>();
    13.     camGO.transform.position = new Vector3(.5f, .5f, .5f);
    14.     EditorSceneManager.MoveGameObjectToScene(camGO, scene);
    15.            
    16.     workingEditor.OnEditorLoaded(scene, cam, root);
    17.     //workingEditor.PreviousSceneSetup = currentScenes;
    18.     return workingEditor;
    19. }
    20.  
    The scene, camera, and root game object (which just serves as a parent for game objects used as visual cues for the pattern in the scene) are all passed to the Editor Window via the OnEditorLoaded call.

    The OnGUI method of the EditorWindow looks like this:
    Code (CSharp):
    1. private void OnGUI()
    2. {
    3.     if (!editorScene.IsValid() || !editorScene.isLoaded)
    4.     {
    5.         OnEditorUnLoaded();
    6.         return;
    7.     }
    8.  
    9.     isInTopDownOrtho = sceneCamera.orthographic && sceneCamera.transform.rotation == downView;
    10.  
    11.     if (patternRepositoryCurrentlyBeingEdited == null)
    12.         AccountForNewRepository();
    13.  
    14.     patternRepositoryCurrentlyBeingEdited.Update();
    15.  
    16.     //GUI.Box(rect, GUIContent.none);
    17.     GUISkin skin = GUI.skin;
    18.     GUI.skin = style;
    19.  
    20.     DrawGUI();
    21.     if (currentPattern != null)
    22.         currentPattern.DrawEditor();
    23.  
    24.     SceneView.RepaintAll();
    25.     GUI.skin = skin;
    26.  
    27.     patternRepositoryCurrentlyBeingEdited.ApplyModifiedProperties();
    28. }
    The DrawGUI method is what contains the actual camera rendering.
    Code (CSharp):
    1. void DrawGUI()
    2. {
    3.     GetStyles();
    4.  
    5.     Handles.SetCamera(sceneCamera);
    6.     Handles.DrawCamera(new Rect(0, 0, position.width, position.height), sceneCamera);
    7.     Handles.BeginGUI();
    8.     EditorGUILayout.BeginVertical("BackgroundWindow");
    9.  
    10.     EditorGUILayout.BeginHorizontal(horizontalStyle);
    11.     LabelField("Show Full Menu");
    12.     showMenuProp.boolValue = EditorGUILayout.Toggle(showMenuProp.boolValue, toggleStyle, toggleWidth, toggleHeight);
    13.     EditorGUILayout.EndHorizontal();
    14.  
    15.     if (showMenuProp.boolValue)
    16.         DrawFullMenu();
    17.     else
    18.         DrawHiddenMenu();
    19. }
    There is a lot more code. I can post it all if you don't think it would be helpful, but I am hopeful that whatever I am doing wrong is apparent in the code posted above. Posting the entire code would provide a lot to sift through.

    Thanks for the help!
     
  21. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,191
    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.
    EditorWindow.png

    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.
     
    Last edited: Jan 24, 2019
  22. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,191
    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.
     
  23. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    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.
     
  24. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,191
    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.
     
  25. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,191
    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.
     
  26. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    Yep, I was just about to post that I got it working with these parameters:

    Code (CSharp):
    1.         previewScene = EditorSceneManager.NewPreviewScene();
    2.  
    3.         var camObj = new GameObject( "Camera" );
    4.         cam = camObj.AddComponent<Camera>();
    5.         cam.transform.position = new Vector3( 0, 0, -10 );
    6.         cam.hideFlags = HideFlags.HideAndDontSave;
    7.         cam.scene = previewScene;
    8.         cam.enabled = false;
    9.         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:

    Code (CSharp):
    1. PreviewRenderUtility renderUtils;
    2.  
    3. private void OnEnable () {
    4.     renderUtils = new PreviewRenderUtility();
    5.     renderUtils.AddSingleGO( GameObject.CreatePrimitive( PrimitiveType.Cube ) );
    6.     renderUtils.camera.transform.position = new Vector3( 0, 0, -10 );
    7. }
    8.  
    9. private void OnDisable () {
    10.     renderUtils.Cleanup();
    11. }
    12.  
    13. public void OnGUI () {
    14.     var pos = position;
    15.     pos.x = 0;
    16.     pos.y = 0;
    17.     renderUtils.BeginPreview( pos, EditorStyles.helpBox );
    18.     renderUtils.Render();
    19.     renderUtils.EndAndDrawPreview( pos );
    20. }
     
  27. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,191
    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!
     
  28. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    Yep, this is a wrapper around a PreviewScene. Takes care of the camera, and has its own lights as well.
     
  29. BinaryCats

    BinaryCats

    Joined:
    Feb 8, 2016
    Posts:
    317
    ahhhh yes PreviewRenderUtility this is what was ringing a bell when I linked my previous reply
     
  30. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,191
    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!!
     
  31. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,191
    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
    Incorrect.png
    Positioning is right Correct.png
     
  32. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,191
    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.
     
  33. BinaryCats

    BinaryCats

    Joined:
    Feb 8, 2016
    Posts:
    317
    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
     
    gilley033 likes this.
  34. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,191
    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!
     
    BinaryCats likes this.
  35. ecurtz

    ecurtz

    Joined:
    May 13, 2009
    Posts:
    640
    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.
     
    Last edited: Mar 15, 2019
  36. NosorozecKaszle

    NosorozecKaszle

    Joined:
    Dec 3, 2019
    Posts:
    9
    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.
     
    Last edited: Apr 14, 2020
  37. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,191
    Thank you for the tip however this did not work for me.
     
  38. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,191
    Still can't figure this out. Just to add to the discussion, the objects in my PreviewRenderUtility are getting cutoff, like so:

    upload_2020-4-21_3-45-12.png
    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.​
     
  39. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,191
    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.
     
  40. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,191
    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.
     
  41. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,191
    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.
     
  42. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,191
    All right, since this was a bug and there doesn't appear to be a work around, I am giving up on this issue.
     
  43. NosorozecKaszle

    NosorozecKaszle

    Joined:
    Dec 3, 2019
    Posts:
    9
    1024 is hardcoded in PreviewRenderUtility
    https://github.com/Unity-Technologi...r/Mono/Inspector/PreviewRenderUtility.cs#L330

    Here is a bit simplified version of what I wrote. Maybe it will help you.
    Code (CSharp):
    1.  
    2. m_previewRect = EditorGUILayout.GetControlRect(false, 256, GUIStyle.none);
    3.  
    4. // Store preview rect area for Layout pass
    5. if (Event.current.type == EventType.Repaint)
    6. {
    7.    m_handlesRect = m_previewRect;
    8.    m_screenRect = EditorGUIUtility.GUIToScreenRect(m_previewRect);
    9. }
    10.  
    11. // Draw preview only in Repaint
    12. if (Event.current.type == EventType.Repaint)
    13.    m_previewRender.BeginPreview(m_previewRect, GUIStyle.none);
    14. else
    15. {
    16.    // Required to make handles work properly
    17.    m_previewRender.camera.pixelRect = new Rect(0,0, m_handlesRect.width,m_handlesRect.height);
    18.    m_previewRender.camera.aspect = m_handlesRect.width / m_handlesRect.height;
    19. }
    20.  
    21. // Draw preview only in Repaint
    22. if (Event.current.type == EventType.Repaint)
    23. {
    24.    DrawMeshesAndStuff();
    25.    
    26.    m_previewRender.Render(true, false);
    27. }
    28.  
    29. Rect windowRect = EditorWindow.position; // Rect from window that preview is displayed in
    30.  
    31. Vector2 mouseOffset = new Vector2(m_screenRect.xMin, m_screenRect.yMax) -
    32.                       new Vector2(windowRect.xMin, windowRect.yMax) - OFFSET_BY_HEIGHT_OF_TAB_BAR;
    33.  
    34. // Offset mouse position to make handle interaction correct
    35. Event.current.mousePosition -= mouseOffset;
    36.  
    37. Handles.SetCamera(m_previewRender.camera);
    38.  
    39. DrawHandles();
    40.  
    41. // Clear previously added offset
    42. Event.current.mousePosition += mouseOffset;
    43.  
    44. // Draw preview only in Repaint
    45. if (Event.current.type == EventType.Repaint)
    46.    m_previewRender.EndAndDrawPreview(m_previewRect);
    47.  
    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.
     
  44. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,191
    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.

    Code (CSharp):
    1. float scaleFacX = Mathf.Max(Mathf.Min(width * 2, 1024), width) / width;
    2. float scaleFacY = Mathf.Max(Mathf.Min(height * 2, 1024), height) / height;
     
    Last edited: Apr 23, 2020
  45. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,191
    @NosorozecKaszle

    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?
     
  46. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,191
    @NosorozecKaszle

    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!!

    upload_2020-4-23_18-57-46.png
     
  47. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,191
    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?
     
  48. NosorozecKaszle

    NosorozecKaszle

    Joined:
    Dec 3, 2019
    Posts:
    9
    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.
    upload_2020-4-24_19-11-17.png
     
  49. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,191
    @NosorozecKaszle

    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.
     
  50. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,191
    @NosorozecKaszle

    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:
    Code (CSharp):
    1. var previewRect = new Rect(300f, 125f, position.width - 300f, position.height - 125f);
    2.  
    3. var eventType = Event.current.type;
    4. if (eventType == EventType.Repaint)
    5. {
    6.     handlesRect = previewRect;
    7.     screenRect = new Rect(GUIUtility.GUIToScreenPoint(new Vector2(previewRect.x, previewRect.y)), new Vector2(previewRect.width, previewRect.height));
    8.     BeginPreview(previewRect);
    9.     Render();
    10. }
    11. else
    12. {
    13.     //SceneCamera is from preview scene
    14.     SceneCamera.pixelRect = new Rect(0, 0, handlesRect.width, handlesRect.height);
    15.     SceneCamera.aspect = handlesRect.width / handlesRect.height;
    16. }
    17.  
    18. //determines whether the mouse is over one of my windows. Needed this to stop some click through errors.
    19. SetMouseOverWindow();
    20. Rect pos = this.position;
    21.  
    22. mouseOffset = new Vector2(screenRect.xMin, 0f) - new Vector2(pos.xMin, 0f);
    23. Event.current.mousePosition -= mouseOffset;
    24.  
    25. Handles.SetCamera(SceneCamera);
    26.  
    27. DrawSceneHandles();
    28.  
    29.  
    30. Event.current.mousePosition += mouseOffset;
    31. resetMouseOffset = false;
    32.  
    33. if (eventType == EventType.Repaint)
    34.     EndAndDrawPreview(previewRect);
    35.  
    36. //Drawing of my internal windows
    37. DrawWindows();
    issues.png

    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.