Search Unity

OnGUI vs Update / events for UI changes?

Discussion in 'Scripting' started by Kev00, Jun 3, 2019.

  1. Kev00

    Kev00

    Joined:
    Dec 6, 2016
    Posts:
    229
    quick question.

    I've noticed that updating many UI elements (modifying images colors, etc) causes changes to be noticeable within Update() and within UI Events (IPointerClickEvent::OnPointerClick , etc).

    but, when I defer these changes (via the use of flags) to OnGUI all this flickering goes away.

    Yet, I read that OnGUI is the old method....

    What is the proper way to update many UI elements at once?
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Have you tried LateUpdate? In general, you want game logic to run in Update(), and anything that updates visuals-only to run in LateUpdate.

    But yes, OnGUI is the old method, and the only reason it seems to work here is because, by sheer coincidence, OnGUI is run near the end of the frame cycle.
     
    Joe-Censored likes this.
  3. Kev00

    Kev00

    Joined:
    Dec 6, 2016
    Posts:
    229
    Thanks for the suggestion. Yes, LateUpdate works.

    It seems that some of my problems stem from the use of gameObject.SetActive(true) during OnPointerClick events.

    In this case, when a UI game object is set to active that has a toggle group, you can visually see the toggles flicker as it enumerates all the toggles.

    At this point, I'm trying to hide / show the UI objects using the CanvasGroup, but the first time they load flicker occurs.

    There must be something I'm not doing correctly.
     
    Last edited: Jun 5, 2019