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

Question does GUI.skin.GetStyle("string")) get cached?

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

  1. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,221
    I'm wondering if this is what's causing all the crazy allocation in the ton and a half of IMGUI code I've got.
     
  2. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,221
    answer: nope
    and a GUIStyle variable can only be initialized in OnGUI :rolleyes:
    off course
    So if you have a loop of 100 buttons using the same style, better make that guistyle variable outside the loop.
    Otherwise ... well... it's not possible and less than 100 there is very little gain
    100 getstyle eat up only 0.07ms
    upload_2020-5-30_22-7-28.png

    Code (CSharp):
    1. using UnityEngine;
    2. public class TestGetStylePerformance : MonoBehaviour
    3. {
    4.     GUIStyle style;
    5.     private void Start()
    6.     {
    7.     }
    8.     public bool cached = false;
    9.     private void OnGUI()
    10.     {
    11.         style = GUI.skin.GetStyle("Box");
    12.         for(int i = 0; i < 100; i++)
    13.         {
    14.             if (cached)
    15.                 GUI.Button(new Rect(0, 0, 10, 10), "", style);
    16.             else
    17.                 GUI.Button(new Rect(0, 0, 10, 10), "", GUI.skin.GetStyle("Box"));
    18.         }
    19.     }
    20. }
    21.