unity-game-engineunity-editor

Unity - Custom Editor - data refresh


I created a new window in the editor. It is intended for debugging and creating some mini test scenarios. For example, reducing or increasing the HP of enemies, subtracting resources, etc. In the OnGUI method - I create a loop that iterates through the list of opponents and collects information about HP, ammunition etc. Via GUILayout.Label I display this information. Unfortunately, this data does not refresh dynamically. And every few seconds or clicking on the window. I don't know if I'm using this editor well. But I would like to have a section where this data will dynamically refresh for me - and will be part of the UI of the UnityEditor, not the Game itself.

Editor window screenshoot


Solution

  • You could use Update

    Called multiple times per second on all visible windows.

    like

    private void Update()
    {
        Repaint();
    }
    

    This can however get quite expensive and I think you could also rather use OnInspectorUpdate

    OnInspectorUpdate is called at 10 frames per second to give the inspector a chance to update.

    like in the example:

    void OnInspectorUpdate()
    {
        // Call Repaint on OnInspectorUpdate as it repaints the windows
        // less times as if it was OnGUI/Update
        Repaint();
    }
    

    so it only runs at 10 frames/second but continuously so also if you don't e.g. move the mouse.

    Repaint basically forces a new OnGUI run.