我想在脚本中做一个下拉列表容器

发布于 2025-01-19 21:04:59 字数 158 浏览 2 评论 0原文

我想创建一个下拉容器来组织我的导出变量。是否可以在脚本中创建自定义下拉列表容器?

这样:
“

I want to create a dropdown container to organize my export variable. Is it possible to create a custom dropdown container in the script?

Like this:
Image in inspector

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

缘字诀 2025-01-26 21:04:59

这是执行此操作的另一种方法。它还要求脚本是 工具< /a>.

我们需要这种方法作为要分组的变量的公共前缀。优点是我们不需要 _get_set

tool
extends Node

var custom_position:Vector2
var custom_rotation_degrees:float
var custom_scale:Vector2

func _get_property_list():
    return [
        {
            name = "Custom",
            type = TYPE_NIL,
            hint_string = "custom_",
            usage = PROPERTY_USAGE_GROUP
        },
        {
            name = "custom_position",
            type = TYPE_VECTOR2
        },
        {
            name = "custom_rotation_degrees",
            type = TYPE_REAL
        },
        {
            name = "custom_scale",
            type = TYPE_VECTOR2
        }
    ]

如您所见,我们定义了一个带有 name 的类别,该类别将出现在检查器面板,hint_string 是我们将使用的前缀。将类别放在数组中的属性之前很重要。

请参阅:添加脚本类别

附录:使用PROPERTY_USAGE_CATEGORY将生成一个命名标头,类似于图片上显示“Node2D”的标头关于这个问题。使用 PROPERTY_USAGE_GROUP 创建可折叠组。

This is another approach to do this. It also requires the script to be tool.

What we need for this approach as a common prefix for the variables you want to group. The advantage is that we don't need _get and _set:

tool
extends Node

var custom_position:Vector2
var custom_rotation_degrees:float
var custom_scale:Vector2

func _get_property_list():
    return [
        {
            name = "Custom",
            type = TYPE_NIL,
            hint_string = "custom_",
            usage = PROPERTY_USAGE_GROUP
        },
        {
            name = "custom_position",
            type = TYPE_VECTOR2
        },
        {
            name = "custom_rotation_degrees",
            type = TYPE_REAL
        },
        {
            name = "custom_scale",
            type = TYPE_VECTOR2
        }
    ]

As you can see we define a category with a name that will appear in the Inspector panel, and the hint_string is the prefix we will use. It is important to put the category before the properties in the array.

See: Adding script categories

Addendum: Using PROPERTY_USAGE_CATEGORY will produce a named header, similar to the one that says "Node2D" on the picture on the question. Use PROPERTY_USAGE_GROUP to make a collapsible group.

我的影子我的梦 2025-01-26 21:04:59

是的,您可以做到这一点,但是(我认为)这有点丑陋,并缩短了您的脚本。您需要将脚本标记为工具脚本,并覆盖_get_SET _SET 和_get_property_list函数。

一个基于您的屏幕截图的示例(不是100%确定这完全可以正常工作;我还基于一个最近的项目,此后我将其删除并在某种程度上重组了项目/代码/节点,因为略带UI' t值得脚本中的其他混乱):

tool
extends Node2D

# Note that these are NOT exported
var actual_position: Vector2
var actual_rotation: float
var actual_scale: Vector2

# Function to enumerate the properties to list in the editor
#  - Not actually directly/automatically backed by variables
#  - Note the naming pattern - it is {group heading}/{variable}
func _get_property_list():
    var props = []
    props.append({name="transform/position", type=TYPE_VECTOR2})
    props.append({name="transform/rotation deg", type=TYPE_FLOAT}) # might not exist; look at docs to determine appropriate type hints for your properties
    props.append({name="transform/scale", type=TYPE_VECTOR2})
    return props


# Now the get/set functions to map the names shown in the editor to actual script variables
# Property names as input here will match what is displayed in the editor (what is enumerated in _get_property_list); just get/set the appropriate actual variable based on that
func _get(property: String):
    if property == "transform/position":
        return actual_position
    if property == "transform/rotation deg":
        return actual_rotation
    if property == "transform/scale":
        return actual_scale

func _set(property: String, value):
    if property == "transform/position":
        actual_position = value
        return true
    if property == "transform/rotation deg":
        actual_rotation = value
        return true
    if property == "transform/scale":
        actual_scale = value
        return true
    # Not a supported property
    return false

请注意,此答案基于Godot 3.4。我不确定Godot 4中是否可以(或将)使用更简单的方法。

Yes, you can do this, but (in my opinion) it is a bit ugly and clutters up your script. You need to mark your script as a tool script and override the _get, _set, and _get_property_list functions.

An example based on your screenshot (not 100% sure this works exactly as-is; I'm also basing it on a recent project where I have since removed it and somewhat reorganized the project/code/node because the slightly nicer UI wasn't worth the additional clutter in the script):

tool
extends Node2D

# Note that these are NOT exported
var actual_position: Vector2
var actual_rotation: float
var actual_scale: Vector2

# Function to enumerate the properties to list in the editor
#  - Not actually directly/automatically backed by variables
#  - Note the naming pattern - it is {group heading}/{variable}
func _get_property_list():
    var props = []
    props.append({name="transform/position", type=TYPE_VECTOR2})
    props.append({name="transform/rotation deg", type=TYPE_FLOAT}) # might not exist; look at docs to determine appropriate type hints for your properties
    props.append({name="transform/scale", type=TYPE_VECTOR2})
    return props


# Now the get/set functions to map the names shown in the editor to actual script variables
# Property names as input here will match what is displayed in the editor (what is enumerated in _get_property_list); just get/set the appropriate actual variable based on that
func _get(property: String):
    if property == "transform/position":
        return actual_position
    if property == "transform/rotation deg":
        return actual_rotation
    if property == "transform/scale":
        return actual_scale

func _set(property: String, value):
    if property == "transform/position":
        actual_position = value
        return true
    if property == "transform/rotation deg":
        actual_rotation = value
        return true
    if property == "transform/scale":
        actual_scale = value
        return true
    # Not a supported property
    return false

Note that this answer is based on Godot 3.4. I'm not sure if a simpler approach is (or will be) available in Godot 4.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文