Search Unity

Performance difference between SetActive and CanvasGroup

Discussion in 'UGUI & TextMesh Pro' started by MichaelTGD, Sep 22, 2020.

  1. MichaelTGD

    MichaelTGD

    Joined:
    Oct 24, 2019
    Posts:
    16
    Hi all,

    I've recently started questioning how I'm handling all my GUI elements. The game always seems to run fine, but I realized after I caught myself being inconsistent about setting SetActive = False after I set the CanvasGroup off, that I actually don't know if there is a performance difference at that point or not.

    The different GUIs are generally all set up with their own CanvasGroups, and when I toggle them, I always toggle Alpha/Interactable/Blocks Raycasts. So, if something is unseen and not blocking raycasts, (and presumably not being called or altered during this time), does it affect the performance to let the objects stay active?

    A second, related question: I came across this other older post discussing the same thing, which surmises that the CanvasGroup.Alpha = 0 is enough, which I'm not sure about. But he also mentioned Graphic Raycasters, which made me also realize that I have several of these in the scene. As I said, I'm not seeing any performance hitches as I play, but is it better practice to minimize Graphic Raycasters,or does it not have much of an affect?

    Thanks guys!

    • Unity 2019.4
    • OS: Windows 10
    • Build Platform: iOS
     
  2. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    I believe it is like this:

    For rendering it sees the canvas group checks the alpha and skips everything beneath if alpha is zero. However, it will not totally skip the elements below. It would cycle through the childs to see if there is a canvas group with "Ignore Parent Groups" set to true.
    Similar logic would be performed for raycast / interactability.

    If you instead set the game object to inactive, it would just ignore the game object and its children completely as if there where not in the scene.

    So, setting the game object to inactive is a little bit faster, but the performance impact is usually barely measurable.
     
    UnityKristy, NukeGs, Marrlie and 2 others like this.
  3. MichaelTGD

    MichaelTGD

    Joined:
    Oct 24, 2019
    Posts:
    16
    Ah, okay, I think I understand. Thanks for the answer!