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