Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

How to force IMGUIContainer OnGUIHandler rendering a UnityEditor.Editor as a preview component

Discussion in 'Immediate Mode GUI (IMGUI)' started by k4m4j1, Apr 29, 2020.

  1. k4m4j1


    Jul 5, 2013
    Hello there folks,
    I have a IMGUIContainer rendering a UnityEditor.Editor in a EditorWindow instance, all works fine, the gameobject is displayed like it should in the UI, the problem is that my editor window makes changes at the selected gameobject, when the changes are made ReloadPreviewInstances is called at the UnityEditor.Editor instance, but the IMGUIContainer is not repainted, I can only see the changes when I interact with the UnityEditor.Editor, to be especific, the changes made are only displayed if the mouse is dragged making the gameobject rotate at the preview frame.

    Below the Action sent the IMGUIContainer constructor:
    Code (CSharp):
    1.         public Action OnGuiHandler()
    2.         {
    3.             return () =>
    4.             {
    5.                 if (_gameObjectEditor == null)
    6.                 {
    7.                     _gameObjectEditor = UnityEditor.Editor.CreateEditor(Selection.activeGameObject);
    8.                 }
    9.                 _gameObjectEditor.OnPreviewGUI(GUILayoutUtility.GetRect(position.width, position.height), _previewColor);
    10.             };
    11.         }
    I tryied to call MarkDirtyRepaint at the IMGUIContainer instance and execute the OnGuiHandler directly without success, now I'm looking for help, any ideas?

    Thanks in advance!
    a436t4ataf likes this.
  2. a436t4ataf


    May 19, 2013
    Did you log a bug for this?

    At the very least: the documentation for IMGUIContainer sucks, and needs to be fixed to actually ... document ... the onGUIHandler method.
  3. unity_2402justin


    Oct 18, 2019

    Hello, This might be a little too late and you might have already found the solution for it but basically this is how I managed to fix it.

    Code (CSharp):
    2. public Action OnGuiHandler()
    3. {
    4.             if (model != null)
    5.             {
    6.                 return () =>
    7.                 {
    8.                     GUIStyle bgColor = new GUIStyle();
    9.                     bgColor.normal.background = EditorGUIUtility.whiteTexture;
    10.                     if (gameObjectEditor == null) gameObjectEditor = UnityEditor.Editor.CreateEditor(model);
    11.                     gameObjectEditor.OnInteractivePreviewGUI(GUILayoutUtility.GetRect(256, 256), bgColor);
    12.                 };
    13.             }
    14.             else return () => {  };
    15. }
    ^ this up here is my code for actually getting a model to show, its pretty similar to yours.
    However I also have an event method that fires all my events that I need and before showing the preview of the object I run this method.

    Code (CSharp):
    2. private void DestroyPreviousObjectPreview()
    3.         {
    4.             modelChanged -= ImGuiObjectPreview();
    5.             gameObjectEditor = null;
    6.             previewContainerGroupBox.Clear();
    8.             previewContainer = new IMGUIContainer();
    9.             previewContainerGroupBox.Add(previewContainer);
    10.         }
    11. }
    What this does is that it unsubscribes from the event first to avoid any errors, unassigns the Editor object, clears the imgui container from the hierarchy (I am also referencing the group box that the imgui container is a child of) and creates a new imgui container in its place.

    I hope you can understand this as this is my first post and Im not really that good at explaining things personally.
    Last edited: Mar 11, 2023