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.
  2. Dismiss Notice

Question Inspector Repaint and GC Allocations in Unity versions

Discussion in 'UI Toolkit' started by Kazko, Feb 21, 2023.

  1. Kazko

    Kazko

    Joined:
    Apr 2, 2014
    Posts:
    82
    Hi, not sure whether to post this in IMGUI forum or here, because this issue seems related.

    I'm making a custom Property Drawer where I need the inspector to be responsive when manipulating/dragging some of it's properties and gizmos. To achieve this, I'm using these lines at the end of OnGUI.

    Code (CSharp):
    1. if (m_CurrentEvent.type != EventType.Repaint) return;
    2. if (position.Contains(m_CurrentEvent.mousePosition)) EditorWindow.focusedWindow.Repaint();
    This shoots up GC Allocs from default 237B to hundreds of kB (depending on Unity version). I am developing the tool in 2020 LTS and by optimizing I was able to bring the GC down to about 24 kB when the mouse is over the drawer. This seems reasonable.

    Bringing the tool to 2021 LTS, the GC goes up to 38 kB.
    The same code in 2022.2 generates 54 kB.
    And in 2023.1 Beta, it shoots up to 245 kB!

    Deep profiling shows much of the increase is due to ReorderableList, which I'm using to display elements of a list. In my situation, each new list element adds 3.5 kB GC in 2023.1 Beta.

    I suspect the increase is caused by ongoing changes to the Editor GUI. For example in 2022.2 when "Use IMGUI Default Inspector" checkbox is on, the GC falls back to 2021 LTS levels, which is still almost twice the 2020 LTS. And in 2023.1 Beta, this checkbox does not help much, leaving the GC in hundreds.

    Correct me if I'm wrong but it seems like the ongoing gradual change towards UI Toolkit is the cause, where the toolkit is wrapping and handling the old GUI?

    So, the questions?

    1) How to develop editor tool for old and new versions of Unity? Can classic Inspector GUI and UI Toolkit version of it coexist?
    (edit) Discussion and answers: https://forum.unity.com/threads/property-drawers.595369/

    2) How to combat the increased GC Allocations in each major Unity release?
    3) How worried should I be about the Editor GC Allocations in general?
    4) Is there something else I'm missing? For example a different way of achieving a fluid responsive Inspector UI?


    I think this issue affects also the default drawers. For example the simple Transform component.
    In 2020 LTS it idles at 0.5 kB, but dragging for example the Position X field makes the redraw faster and the GC sometimes spikes to 32 kB.
    Transform in 2021 LTS: Idle at 237 B, dragging 32 kB
    Transform in 2022.2: Idle at 237 B, dragging 30 kB
    Transform in 2023.1 Beta: Idle at 237 B, dragging spikes to over 100 kB

    Thanks for any ideas!
     
    Last edited: Feb 23, 2023
  2. Kazko

    Kazko

    Joined:
    Apr 2, 2014
    Posts:
    82
  3. Kazko

    Kazko

    Joined:
    Apr 2, 2014
    Posts:
    82
    Up.
    In addition to question #4 - how does Unity achieve semi-responsive inspectors? For example the mentioned Transform component, or LOD Group drawer, their GC while dragging values or the LOD stripe is very low with occasional spikes, so it's probably not done via Repaint. Any insights?
     
  4. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    1,203
    This is a bug, and a fix should land soon. It won't return to the same levels but it should improve significantly. The reason there was this increase in 2023.1 is because we switched the text backend of IMGUI to TextCore (the tech taken from TextMeshPro and used by UI Toolkit). This backend is made for retained more UIs, and it's pure C# (unlike the legacy TextNative backend of IMGUI previously). But now that all UI frameworks are on the same text backend, we can improve performance and stability across all three at the same time.
     
    Kazko likes this.