マウスと入力座標¶
概要¶
この小さなチュートリアルの理由は、入力座標、マウスの位置や画面の解像度などに関する多くのよくある間違いを解決するためです。
ハードウェア表示の座標¶
エディタ、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_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: ", GetViewportRect().Size);
}
または、ビューポートにマウスの位置を問い合わせるようにします:
get_viewport().get_mouse_position()
GetViewport().GetMousePosition();
注釈
マウスモードが Input.MOUSE_MODE_CAPTURED
に設定されている場合、InputEventMouseMotion
の event.position
値は画面の中心になります。マウスの移動と位置の変更を処理するには、event.position``と ``event.speed
の代わりに event.relative
を使用します。