Page 1 of 1

Best way of hiding elements in c# using Unity.

Posted: 29 Aug 2013, 20:54
by Justei
Hi there, I was wondering what the best way of hiding elements dynamically would be using C# in Unity.

For example say a button, what is the best way of toggling it, so that I can make it visible or invisible at will.

Thanks.

Re: Best way of hiding elements in c# using Unity.

Posted: 29 Aug 2013, 21:20
by sfernandez
Considering you have a reference of the button, you just make use of the SetVisibility() function.

This makes the button invisible but still takes its space during layout process:
button.SetVisibility(Visibility.Hidden);
This makes the button invisible and it takes no space during layout process:
button.SetVisibility(Visibility.Collapsed);

Re: Best way of hiding elements in c# using Unity.

Posted: 29 Aug 2013, 21:34
by sfernandez
To better understand the concept of layout space allocated by an element here you have a xaml example and the resulting image:
<Grid
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  
    <StackPanel Orientation="Horizontal"
      HorizontalAlignment="Center" VerticalAlignment="Center">
        <StackPanel Background="LightYellow" VerticalAlignment="Center">
            <Button Content="Button" Margin="10,0"/>
            <Button Content="Button" Margin="10,0" Visibility="Hidden"/>
            <Button Content="Button" Margin="10,0"/>
        </StackPanel>
        <StackPanel Background="LightGreen" VerticalAlignment="Center">
            <Button Content="Button" Margin="10,0"/>
            <Button Content="Button" Margin="10,0" Visibility="Collapsed"/>
            <Button Content="Button" Margin="10,0"/>
        </StackPanel>
    </StackPanel>
</Grid>
visibility.png
visibility.png (1.98 KiB) Viewed 1983 times

Re: Best way of hiding elements in c# using Unity.

Posted: 29 Aug 2013, 21:44
by Justei
Thanks a lot! :)
For some reason, monodevelop didn't want to work with me, thus not showing the function SetVisibility when I looked for it. But it works like a charm, thanks!

Re: Best way of hiding elements in c# using Unity.

Posted: 29 Aug 2013, 21:57
by sfernandez
I totally recommend you use Visual Studio for editing your scripts if possible. The response speed is far better than using MonoDevelop. And use MonoDevelop just for debugging the scripts ;)