Justei
Topic Author
Posts: 17
Joined: 28 Aug 2013, 21:03

Best way of hiding elements in c# using Unity.

29 Aug 2013, 20:54

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.
 
User avatar
sfernandez
Site Admin
Posts: 2995
Joined: 22 Dec 2011, 19:20

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

29 Aug 2013, 21:20

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);
 
User avatar
sfernandez
Site Admin
Posts: 2995
Joined: 22 Dec 2011, 19:20

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

29 Aug 2013, 21:34

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 2582 times
 
Justei
Topic Author
Posts: 17
Joined: 28 Aug 2013, 21:03

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

29 Aug 2013, 21:44

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!
 
User avatar
sfernandez
Site Admin
Posts: 2995
Joined: 22 Dec 2011, 19:20

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

29 Aug 2013, 21:57

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 ;)

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Semrush [Bot] and 3 guests