Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Rendering Inspector inside an EditorWindow and saving asset changes

Discussion in 'Immediate Mode GUI (IMGUI)' started by log_dewald, Nov 12, 2020.

  1. log_dewald

    log_dewald

    Joined:
    Jan 8, 2020
    Posts:
    1
    Hello! I've been hitting a brick wall trying to work out if this is a bug, or intended behaviour that i just don't understand.

    Basically what i'm trying to do is render an inspector for an object inside another EditorWindow where i can make changes to said object, with those changes taking affect and saving to disk - basically the exact same behaviour a normal Inspector does when you click on an asset in the project view and change some data.

    I'm aware that i can create an editor and run OnInspectorGUI() within my EditorWindow - this is all fine and works just as expected.
    The problem im running into is, if i make a change inside the EditorWindow, the changes do not serialize until i click 'File > Save Project', pressing Save, closing the editor or if i click on the asset inside the project view. Viewing the asset in NotePad for instance indicates nothing has saved prior to one of these steps.

    In contrast, when i view the asset in the normal Inspector and change a value, the cursor changes to the spinning circle, and the changes is saved to disk. Viewing the asset in NotePad indicates it's saved as soon as a change i made.

    Here's some example code i've written. This example requires a prefab in your asset folder named "TestAsset".
    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3. public class TestWindowEditor : EditorWindow
    4. {
    5.     Editor _configEditor;
    6.     [MenuItem("Test/TestWindow %#l")]
    7.     public static void ShowWindow()
    8.     {
    9.         var wnd = EditorWindow.GetWindow<TestWindowEditor>();
    10.         wnd.titleContent = new GUIContent("Test Window");
    11.     }
    12.     public void OnEnable()
    13.     {
    14.         var testAssetGuids = AssetDatabase.FindAssets("TestAsset");
    15.         if (testAssetGuids.Length == 0) return;
    16.         var testAssetPath = AssetDatabase.GUIDToAssetPath(testAssetGuids[0]);
    17.         if (string.IsNullOrEmpty(testAssetPath)) return;
    18.         var loadedTestAsset = AssetDatabase.LoadAssetAtPath<GameObject>(testAssetPath);
    19.         if (loadedTestAsset == null) return;
    20.         _configEditor = Editor.CreateEditor(loadedTestAsset.transform);
    21.     }
    22.     void OnGUI()
    23.     {
    24.         if (_configEditor == null) return;
    25.         _configEditor.OnInspectorGUI();
    26.     }
    27.     public void OnDisable()
    28.     {
    29.         Object.DestroyImmediate(_configEditor);
    30.     }
    31. }


    My current solution is to check if the editor target has become dirty and then force the AssetDatabase to save, this feels unnecessary, and quite annoying when changing values through sliders for instance as it will attempt to save prior to releasing drag.

    Code (CSharp):
    1. var dirtyTarget = EditorUtility.IsDirty(_configEditor.target);
    2. if (dirtyTarget)
    3. {
    4.     AssetDatabase.SaveAssets();
    5. }


    Here's a short example of what i'm talking about.
    First i'm changing the transform value using the normal Inspector, it saves to disk fine.
    Secondly i'm changing the transform value using the Inspector drawn in the EditorWindow, you'll notice it does not save to disk until i click on the asset in the project view again (or if i quit Unity, or save project).




    Am i misunderstanding the usage of calling OnInspectorGUI(); inside an EditorWindow? Any insight would be appreciated!
     
    Last edited: Nov 12, 2020