Create button with image using C# in Unity
I'm trying to programatically create
Do I need to use ControlTemplate and ImageSource in Image.SetSource?
Could you provide an example?
Thanks
Code: Select all
<Button>
<Button.Template>
<ControlTemplate>
<Image Source="source.png"/>
</ControlTemplate>
</Button.Template>
</Button>
Code: Select all
Button b = new Button();
b.SetTemplate()?
Could you provide an example?
Thanks
-
-
sfernandez
Site Admin
- Posts: 3239
- Joined:
Re: Create button with image using C# in Unity
Hi,
Where does the image come from?
It is an image placed in your project folder?
I mean, why do you need to create the template programmatically?
Where does the image come from?
It is an image placed in your project folder?
I mean, why do you need to create the template programmatically?
Re: Create button with image using C# in Unity
I have a "group" GUI where each person added to the group GUI ends up as a button with an avatar and health bar.
These avatars are placed in a "Resources" folder in Unity. So the buttons with avatar and healthbar needs to be added at runtime I thought?
These avatars are placed in a "Resources" folder in Unity. So the buttons with avatar and healthbar needs to be added at runtime I thought?
-
-
sfernandez
Site Admin
- Posts: 3239
- Joined:
Re: Create button with image using C# in Unity
If your images are predefined and available in your Unity project folder, I think the best option is to follow the instructions Jesus described in the topic "How to change Image source at runtime?".
You set a dictionary with all your images (you can even create an image atlas using our TexturePacker template):
Then you can set the background of your avatar button by code:
You set a dictionary with all your images (you can even create an image atlas using our TexturePacker template):
Code: Select all
<Grid
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" UseLayoutRounding="True" >
<Grid.Resources>
<ImageBrush x:Key="avatar0" ImageSource="avatar0.jpg" Stretch="Fill" />
<ImageBrush x:Key="avatar1" ImageSource="avatar1.jpg" Stretch="Fill" />
<ImageBrush x:Key="avatar2" ImageSource="avatar2.jpg" Stretch="Fill" />
<Style x:Key="AvatarButtonStyle" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border BorderBrush="Gold" BorderThickness="2" CornerRadius="4" Background="{TemplateBinding Background}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
<StackPanel x:Name="Avatars" HorizontalAlignment="Left" VerticalAlignment="Top" />
</Grid>
Code: Select all
NoesisGUIPanel gui = GetComponent<NoesisGUIPanel>();
FrameworkElement root = gui.GetRoot<FrameworkElement>();
Button avatarBtn = new Button();
avatarBtn.SetWidth(48.0f);
avatarBtn.SetHeight(48.0f);
Brush avatarBtnBg = root.FindStringResource<Brush>("avatar0");
avatarBtn.SetBackground(avatarBtnBg);
Style avatarBtnStyle = root.FindStringResource<Style>("AvatarButtonStyle");
avatarBtn.SetStyle(avatarBtnStyle);
StackPanel avatars = root.FindName<StackPanel>("Avatars");
avatars.GetChildren().Add(avatarBtn);
Re: Create button with image using C# in Unity
I see, that should work nicely.
I'm also struggelig with something that I thought was simple, setting TextBlock foreground color. It asks for a Noesis.Brush, but how do you specify a color in the brush for the foreground?
Thanks!
I'm also struggelig with something that I thought was simple, setting TextBlock foreground color. It asks for a Noesis.Brush, but how do you specify a color in the brush for the foreground?
Thanks!
-
-
sfernandez
Site Admin
- Posts: 3239
- Joined:
Re: Create button with image using C# in Unity
That one is easyI'm also struggelig with something that I thought was simple, setting TextBlock foreground color. It asks for a Noesis.Brush, but how do you specify a color in the brush for the foreground?

Code: Select all
<TextBlock Text="Hello world!" Foreground="#FFFF0000"/>
Code: Select all
<TextBlock Text="Hello world!">
<TextBlock.Foreground>
<SolidColorBrush Color="#FFFF0000"/>
</TextBlock.Foreground>
</TextBlock>
Code: Select all
TextBlock text = new TextBlock();
text.SetText("Hello world!");
text.SetForeground(new SolidColorBrush(Noesis.Color.FromPackedBGRA(0xFFFF0000)));
Who is online
Users browsing this forum: Ahrefs [Bot], Semrush [Bot] and 31 guests