Up to date
This page is up to date for Godot 4.2
.
If you still find outdated information, please open an issue.
マウスと入力座標¶
概要¶
この小さなチュートリアルの理由は、入力座標、マウスの位置や画面の解像度などに関する多くのよくある間違いを解決するためです。
ハードウェア表示の座標¶
エディタ、MMO、ツールなど、PC上で実行する複雑なUIを記述する場合、ハードウェア座標の使用は理にかなっています。しかし、その以外ではあまり意味がありません。
ビューポート表示の座標¶
Godotはビューポートを使用してコンテンツを表示し、ビューポートはいくつかのオプションでスケーリングできます(複数の解像度 チュートリアルを参照)。次に、ノード内の関数を使用して、マウス座標とビューポートサイズを取得します。次に例を示します:
func _input(event):
# Mouse in viewport coordinates.
if event is InputEventMouseButton:
print("Mouse Click/Unclick at: ", event.position)
elif event is InputEventMouseMotion:
print("Mouse Motion at: ", event.position)
# Print the size of the viewport.
print("Viewport Resolution is: ", get_viewport().get_visible_rect().size)
public override void _Input(InputEvent @event)
{
// Mouse in viewport coordinates.
if (@event is InputEventMouseButton eventMouseButton)
GD.Print("Mouse Click/Unclick at: ", eventMouseButton.Position);
else if (@event is InputEventMouseMotion eventMouseMotion)
GD.Print("Mouse Motion at: ", eventMouseMotion.Position);
// Print the size of the viewport.
GD.Print("Viewport Resolution is: ", GetViewport().GetVisibleRect().Size);
}
または、ビューポートにマウスの位置を問い合わせるようにします:
get_viewport().get_mouse_position()
GetViewport().GetMousePosition();
注釈
When the mouse mode is set to Input.MOUSE_MODE_CAPTURED
, the event.position
value from InputEventMouseMotion
is the center of the screen. Use event.relative
instead of event.position
and event.velocity
to process mouse movement and position changes.