Before you synthesize and send custom events, you should understand how the UI(User Interface) Allows a user to interact with your application. Unity currently supports three UI systems. More info
See in Glossary Toolkit event systemA way of sending events to objects in the application based on input, be it keyboard, mouse, touch, or custom input. The Event System consists of a few components that work together to send events. More info
See in Glossary 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:
using
block to ensure it’s returned to the event pool.element.SendEvent()
.If you want to send events that 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);
}
}