godot 4.0 tool_button
Old Version
編輯按鈕一行代碼: 代碼零行。@tool
只需安裝,然後選擇任何節點:
有關更高級的功能,請閱讀下面。

@tool添加到腳本的頂部。_get_tool_buttons() -> Array方法。"my_method"的名稱my_methodmethod.bind(true, "ok" 1.0)func(): print("Hey!")func press_me(): print("You pressed me!")"my_signal"my_signal["my_signal", [true, "ok"]]{call=method.bind(true), text="My Method", tint=Color.RED}["my_method", method2.bind(1.0), "a_signal", {text="X", call=reset}]數組
要指定在檢查器上方顯示的按鈕,請添加_get_tool_buttons func。

與Strings :
@tool
extends Node
func _get_tool_buttons() :
return [ boost_score, remove_player ]
func boost_score() :
Player.score += 100
func remove_player() :
Player.queue_free()和/或Callable s
# WARNING, some stuff won't work: If you get *"Cannot access member without instance"*: https://github.com/godotengine/godot/issues/56780
@tool
extends Node
func _get_tool_buttons() :
return [
func add_score() : score += 10 ,
func reset_health() : health = 0
]和/或Dictionarys
@tool
extends Node
signal reset()
func _get_tool_buttons() : return [
" boost_score " ,
{ call = " boost_score " , args = [ 100 ] , tint = Color.DEEP_SKY_BLUE } ,
{ call = " emit_signal " , args = [ " reset " ] , text = " Reset " , tint = Color.TOMATO } ,
{
call = func() : print( " My Health: " , health),
text = " Print Health " ,
tint = func() : return Color(health * . 1 , 0.0 , 0.0 )
lock = func() : return health == 100
}
]
func boost_score(x = 10 ) :
Player.score += x call是強制性的。他人是可選的。
| 鑰匙 | desc | 預設 |
|---|---|---|
| 稱呼 | 打電話的方法。 | - |
| args | 一系列論點要通過。 (鼠標按鈕以查看Args。) | - |
| 文字 | 按鈕標籤。 | - |
| 著色 | 按鈕顏色。 | 顏色。白色 |
| 圖示 | 按鈕圖標。 | - |
| 平坦的 | 按鈕是平坦的樣式。 | 錯誤的 |
| 暗示 | 提示鼠標的文本。 | - |
| 列印 | 打印方法調用? | 真的 |
| 對齊 | 按鈕對齊。 | BoxContainer.Alignment_Center |
| 鎖 | 禁用按鈕? | 錯誤的 |
| update_filesystem | 告訴Godot編輯恢復文件系統。 | 錯誤的 |
對於_get_tool_buttons來說,要在需要靜態的Resource上工作。
@tool
extends Resource
class_name MyResource
# STATIC
static func _get_tool_buttons() :
return [ " my_button " ]
# LOCAL
export(String) var my_name : String = " "
func my_button() :
print(my_name)signal my_signal()
signal my_arg_signal(x, y)
func _get_tool_buttons() :
return [
my_signal,
[ my_arg_signal, [ true , " okay " ]]
] 您可以在同一行上有多個按鈕。
它們都可以立即根據切換(默認值)或單獨調用。
func _get_tool_buttons():
return [
[msg, msg.bind("yes"), msg.bind("no")],
]
func msg(msg := "Default Message"):
print("Got: ", msg)
func doit():
pass
@SELECT_AND_EDIT::file_path , @EDIT_RESOURCE::file_path , @SELECT_FILE::file_path 。 4.0.alpha2Callable支持call text icon tint lock : { text="Callable", call=func(): print("From Callable with love") }disabled更改為lock