マウスイベントは、マウスを使って UI を操作するときに発生します。タッチ、ペン、その他のポインティングデバイスは、マウスイベントではなく、他のイベントを生成します。マウスイベントのスクリプトリファレンスとこのドキュメントでは、“mouse” (マウス) という用語は、物理的なマウスまたは物理的なマウスをエミュレートする仮想マウスのみを指します。
マウスイベントの前には、必ず対応する PointerEvent
があります。
すべてのマウスイベントの基本クラスは MouseEventBase です。
イベント | 説明 | 下降伝播 | 上昇伝播 | キャンセル可能 |
---|---|---|---|---|
MouseDownEvent | ユーザーがマウスボタンを押すときに送信されます。 | 可 | 可 | 可 |
MouseUpEvent | ユーザーがマウスボタンを離すときに送信されます。 | 可 | 可 | 可 |
MouseMoveEvent | ユーザーがマウスを動かすと送信されます。 | 可 | 可 | 可 |
WheelEvent | ユーザーがマウスホイールをアクティブにすると送信されます。 | 可 | 可 | 可 |
MouseEnterWindowEvent | マウスがウィンドウに入るときに送信されます。 | 可 | ||
MouseLeaveWindowEvent | マウスがウィンドウを離れるときに送信されます。 | 可 | ||
MouseEnterEvent | マウスが要素またはその子孫に入るときに送信されます。 | 可 | 可 | |
MouseLeaveEvent | マウスが要素またはその子孫から離れるときに送信されます。 | 可 | 可 | |
MouseOverEvent | マウスが要素に入るときに送信されます。 | 可 | 可 | 可 |
MouseOutEvent | マウスが要素から離れるときに送信されます。 | 可 | 可 | 可 |
ContextClickEvent (廃止予定) | ユーザーが第 3 のマウスボタンを押して離すときに送信されます。IMGUI との後方互換性のために存在します。 | 可 | 可 | 可 |
button
: button プロパティは、イベントを発生させるために押されたマウスボタンを識別する整数を返します。以下の表は、整数とそれに紐づけられたマウスボタンの一覧です。
整数 | ボタン |
---|---|
0 | 左ボタン |
1 | 右ボタン |
2 | 中央ボタン |
pressedButtons
: pressedButton
プロパティは、現在押されているマウスボタンの組み合わせを識別する整数を返します。
この数値は、個々のボタンの整数値の合計です (下表参照)。例えば、マウスの右ボタンと中ボタンを同時に押すと、pressedButton の値は 6 になります。
整数 | ボタン |
---|---|
1 | 左ボタン |
2 | 右ボタン |
4 | 中央ボタン |
modifiers
: modifiers
プロパティは、キーボードイベント中に押された修飾キーを返します。修飾キーの例としては、 Shift
、Ctrl
、Alt
キーなどがあります。
詳細は、修飾キー のセクションを参照してください。
mousePosition
: mousePosition
プロパティは、パネル内のマウスの位置 (スクリーン座標系とも呼ばれます) を返します。パネル座標の詳細については、ビジュアルツリー を参照してください。
localMousePosition
: localMousePosition
プロパティは、ターゲットとなるビジュアル要素に対し相対的な座標を返します。
mouseDelta
: 前のマウスイベント時のポインターと、現在のマウスイベント時のポインターの位置の差。
以下のリストは、イベントファミリーの各イベントの名前、説明、ターゲットを示しています。イベントの詳細については、UI Toolkit API を参照してください。
MouseDownEvent は、カーソルがビジュアル要素内にあるときにマウスボタンが押されると送信されます。
target
: マウスキャプチャを受け取るビジュアル要素。それ以外の場合は、カーソルの下の一番上の選択可能な要素。
MouseUpEvent は、カーソルがビジュアル要素内にあるときにマウスボタンを離すと発信されます。MouseUpEvent
は MouseDownEvent
を補完するものです。
target
: マウスキャプチャを受け取るビジュアル要素。それ以外の場合は、カーソルの下の一番上の選択可能な要素。
MouseMoveEvent は、カーソルのホットスポットがビジュアル要素内で移動するときに送信されます。
target
: マウスキャプチャを受け取るビジュアル要素。それ以外の場合は、カーソルの下の一番上の選択可能な要素。
WheelEvent は、マウスホイールが押されると送信されます。
target
: マウスキャプチャを受け取るビジュアル要素。それ以外の場合は、カーソルの下の一番上の選択可能な要素。
MouseEnterWindowEvent は、カーソルがエディターウィンドウに移動するときに送信されます。ランタイムパネルでは、ゲームビューに入ると、このイベントを受け取りません。
target
: マウスキャプチャを受け取るビジュアル要素。それ以外の場合は、カーソルの下の一番上の選択可能な要素。
MouseLeaveWindowEvent は、カーソルがエディターウィンドウのスペースを出るときに発生します。MouseLeaveWindowEvent
は、MouseEnterWindowEvent
の逆です。
target
: マウスキャプチャーを受け取るビジュアル要素。取得時以外は、カーソルが要素の上にないため null を返します。
MouseEnterEvent は、カーソルがビジュアル要素、またはその子孫の 1 つに移動するときに送信されます。
target
: マウスカーソルの下にあるビジュアル要素、またはその子孫の 1 つ。
MouseLeaveEvent は、カーソルがビジュアル要素の外に移動するときに送信されます。このイベントは MouseOutEvent
とは異なり、このイベントはマウスが離れる各要素に送られます。このイベントは伝播しません。
target
: マウスカーソルが離れるビジュアル要素 (またはその子孫の 1 つ)。
MouseOverEvent は、カーソルが要素に入ると送信されます。このイベントは MouseEnterEvent
とは異なり、侵入された要素にのみ送信されるます。
target
: マウスカーソルの下にあるビジュアル要素。
MouseOutEvent は、ポインティングデバイスのカーソルがビジュアル要素の境界の外に移動するときに送信されます。
MouseOutEvent
は、 MouseLeaveEvent
とは異なります。MouseOutEvent
は、ビジュアル要素から離れて他の要素に移動するときには常に送信されますが、MouseLeaveEvent
は、ビジュアル要素から子孫の要素に移動するときには送信されません。
target
: マウスカーソルが離れたビジュアル要素。
コンテキストメニューにメニュー項目を作成する必要があるときに、ContextualMenuManager
によって送信されるイベント
target
: コンテキストメニューが作成される要素。
ユーザーが 3 番目のマウスボタンを押下、離すときに送信されるイベント。このイベントは IMGUI との後方互換性のためにのみ存在します。
以下のコードサンプルでは、3 つのボタンを持つエディターウィンドウを作成し、マウスが要素の上に移動したり、マウスのボタンが押されたりすると、コンソールにメッセージを表示します。
このコードサンプルでは、MouseDownEvent
と MouseEnterEvent
の両方のイベント送信と、イベントパラメーターの使用方法を紹介しています。
この例の動作を実際に見るには、以下を行います。
MouseEventTestWindow.cs
という名前の新しい C# スクリプトを作成します。using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
// Open this in the Editor via the menu Window > UI ToolKit > Mouse Event Test Window
public class MouseEventTestWindow : EditorWindow
{
[MenuItem("Window/UI Toolkit/Mouse Event Test Window")]
public static void ShowExample()
{
MouseEventTestWindow wnd = GetWindow<MouseEventTestWindow>();
wnd.titleContent = new GUIContent("Mouse Event Test Window");
}
public void CreateGUI()
{
// Add a few buttons
for (int i = 0; i < 3; i++)
{
Button newElement = new Button { name = $"Button {i}", text = $"Button {i}" };
newElement.style.flexGrow = 1;
rootVisualElement.Add(newElement);
}
// Register mouse event callbacks
rootVisualElement.RegisterCallback<MouseDownEvent>(OnMouseDown, TrickleDown.TrickleDown);
rootVisualElement.RegisterCallback<MouseEnterEvent>(OnMouseEnter, TrickleDown.TrickleDown);
}
private void OnMouseDown(MouseDownEvent evt)
{
bool leftMouseButtonPressed = 0 != (evt.pressedButtons & (1 << (int)MouseButton.LeftMouse));
bool rightMouseButtonPressed = 0 != (evt.pressedButtons & (1 << (int)MouseButton.RightMouse));
bool middleMouseButtonPressed = 0 != (evt.pressedButtons & (1 << (int)MouseButton.MiddleMouse));
Debug.Log($"Mouse Down event. Triggered by {(MouseButton)evt.button}.");
Debug.Log($"Pressed buttons: Left button: {leftMouseButtonPressed} Right button: {rightMouseButtonPressed} Middle button: {middleMouseButtonPressed}");
}
private void OnMouseEnter(MouseEnterEvent evt)
{
VisualElement targetElement = (VisualElement)evt.target;
Debug.Log($"Mouse is now over element '{targetElement.name}'");
}
}
以下のコードサンプルは、マウスのボタンが押されるときに、どのボタンがイベントを送信したか、現在どのボタンが押されているかを示すメッセージをコンソールに表示します。
このコードサンプルは、MouseDownEvent
へのコールバックの登録と、イベントパラメーターの使用方法を紹介します。
この例を実際に見るには、以下のようにします。
using UnityEngine;
using UnityEngine.UIElements;
public class MouseEventTestRuntime : MonoBehaviour
{
void Start()
{
var root = GetComponent<UIDocument>().rootVisualElement;
var newLabel = new Label("Move the mouse or press buttons to see the log output");
newLabel.style.flexGrow = 1;
root.Add(newLabel);
root.RegisterCallback<MouseDownEvent>(OnMouseDown, TrickleDown.TrickleDown);
}
private void OnMouseDown(MouseDownEvent evt)
{
bool leftMouseButtonPressed = 0 != (evt.pressedButtons & (1 << (int)MouseButton.LeftMouse));
bool rightMouseButtonPressed = 0 != (evt.pressedButtons & (1 << (int)MouseButton.RightMouse));
bool middleMouseButtonPressed = 0 != (evt.pressedButtons & (1 << (int)MouseButton.MiddleMouse));
VisualElement targetElement = (VisualElement)evt.target;
Debug.Log($"Mouse Down event. Triggered by {(MouseButton)evt.button} over element '{targetElement.name}'");
Debug.Log($"Pressed buttons: Left button: {leftMouseButtonPressed} Right button: {rightMouseButtonPressed} Middle button: {middleMouseButtonPressed}");
}
}
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.