Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

another HUD question (showing/hiding)

Discussion in 'General Discussion' started by tomjoad, Apr 23, 2007.

  1. tomjoad

    tomjoad

    Joined:
    Apr 14, 2007
    Posts:
    37
    Hi~

    Maybe a very simple question: assuming one has several control panel type screens (using GUI elements) that the user can call up one at a time, what is the best approach in Unity for showing/hiding them?

    I've looked at layers, wondering if I should

    -assign different layers to the elements of each control panel?

    -create an empty game object for each control panel, add the control panel elements as children and then enable/disable it - I tried this but the game object's children still render.

    Also tried using GUIlayer but that adds a new camera to the scene (?).

    --Roy
     
  2. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    The easiest way is to keep each panel in its own hierarchy and enable/disable the individual GUI element components in the children.

    Hint: GetComponentsInChildren is your friend.
     
  3. tomjoad

    tomjoad

    Joined:
    Apr 14, 2007
    Posts:
    37
    thanks! - I think I've got it

    Code (csharp):
    1.  
    2. //toggle HUD
    3.  
    4. var target: GameObject;
    5.  
    6. function OnMouseUp(){
    7.     var uiParts = new Array(GUITexture, GUIText);
    8.     for (part in uiParts){
    9.     var uiList = target.GetComponentsInChildren(part); 
    10.         for(i in uiList){
    11.             if (i.enabled == true){
    12.                 i.enabled = false;
    13.             } else {
    14.                 i.enabled = true;
    15.             }
    16.         }
    17.     }
    18. }
    19.  
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    For the sake of efficiency, you'd want to declare uiParts in Start(), as well as setting up uiList in Start() too, so you don't have to do the GetComponents thing every time the function is called. (Although it would be more important if that was an Update instead of OnMouseUp, but still, it's the principle....) Also, make that:

    Code (csharp):
    1.  
    2.         for(i in uiList){
    3.             i.enabled = !i.enabled;
    4.         }
    5.  
    --Eric
     
  5. tomjoad

    tomjoad

    Joined:
    Apr 14, 2007
    Posts:
    37
    that's great - thanks for the tip! Still feeling my way with all this (coming from years of Shockwave 3D coding - things are a tad different..)

    I changed the scope of that function so now it can be called from anywhere (thanks to NCarter for some tips on that).

    Code (csharp):
    1.  
    2. static function ToggleNamedHUD(HudName: String){
    3. //toggles display of the named HUD game object
    4. //and all it's children
    5.     var target =GameObject.Find(HudName);
    6.     if (target ==null) return;
    7.     var uiParts = new Array(GUITexture, GUIText);
    8.     for (part in uiParts){
    9.     var uiList = target.GetComponentsInChildren(part); 
    10.         for(i in uiList){
    11.             i.enabled = !i.enabled;
    12.         }
    13.     }
    14. }
    15.