Search Unity

do you cache all your GUI.GetStyle ?

Discussion in 'Immediate Mode GUI (IMGUI)' started by laurentlavigne, May 31, 2020.

  1. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,363
    I see our code has all the GetStyle inlined, called each frame, in the real world this would be cached by Unity and they did optimize the gnarly beast but is this the reality? In other word, do I have to cache the 100s of GetStyle?
    (and if you have a better refactoring tool than rider, I'm taker)
     
  2. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,330
    I'm pretty sure that they won't get cached automatically, I don't see how the compiler would know to do this.

    Looking at the source code for GUISkin.GetStyle we can see that it contains a couple of instances of casting, a couple of null checks and it retrieves the GUIStyle from a dictionary. So nothing too costly, you probably wouldn't notice any improvements even if you cached the GUIStyles in OnEnable. Maybe hundreds of calls multiple times per frame could add up to making a tiny difference, you could do a simple test script and profile it in a build to measure the real world impact.
     
    laurentlavigne likes this.