Material Box

Material Box

WEBデザイン & フリー素材

Godot - Buttonを複数作成して並べる

Godot

GDScriptからButtonノードを複数作成して並べる方法とコード例です。


以下のコード例ではButton.new()で複数のButtonノードを作成して横に並べています。
「size」プロパティを指定する事で各Buttonノードの位置指定を簡単にしています。
「size」プロパティを利用しない場合は「size」と「position」を加算して右端をポジションを割り出します。

# 複数のButtonノードを作成して横に並べる
for i in 5:
	var button = Button.new()
	add_child(button)
	button.set_text("Button")
	button.set_size(Vector2(100, 50))
	button.set_position(Vector2(120 * i, 0))

以下のコード例ではButton.new()で複数のButtonノードを作成して縦に並べています。

# 複数のButtonノードを作成して縦に並べる
for i in 5:
	var button = Button.new()
	add_child(button)
	button.set_text("Button")
	button.set_size(Vector2(100, 50))
	button.set_position(Vector2(0, 70 * i))

Godot

TitleGodot - Buttonを複数作成して並べる

CategoryGodot

Created

Update

AuthorYousuke.U