Version: 2022.3
언어: 한국어
Handle events
이벤트 레퍼런스

Synthesize and send events

Before you synthesize and send custom events, 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:

  1. 이벤트 풀에서 이벤트 오브젝트를 가져옵니다.
  2. 이벤트 프로퍼티를 채웁니다.
  3. 이벤트를 using 블록으로 둘러싸서 이벤트 풀에 반환되도록 만듭니다.
  4. Pass the event to panel.visualTree.SendEvent().

You can send operating system events, such as keyboard and mouse 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);
    }
}

Note: Don’t send events that don’t come from the operating system and can’t be found in the UnityEngine.Event types. Some events are sent by UI Toolkit as a reaction to internal state changes and must not be sent by external processes. 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.

추가 리소스

Handle events
이벤트 레퍼런스
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961