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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Nested Canvas Groups

Discussion in 'UGUI & TextMesh Pro' started by khanstruct, Mar 19, 2015.

  1. khanstruct

    khanstruct

    Joined:
    Feb 11, 2011
    Posts:
    2,870
    I have a few Canvas Groups all nested together to display the characters in my RPG party.

    Menu Canvas Group
    > Characters Canvas Group
    >> Party Member 1 Canvas Group
    >> Party Member 2 Canvas Group
    >> Party Member 3 Canvas Group

    I need to be able to turn all of these on and off individually. However, when I hit my button, the Menu alpha becomes 1, and the Party Member alpha becomes 1, but the middle child (Character Canvas Group) does not.

    I would post my code, but it's kinda all over the place, so I'm just posing the basic question: Is there a reason the middle canvas group in a nested collection wouldn't become visible?

    They all call the same function, which sets their alpha to 1. I've put in Debug flags, which output and say that all of the canvases are being set to alpha 1.

    If there isn't a simple "oh, yeah, it's because ---" answer to this, I'll be happy to post my code, but that will be a bit tougher to explain.

    Help! Thanks.
     
  2. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,685
    You might want to check out the UT UI sample project on the asset store which shows you a working multi-window menu system: https://www.assetstore.unity3d.com/en/#!/content/25468

    Also, why use nested canvases if they are all the same resolution / scale. Just use EmptyGO's. It is the RectTransform that controls it's size / position and scale, not the canvas!

    You should only use nested Canvases if you need to apply different CanvasScalar types or multiple WorldSpace Canvases.
     
  3. khanstruct

    khanstruct

    Joined:
    Feb 11, 2011
    Posts:
    2,870
    I'm either misunderstanding you, or you're misunderstanding me. I'm not using multiple canvases. I'm using multiple "canvas groups".
     
  4. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,685
    Right, I get you now (so obvious when you read a second time), I have seen this question before on the UI forums

    The answer is to create a script to put on the Canvas that will maintain a list of all the child CanvasGroups on the Canvas. You can do this using GetComponentsInChildren<CanvasGroup>() (done that from memory so appologies if it's not quite right).

    Then have a function to loop through the Alpha property of each discovered CanvasGroup with a property in the script. Simples :D

    If you want me to write that up, let me know (I Should really add this script to the UI Extensions bitbucket repo :S)
     
  5. khanstruct

    khanstruct

    Joined:
    Feb 11, 2011
    Posts:
    2,870
    Okay, so we are on the same page. My situation is a little more tricky than that, as I have to be able to turn them on and off individually. For instance, you can have the menu open, but not be on the "Character tab". Or, you can be on the character tab, but if you only have 2 people in your party, then the Party Member 3 group will be off.

    Basically what I'm getting from your answer though is, no, there isn't a common reason that the middle group shouldn't be working... which puts me back at square one :(
     
  6. Ramcat

    Ramcat

    Joined:
    Aug 16, 2014
    Posts:
    95
    Why is that not in the documentation?

    That's the important stuff that should be in the documentation.
     
  7. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,685
    :D It is implied on the documentation, if you dig deep enough @Ramcat
    There is a lot of work going on with the docs and learn team to show architecture and best practice, slow work but it is getting there.
     
  8. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,685
    If you have a specific test case, then try to quickly replicate just that scenario in a test project and if you can replicate it, just log it using the Unity bug reporter in the editor
     
  9. Feaver1968

    Feaver1968

    Joined:
    Nov 16, 2014
    Posts:
    70
    Is there a reason you are using alpha and not setting the gameobject's active state?
     
  10. khanstruct

    khanstruct

    Joined:
    Feb 11, 2011
    Posts:
    2,870
    I don't like using Active states, as that makes the objects more difficult to work with (when they're inactive). And I'm pretty sure the alpha and enable stuff is what the Canvas Groups were meant for.

    The only difference I have between the objects is that the ones that are working, I have their GameObjects stored and use GetComponent<CanvasGroup> during runtime, while the one that isn't working, I have it's CanvasGroup stored and just use it directly.

    I started reworking it to store them all as objects and just reference their CanvasGroups at runtime, but that was a whole lot of rewriting, and I just got frustrated and Ctrl+Z-ed the crap out of it. :mad:
     
  11. Feaver1968

    Feaver1968

    Joined:
    Nov 16, 2014
    Posts:
    70
    The thing about the active state, it's like if you have a lamp and need to turn it off... You flip the switch off. Setting the alpha to zero is like putting a shirt over the lamp shade.
     
    khanstruct likes this.
  12. khanstruct

    khanstruct

    Joined:
    Feb 11, 2011
    Posts:
    2,870
    Yes, but then turning it back on is like trying to find a lamp in the dark.
     
    Feaver1968 likes this.
  13. Feaver1968

    Feaver1968

    Joined:
    Nov 16, 2014
    Posts:
    70
    But if you use a public reference, it's like using a clapper.
     
  14. khanstruct

    khanstruct

    Joined:
    Feb 11, 2011
    Posts:
    2,870
    lol, I think this metaphor is getting away from us.
     
  15. Feaver1968

    Feaver1968

    Joined:
    Nov 16, 2014
    Posts:
    70
    Nahh, I'm on point. If you use an inspector assigned reference to a GameObject you'll always have access to it (clap on). On Awake you could do any needed GetComponents then disable again. I do this all the time... but I guess it is what it is.
     
  16. khanstruct

    khanstruct

    Joined:
    Feb 11, 2011
    Posts:
    2,870
    Ok, now I am confused. As suggested, I removed all of the CanvasGroup, alpha, enable, etc, etc. I have public references to each of my UI game objects and just use SetActive() on them.

    The same object is still not working. It's parent is working as it should, and all three of its children are working as they should. WTF :(

    O. M. G... nevermind.

    Code (csharp):
    1.  
    2. switch (tabClicked)
    3.         {
    4.             case menuTabs.CHARACTERS:
    5.                 characterTab.SetActive(true);
    6.                 PopulateCharacterTab();
    7.                 currentGroup = characterTab;
    8.                 break;
    9.             case menuTabs.INVENTORY:
    10.                 inventoryTab.SetActive(true);
    11.                 currentGroup = inventoryTab;
    12.                 break;
    13.             case menuTabs.PARTY:
    14.                 partyTab.SetActive(true);
    15.                 currentGroup = partyTab;
    16.                 break;
    17.             case menuTabs.SAVE:
    18.                 saveTab.SetActive(true);
    19.                 currentGroup = saveTab;
    20.                 break;
    21.             case menuTabs.SETTINGS:
    22.                 settingsTab.SetActive(true);
    23.                 currentGroup = settingsTab;
    24.                 break;
    25.             default:
    26.                 break;
    27.         }
    28.         currentGroup.SetActive(false); //ARE YOU FREAKING KIDDING ME!?!?!?!?!
    29.  
     
    Last edited: Mar 20, 2015
  17. khanstruct

    khanstruct

    Joined:
    Feb 11, 2011
    Posts:
    2,870
    Well, if it's any consolation for wasting your time, I appreciate the advice to use SetActive, rather than the CanvasGroups. Also, all of my code that was depending on this worked on the first try! :D

    ...feel so stupid :(
     
  18. Feaver1968

    Feaver1968

    Joined:
    Nov 16, 2014
    Posts:
    70
    I don't know about anyone else, but I like trying to help people with UI troubles. I actually learn more through trying to diagnose issues than I would messing around with the generally incomplete documentation.

    Anyway, glad you pinned down the issue. :)
     
    SimonDarksideJ and khanstruct like this.
  19. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,685
    I'm the same @Feaver1968 , I try to help out with all UI issues. But my sig might also give another answer why :p
     
    khanstruct likes this.