The event system listens for events that come from the operating system or scripts, then uses the EventDispatcher to dispatch those events to visual elements. The event dispatcher determines an appropriate dispatching strategy for each event it sends. Once determined, the dispatcher executes the strategy.
Visual elements implement default behaviors for several events. This involves the creation and execution of additional events. For example, a PointerMoveEvent
can generate an additional PointerEnterEvent
and a PointerLeaveEvent
. These events enter a queue and process after the current event. For example, the PointerMoveEvent
finishes processing before the PointerEnterEvent
and PointerLeaveEvent
events.
Each event type has its own dispatch behavior. The behavior of each event type breaks down into two stages:
有关每种事件类型的分发行为的列表,请参阅事件参考页面 。
事件分发程序选择事件目标后,会计算事件的传播路径。传播路径是接收事件的视觉元素的有序列表。传播路径按以下顺序发生:
大多数事件类型将沿着传播路径发送到所有元素。某些事件类型跳过冒泡阶段,某些事件类型仅发送到事件目标。
如果元素被隐藏或禁用,它将不会接收事件。事件仍会传播到被隐藏或禁用的元素的祖先和后代。
在事件沿着传播路径行进时,Event.currentTarget
更新为当前正在处理该事件的元素。在事件回调函数中,有两个属性记录了分发行为:
EventBase.currentTarget
is the visual element on which the callback was registered.EventBase.target
is the element where the event occurs, for example, the element directly under the pointer.The target of an event depends on the event type. For pointer events, the target is most commonly the topmost pickable element, directly under the pointer. For keyboard events, the target is the element that has focus.
UI Toolkit events have a target
property that contains a reference to the element where the event occurred. For most events that originate from the operating system, the dispatch process finds the event target automatically.
目标元素存储在 EventBase.target
中并且在分发过程中不会改变。属性 Event.currentTarget
会更新为当前正在处理该事件的视觉元素。
Most pointer events use the picking mode to decide their target. The VisualElement
class has a pickingMode
property that supports the following values:
PickingMode.Position
(default): performs picking based on the position rectangle.PickingMode.Ignore
: prevents picking as the result of a pointer event.You can override the VisualElement.ContainsPoint()
method to perform custom intersection logic.
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.