Material Box

Material Box

WEB Design & Material Images

Godot - Changing the Background Color of a Button

Godot

This code example shows how to change the background color of a Button node using GDScript.


To change the background color of a Button node, use `add_theme_stylebox_override()` to specify a new `StyleBoxFlat` for each element.
The value of `StyleBoxFlat` stored in a variable will be reflected in the changes, so create them individually without using the same variable.

# Create a Button node
var button = Button.new()
add_child(button)
button.text = "Button"

# Default background color
var normal_bg_color = StyleBoxFlat.new()
normal_bg_color.bg_color = Color("#FF2222", 1)
button.add_theme_stylebox_override("normal", normal_bg_color)

# Background color on hover
var hover_bg_color = StyleBoxFlat.new()
hover_bg_color.bg_color = Color("#000FFF", 1)
button.add_theme_stylebox_override("hover", hover_bg_color)

# Background color when pressed
var pressed_bg_color = StyleBoxFlat.new()
pressed_bg_color.bg_color = Color("#000222", 1)
button.add_theme_stylebox_override("pressed", pressed_bg_color)

Godot

TitleGodot - Changing the Background Color of a Button

CategoryGodot

Created

Update

AuthorYousuke.U