Version: 2020.2
Supporting drag and drop
Event Type Reference

Synthesizing Events

Before you synthesize and send custom events, you should understand how the UI Toolkit event system allocates and sends operating system events.

The event system uses a pool of events to avoid allocating event objects repeatedly. To synthesize and send your own events, you should allocate and send events by following the same steps:

  1. Get an event object from the pool of events.
  2. Fill in the event properties.
  3. Enclose the event in a using block to ensure it is returned to the event pool.
  4. Pass the event to element.SendEvent().

If you want to send events that usually come from the operating system, such as keyboard events and some mouse events, use a UnityEngine.Event to initialize the UI Toolkit event.

The following example demonstrates how to synthesize and send events:

void SynthesizeAndSendKeyDownEvent(IPanel panel, KeyCode code,
     char character = '\0', EventModifiers modifiers = EventModifiers.None)
{
    // Create a UnityEngine.Event to hold initialization data.
    // Also, this event will be forwarded to IMGUIContainer.m_OnGUIHandler
    var evt = new Event() {
        type = EventType.KeyDownEvent,
        keyCode = code,
        character = character,
        modifiers = modifiers
    };

    using (KeyDownEvent keyDownEvent = KeyDownEvent.GetPooled(evt))
    {
        panel.SendEvent(keyDownEvent);
    }
}


  • 2018–11–02 Page amended
Supporting drag and drop
Event Type Reference
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961