InputSystem Architecture
Design Patterns
- Singleton:
InputManagersets a staticInstanceinAwake, callsDontDestroyOnLoad, and destroys any duplicate. Other code reads the manager throughInputManager.Instance. - Event-based notification (observer style):
InputManagerexposesAction<Vector3>delegates such asClicked,Pressed, andReleased.ClickDetectorexposes anAction<Vector3>event, andDoubleClickDetectorexposes anActionevent. Subscribers attach with+=and detach with-=. - Composition:
InputManagercreates and owns aClickDetectorand aDoubleClickDetector.ClickDetectorreceives theInputManagerin its constructor and subscribes to itsPressedandReleaseddelegates. - MonoBehaviour lifecycle:
InputManagerandInputHandleruseAwake,Start,Update, andOnDisable. - Compile-time platform selection:
InputManager.UpdatecallsHandleTouchwhenUNITY_ANDROIDis defined andHandleMouseotherwise.
Connections to Other Systems
- UI system (UIEventFactory):
InputHandlercalls the UI event factory when a click or double-click occurs. The factory raises the matching UI events on the EventBus, which is how other systems receive them. This system does not subscribe to the EventBus itself. - World and UI objects (IInteractable): When a press hits a UI object,
InputManagerfinds everyIInteractableon that object and its parents and callsOnInteractionon each one. - EventBus: The UI event factory publishes to the EventBus. Listeners on the EventBus do not need a direct reference to this system.
Key Concepts
- Press and release: A press is the start of a pointer interaction (mouse button down or touch began). A release is the end (mouse button up or touch ended).
- Hold: Reported every frame while the pointer stays down, as
Held. - Drag: Starts when the pointer moves more than
dragThresholdpixels from the press position while held. Ends on release if a drag was in progress. - Click: A press and release where the elapsed time is at or below
InputConstants.ClickThreshold(0.2 seconds) and no drag occurred. - Double-click: Two presses within the window set by
doubleClickTime(0.3 seconds by default inInputManager). Presses are counted, not clicks. - Hit resolution: The raycast uses the EventSystem's
RaycastAlland takes the first result, which the code describes as the closest object to the camera.
Event and Data Flow
Each frame, InputManager runs one of two paths. The order of events within a single release is fixed by the code.
Press (mouse down or touch began)
InputManager.Pressed
Raycast via EventSystem, call IInteractable.OnInteraction on hit
DoubleClickDetector.RegisterClick(Time.time)
-> OnDoubleClick -> InputManager.DoubleClicked
Hold (mouse held or touch moved/stationary)
InputManager.Held
If distance from press > dragThreshold and not yet dragging:
InputManager.DragStarted
DoubleClickDetector.Reset()
ClickDetector.Reset()
Release (mouse up or touch ended)
InputManager.Released
ClickDetector.OnReleased
If elapsed <= ClickThreshold -> ClickDetector.OnClick -> InputManager.Clicked
If dragging:
InputManager.DragEndedInputHandler subscribes to Clicked and DoubleClicked and forwards each one to the UI event factory.
Thread Safety
The code shows no threading. All work happens on the Unity main thread in Update and in event callbacks.