Events
Handle user interactions with React-style event props.
Click Events
<Button
text="Click me"
onClick={(e) => {
console.log("Button clicked!")
console.log("Position:", e.x, e.y)
}}
/>Pointer Events
Track pointer (mouse/touch) interactions:
<View
style={{ width: 200, height: 200, backgroundColor: "#333" }}
onPointerDown={(e) => console.log("Pointer down", e.x, e.y)}
onPointerUp={(e) => console.log("Pointer up")}
onPointerMove={(e) => console.log("Pointer move", e.x, e.y)}
onPointerEnter={(e) => console.log("Pointer entered")}
onPointerLeave={(e) => console.log("Pointer left")}
>
<Label text="Hover and click me" />
</View>Focus Events
<TextField
placeholder="Type here..."
onFocus={(e) => console.log("Focused")}
onBlur={(e) => console.log("Lost focus")}
/>Change Events
For input elements:
function TextInput() {
const [value, setValue] = useState("")
return (
<TextField
value={value}
onChange={(e) => setValue(e.value)}
placeholder="Enter text..."
/>
)
}function ToggleSwitch() {
const [enabled, setEnabled] = useState(false)
return (
<Toggle
value={enabled}
onChange={(e) => setEnabled(e.value)}
label="Enable feature"
/>
)
}function VolumeSlider() {
const [volume, setVolume] = useState(50)
return (
<Slider
value={volume}
lowValue={0}
highValue={100}
onChange={(e) => setVolume(e.value)}
/>
)
}Event Object Properties
Pointer Event
| Property | Type | Description |
|---|---|---|
type | string | Event type name |
x | number | Pointer X position |
y | number | Pointer Y position |
button | number | Mouse button (0=left, 1=middle, 2=right) |
pointerId | number | Unique pointer identifier |
modifiers | number | Keyboard modifiers (optional) |
Mouse Event
| Property | Type | Description |
|---|---|---|
type | string | Event type name |
x | number | Mouse X position |
y | number | Mouse Y position |
button | number | Mouse button |
modifiers | number | Keyboard modifiers (optional) |
Change Event
| Property | Type | Description |
|---|---|---|
type | string | Event type name |
value | T | The new value (type depends on control) |
Keyboard Events
Handle keyboard input on focused elements:
<TextField
onKeyDown={(e) => {
if (e.keyCode === CS.UnityEngine.KeyCode.Return) {
console.log("Enter pressed!")
}
}}
/>Event Propagation
Events bubble up through the component tree:
<View onClick={() => console.log("Parent clicked")}>
<Button
text="Click"
onClick={(e) => {
console.log("Button clicked")
// Event will also trigger parent's onClick
}}
/>
</View>To stop propagation:
<Button
onClick={(e) => {
e.stopPropagation()
console.log("Only this handler runs")
}}
/>Drag and Drop
Implement drag behavior:
function Draggable({ children }) {
const [pos, setPos] = useState({ x: 0, y: 0 })
const [dragging, setDragging] = useState(false)
const startPos = useRef({ x: 0, y: 0 })
return (
<View
style={{
position: "absolute",
left: pos.x,
top: pos.y,
cursor: dragging ? "grabbing" : "grab",
}}
onPointerDown={(e) => {
setDragging(true)
startPos.current = { x: e.x - pos.x, y: e.y - pos.y }
}}
onPointerMove={(e) => {
if (dragging) {
setPos({
x: e.x - startPos.current.x,
y: e.y - startPos.current.y,
})
}
}}
onPointerUp={() => setDragging(false)}
onPointerLeave={() => setDragging(false)}
>
{children}
</View>
)
}Global Input
For game input that isn't tied to UI elements, use Unity's Input system:
function useInput() {
const [keys, setKeys] = useState({ w: false, a: false, s: false, d: false })
useEffect(() => {
function checkInput() {
setKeys({
w: CS.UnityEngine.Input.GetKey(CS.UnityEngine.KeyCode.W),
a: CS.UnityEngine.Input.GetKey(CS.UnityEngine.KeyCode.A),
s: CS.UnityEngine.Input.GetKey(CS.UnityEngine.KeyCode.S),
d: CS.UnityEngine.Input.GetKey(CS.UnityEngine.KeyCode.D),
})
requestAnimationFrame(checkInput)
}
const id = requestAnimationFrame(checkInput)
return () => cancelAnimationFrame(id)
}, [])
return keys
}Mouse Events
For mouse-specific interactions (separate from pointer events):
<View
onMouseDown={(e) => console.log("Mouse down", e.button)}
onMouseUp={(e) => console.log("Mouse up")}
onMouseMove={(e) => console.log("Mouse move", e.x, e.y)}
onMouseEnter={(e) => console.log("Mouse entered")}
onMouseLeave={(e) => console.log("Mouse left")}
onMouseOver={(e) => console.log("Mouse over")}
onMouseOut={(e) => console.log("Mouse out")}
onWheel={(e) => console.log("Wheel delta", e.delta.y)}
onContextClick={(e) => console.log("Right-click menu")}
>
<Label text="Mouse interactions" />
</View>Geometry Events
Detect when an element's size or position changes:
<View
onGeometryChanged={(e) => {
console.log("Old rect:", e.oldRect)
console.log("New rect:", e.newRect)
}}
>
<Label text="Resizable content" />
</View>Transition Events
Monitor USS transition animations:
<View
className="animated"
onTransitionStart={(e) => console.log("Transition started:", e.styleProperty)}
onTransitionEnd={(e) => console.log("Transition ended:", e.styleProperty)}
onTransitionRun={(e) => console.log("Transition running")}
onTransitionCancel={(e) => console.log("Transition cancelled")}
>
<Label text="Animated element" />
</View>Navigation Events
Handle gamepad/keyboard UI navigation:
<View
onNavigationMove={(e) => console.log("Navigation direction:", e.direction)}
onNavigationSubmit={(e) => console.log("Navigation submit")}
onNavigationCancel={(e) => console.log("Navigation cancel")}
>
<Label text="Navigable element" />
</View>Tooltip Events
<View onTooltip={() => console.log("Tooltip triggered")}>
<Label text="Hover for tooltip" />
</View>Event Mapping Reference
Click & Pointer
| React Prop | UI Toolkit Event |
|---|---|
onClick | ClickEvent |
onPointerDown | PointerDownEvent |
onPointerUp | PointerUpEvent |
onPointerMove | PointerMoveEvent |
onPointerEnter | PointerEnterEvent |
onPointerLeave | PointerLeaveEvent |
onPointerCancel | PointerCancelEvent |
onPointerCapture | PointerCaptureEvent |
onPointerCaptureOut | PointerCaptureOutEvent |
onPointerStationary | PointerStationaryEvent |
Mouse
| React Prop | UI Toolkit Event |
|---|---|
onMouseDown | MouseDownEvent |
onMouseUp | MouseUpEvent |
onMouseMove | MouseMoveEvent |
onMouseEnter | MouseEnterEvent |
onMouseLeave | MouseLeaveEvent |
onMouseOver | MouseOverEvent |
onMouseOut | MouseOutEvent |
onWheel | WheelEvent |
onContextClick | ContextClickEvent |
Focus & Input
| React Prop | UI Toolkit Event |
|---|---|
onFocus | FocusEvent |
onBlur | BlurEvent |
onFocusIn | FocusInEvent |
onFocusOut | FocusOutEvent |
onChange | ChangeEvent<T> |
onInput | InputEvent |
onKeyDown | KeyDownEvent |
onKeyUp | KeyUpEvent |
Drag & Drop
| React Prop | UI Toolkit Event |
|---|---|
onDragEnter | DragEnterEvent |
onDragLeave | DragLeaveEvent |
onDragUpdated | DragUpdatedEvent |
onDragPerform | DragPerformEvent |
onDragExited | DragExitedEvent |
Layout & Transitions
| React Prop | UI Toolkit Event |
|---|---|
onGeometryChanged | GeometryChangedEvent |
onTransitionRun | TransitionRunEvent |
onTransitionStart | TransitionStartEvent |
onTransitionEnd | TransitionEndEvent |
onTransitionCancel | TransitionCancelEvent |
Navigation
| React Prop | UI Toolkit Event |
|---|---|
onNavigationMove | NavigationMoveEvent |
onNavigationSubmit | NavigationSubmitEvent |
onNavigationCancel | NavigationCancelEvent |
onTooltip | TooltipEvent |
Custom Rendering
For custom drawing with onGenerateVisualContent, see Unity's Painter2D documentation. This callback provides access to Unity's GPU-accelerated vector graphics API.