lboldt
Topic Author
Posts: 14
Joined: 13 Aug 2013, 04:03

Create button with image using C# in Unity

17 Aug 2013, 01:28

I'm trying to programatically create
<Button>
            <Button.Template>
                <ControlTemplate>
                    <Image Source="source.png"/>
                </ControlTemplate>
            </Button.Template>
        </Button>
Button b = new Button();
b.SetTemplate()?
Do I need to use ControlTemplate and ImageSource in Image.SetSource?

Could you provide an example?

Thanks
 
User avatar
sfernandez
Site Admin
Posts: 3239
Joined: 22 Dec 2011, 19:20

Re: Create button with image using C# in Unity

19 Aug 2013, 12:09

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?
 
lboldt
Topic Author
Posts: 14
Joined: 13 Aug 2013, 04:03

Re: Create button with image using C# in Unity

19 Aug 2013, 12:54

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

Re: Create button with image using C# in Unity

19 Aug 2013, 13:50

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):
<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>
Then you can set the background of your avatar button by code:
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);
 
lboldt
Topic Author
Posts: 14
Joined: 13 Aug 2013, 04:03

Re: Create button with image using C# in Unity

19 Aug 2013, 15:42

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

Re: Create button with image using C# in Unity

19 Aug 2013, 15:57

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?
That one is easy :)
<TextBlock Text="Hello world!" Foreground="#FFFF0000"/>
Which is equivalent to this xaml:
<TextBlock Text="Hello world!">
    <TextBlock.Foreground>
        <SolidColorBrush Color="#FFFF0000"/>
    </TextBlock.Foreground>
</TextBlock>
And doing the same in code:
TextBlock text = new TextBlock();
text.SetText("Hello world!");
text.SetForeground(new SolidColorBrush(Noesis.Color.FromPackedBGRA(0xFFFF0000)));
 
lboldt
Topic Author
Posts: 14
Joined: 13 Aug 2013, 04:03

Re: Create button with image using C# in Unity

19 Aug 2013, 16:56

Nice, thanks! :-)

Who is online

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