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
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.
thanks! - I think I’ve got it
//toggle HUD
var target: GameObject;
function OnMouseUp(){
var uiParts = new Array(GUITexture, GUIText);
for (part in uiParts){
var uiList = target.GetComponentsInChildren(part);
for(i in uiList){
if (i.enabled == true){
i.enabled = false;
} else {
i.enabled = true;
}
}
}
}
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:
for(i in uiList){
i.enabled = !i.enabled;
}
–Eric
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).
static function ToggleNamedHUD(HudName: String){
//toggles display of the named HUD game object
//and all it's children
var target =GameObject.Find(HudName);
if (target ==null) return;
var uiParts = new Array(GUITexture, GUIText);
for (part in uiParts){
var uiList = target.GetComponentsInChildren(part);
for(i in uiList){
i.enabled = !i.enabled;
}
}
}