Best way of hiding elements in c# using Unity.
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.
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.
-
sfernandez
Site Admin
- Posts: 3184
- Joined:
Re: Best way of hiding elements in c# using Unity.
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:
This makes the button invisible and it takes no space during layout process:
This makes the button invisible but still takes its space during layout process:
Code: Select all
button.SetVisibility(Visibility.Hidden);
Code: Select all
button.SetVisibility(Visibility.Collapsed);
-
sfernandez
Site Admin
- Posts: 3184
- Joined:
Re: Best way of hiding elements in c# using Unity.
To better understand the concept of layout space allocated by an element here you have a xaml example and the resulting image:
Code: Select all
<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>
Re: Best way of hiding elements in c# using Unity.
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!
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!
-
sfernandez
Site Admin
- Posts: 3184
- Joined:
Re: Best way of hiding elements in c# using Unity.
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: No registered users and 4 guests