Material Box

Material Box

WEBデザイン & フリー素材

Godot - Buttonの背景色を変更する

Godot

GDScriptからButtonノードの背景色を変更する方法とコード例です。


Buttonノードの背景色を変更するにはadd_theme_stylebox_override()にて各要素に新しくStyleBoxFlatを指定していきます。
変数に格納したStyleBoxFlatの値は変更が反映されますので、同じ変数を利用せずに個別に作成します。

# Buttonノードを作成する
var button = Button.new()
add_child(button)
button.text = "Button"

# デフォルトの背景色
var normal_bg_color = StyleBoxFlat.new()
normal_bg_color.bg_color = Color("#FF2222", 1)
button.add_theme_stylebox_override("normal", normal_bg_color)

# ホバー時の背景色
var hover_bg_color = StyleBoxFlat.new()
hover_bg_color.bg_color = Color("#000FFF", 1)
button.add_theme_stylebox_override("hover", hover_bg_color)

# 押された時の背景色
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 - Buttonの背景色を変更する

CategoryGodot

Created

Update

AuthorYousuke.U