カスタムイベントを統合して送信する前に、UI Toolkit のイベントシステムがオペレーティングシステムのイベントをどのように割り当てて送信するか を理解しておく必要があります。
UI Toolkit sends events to visual elements through the panel. If an event has no target, it’s sent to the root element of the panel. To have a propagation path, an element must have a target, and the sender must set that target in advance. Some event types don’t need a target. For example, keyboard events are sent to the focused element, and pointer events are sent to the element under the pointer.
The event system uses a pool of events to avoid allocating event objects repeatedly.
To synthesize and send your own events:
using
ブロックで囲み、イベントプールに確実に返却されるようにします。element.SendEvent()
に渡します。You can send operating system events, such as keyboard and pointer events. To do so, use a UnityEngine.Event
to initialize the UI Toolkit event.
次の例は、イベントを統合して送信する方法を示しています。
void SynthesizeAndSendKeyDownEvent(IPanel panel, KeyCode code,
char character = '\0', EventModifiers modifiers = EventModifiers.None)
{
// Create a UnityEngine.Event to hold initialization data.
var evt = new Event() {
type = EventType.KeyDownEvent,
keyCode = code,
character = character,
modifiers = modifiers
};
using (KeyDownEvent keyDownEvent = KeyDownEvent.GetPooled(evt))
{
panel.visualTree.SendEvent(keyDownEvent);
}
}
Important: Don’t send events that are from outside the operating system or aren’t present in the UnityEngine.Event
types. UI Toolkit sends some events as a reaction to internal state changes. External processes must not send those events. For example, if you send PointerCaptureEvent
, visual elements assume that the underlying conditions for that event are met and won’t set pointer capture for them. This might break the internal configurations of the visual element and cause undefined behaviors.