You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Godot中如何实现WindowDialog可调整大小?type索引报错如何解决?

报错原因

Godot的输入事件实例不存在type属性,你不需要通过读取type字段匹配事件类型,直接使用GDScript提供的is关键字做类型判断即可。同时你原代码中用到的relative_pos属性也不存在,正确的相对位移属性为relative。

修正后的可用代码
extends WindowDialog

var newSize
# 额外加个拖拽状态标记,避免鼠标移出右下角区域后还错误触发尺寸调整
var is_resizing = false

func _ready():
    hide()
    set_process_input(true)
    newSize = get_size()

func _input(event):
    # 修正事件类型判断逻辑
    if event is InputEventMouseMotion:
        # 只有当前处于resize状态,或者鼠标在右下角区域同时按下左键,才触发调整
        if is_resizing or (Input.is_mouse_button_pressed(BUTTON_LEFT) and get_local_mouse_position().x > get_size().x -30 and get_local_mouse_position().y > get_size().y -30):
            is_resizing = true
            newSize += event.relative
            set_size(newSize)
    # 鼠标左键松开时重置拖拽状态
    if event is InputEventMouseButton and event.button_index == BUTTON_LEFT and not event.pressed:
        is_resizing = false
更简单的原生实现方案

你不需要自己手写调整大小的逻辑,WindowDialog控件原生支持可调大小属性,直接在编辑器Inspector面板中找到resizable选项,勾选之后就能直接实现拖拽右下角调整窗口大小的效果,官方实现逻辑更稳定。

内容的提问来源于stack exchange,提问作者RockZombie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 13:36:04