@@ -18,14 +18,18 @@ internal class InputSystemProvider : IEventProviderImpl
18
18
DefaultInputActions m_DefaultInputActions ;
19
19
InputActionAsset m_InputActionAsset ;
20
20
21
- InputActionReference m_PointAction ;
22
- InputActionReference m_MoveAction ;
23
- InputActionReference m_SubmitAction ;
24
- InputActionReference m_CancelAction ;
25
- InputActionReference m_LeftClickAction ;
26
- InputActionReference m_MiddleClickAction ;
27
- InputActionReference m_RightClickAction ;
28
- InputActionReference m_ScrollWheelAction ;
21
+ // Note that these are plain action references instead since InputActionReference do
22
+ // not provide any value when this integration doesn't have any UI. If this integration
23
+ // later gets a UI replace these by InputActionReference and remember to check for e.g.
24
+ // m_ActionReference != null && m_ActionReference.action != null.
25
+ InputAction m_PointAction ;
26
+ InputAction m_MoveAction ;
27
+ InputAction m_SubmitAction ;
28
+ InputAction m_CancelAction ;
29
+ InputAction m_LeftClickAction ;
30
+ InputAction m_MiddleClickAction ;
31
+ InputAction m_RightClickAction ;
32
+ InputAction m_ScrollWheelAction ;
29
33
30
34
InputAction m_NextPreviousAction ;
31
35
@@ -221,7 +225,7 @@ InputDevice GetActiveDeviceFromDirection(NavigationEvent.Direction direction)
221
225
case NavigationEvent . Direction . Right :
222
226
case NavigationEvent . Direction . Down :
223
227
if ( m_MoveAction != null )
224
- return m_MoveAction . action . activeControl . device ;
228
+ return m_MoveAction . activeControl . device ;
225
229
break ;
226
230
case NavigationEvent . Direction . Next :
227
231
case NavigationEvent . Direction . Previous :
@@ -242,9 +246,9 @@ InputDevice GetActiveDeviceFromDirection(NavigationEvent.Direction direction)
242
246
if ( m_MoveAction == null )
243
247
return ( default , default ) ;
244
248
245
- var move = m_MoveAction . action . ReadValue < Vector2 > ( ) ;
249
+ var move = m_MoveAction . ReadValue < Vector2 > ( ) ;
246
250
// Check if the action was "pressed" this frame to deal with repeating events
247
- var axisWasPressed = m_MoveAction . action . WasPressedThisFrame ( ) ;
251
+ var axisWasPressed = m_MoveAction . WasPressedThisFrame ( ) ;
248
252
return ( move , axisWasPressed ) ;
249
253
}
250
254
@@ -610,43 +614,29 @@ void UnregisterFixedActions()
610
614
}
611
615
}
612
616
617
+ InputAction FindActionAndRegisterCallback ( string actionNameOrId , Action < InputAction . CallbackContext > callback = null )
618
+ {
619
+ var action = m_InputActionAsset . FindAction ( actionNameOrId ) ;
620
+ if ( action != null && callback != null )
621
+ action . performed += callback ;
622
+ return action ;
623
+ }
624
+
613
625
void RegisterActions ( )
614
626
{
615
627
// Invoke potential lister observing registration
616
628
s_OnRegisterActions ? . Invoke ( m_InputActionAsset ) ;
617
629
618
- m_PointAction = InputActionReference . Create ( m_InputActionAsset . FindAction ( Actions . PointAction ) ) ;
619
- m_MoveAction = InputActionReference . Create ( m_InputActionAsset . FindAction ( Actions . MoveAction ) ) ;
620
- m_SubmitAction = InputActionReference . Create ( m_InputActionAsset . FindAction ( Actions . SubmitAction ) ) ;
621
- m_CancelAction = InputActionReference . Create ( m_InputActionAsset . FindAction ( Actions . CancelAction ) ) ;
622
- m_LeftClickAction = InputActionReference . Create ( m_InputActionAsset . FindAction ( Actions . LeftClickAction ) ) ;
623
- m_MiddleClickAction = InputActionReference . Create ( m_InputActionAsset . FindAction ( Actions . MiddleClickAction ) ) ;
624
- m_RightClickAction = InputActionReference . Create ( m_InputActionAsset . FindAction ( Actions . RightClickAction ) ) ;
625
- m_ScrollWheelAction = InputActionReference . Create ( m_InputActionAsset . FindAction ( Actions . ScrollWheelAction ) ) ;
626
-
627
- if ( m_PointAction != null && m_PointAction . action != null )
628
- m_PointAction . action . performed += OnPointerPerformed ;
629
-
630
- if ( m_SubmitAction != null && m_SubmitAction . action != null )
631
- m_SubmitAction . action . performed += OnSubmitPerformed ;
632
-
633
- if ( m_CancelAction != null && m_CancelAction . action != null )
634
- m_CancelAction . action . performed += OnCancelPerformed ;
635
-
636
- if ( m_LeftClickAction != null && m_LeftClickAction . action != null )
637
- m_LeftClickAction . action . performed += OnLeftClickPerformed ;
638
-
639
- if ( m_MiddleClickAction != null && m_MiddleClickAction . action != null )
640
- m_MiddleClickAction . action . performed += OnMiddleClickPerformed ;
641
-
642
- if ( m_RightClickAction != null && m_RightClickAction . action != null )
643
- m_RightClickAction . action . performed += OnRightClickPerformed ;
644
-
645
- if ( m_ScrollWheelAction != null && m_ScrollWheelAction . action != null )
646
- m_ScrollWheelAction . action . performed += OnScrollWheelPerformed ;
630
+ m_PointAction = FindActionAndRegisterCallback ( Actions . PointAction , OnPointerPerformed ) ;
631
+ m_MoveAction = FindActionAndRegisterCallback ( Actions . MoveAction ) ; // No callback for this action
632
+ m_SubmitAction = FindActionAndRegisterCallback ( Actions . SubmitAction , OnSubmitPerformed ) ;
633
+ m_CancelAction = FindActionAndRegisterCallback ( Actions . CancelAction , OnCancelPerformed ) ;
634
+ m_LeftClickAction = FindActionAndRegisterCallback ( Actions . LeftClickAction , OnLeftClickPerformed ) ;
635
+ m_MiddleClickAction = FindActionAndRegisterCallback ( Actions . MiddleClickAction , OnMiddleClickPerformed ) ;
636
+ m_RightClickAction = FindActionAndRegisterCallback ( Actions . RightClickAction , OnRightClickPerformed ) ;
637
+ m_ScrollWheelAction = FindActionAndRegisterCallback ( Actions . ScrollWheelAction , OnScrollWheelPerformed ) ;
647
638
648
639
// When adding new actions, don't forget to add them to UnregisterActions
649
-
650
640
if ( InputSystem . actions == null )
651
641
{
652
642
// If we've not loaded a user-created set of actions, just enable the UI actions from our defaults.
@@ -656,37 +646,23 @@ void RegisterActions()
656
646
m_InputActionAsset . Enable ( ) ;
657
647
}
658
648
659
- void UnregisterActions ( )
649
+ void UnregisterAction ( ref InputAction action , Action < InputAction . CallbackContext > callback = null )
660
650
{
661
- if ( m_PointAction != null && m_PointAction . action != null )
662
- m_PointAction . action . performed -= OnPointerPerformed ;
663
-
664
- if ( m_SubmitAction != null && m_SubmitAction . action != null )
665
- m_SubmitAction . action . performed -= OnSubmitPerformed ;
666
-
667
- if ( m_CancelAction != null && m_CancelAction . action != null )
668
- m_CancelAction . action . performed -= OnCancelPerformed ;
669
-
670
- if ( m_LeftClickAction != null && m_LeftClickAction . action != null )
671
- m_LeftClickAction . action . performed -= OnLeftClickPerformed ;
672
-
673
- if ( m_MiddleClickAction != null && m_MiddleClickAction . action != null )
674
- m_MiddleClickAction . action . performed -= OnMiddleClickPerformed ;
675
-
676
- if ( m_RightClickAction != null && m_RightClickAction . action != null )
677
- m_RightClickAction . action . performed -= OnRightClickPerformed ;
678
-
679
- if ( m_ScrollWheelAction != null && m_ScrollWheelAction . action != null )
680
- m_ScrollWheelAction . action . performed -= OnScrollWheelPerformed ;
651
+ if ( action != null && callback != null )
652
+ action . performed -= callback ;
653
+ action = null ;
654
+ }
681
655
682
- m_PointAction = null ;
683
- m_MoveAction = null ;
684
- m_SubmitAction = null ;
685
- m_CancelAction = null ;
686
- m_LeftClickAction = null ;
687
- m_MiddleClickAction = null ;
688
- m_RightClickAction = null ;
689
- m_ScrollWheelAction = null ;
656
+ void UnregisterActions ( )
657
+ {
658
+ UnregisterAction ( ref m_PointAction , OnPointerPerformed ) ;
659
+ UnregisterAction ( ref m_MoveAction ) ; // No callback for this action
660
+ UnregisterAction ( ref m_SubmitAction , OnSubmitPerformed ) ;
661
+ UnregisterAction ( ref m_CancelAction , OnCancelPerformed ) ;
662
+ UnregisterAction ( ref m_LeftClickAction , OnLeftClickPerformed ) ;
663
+ UnregisterAction ( ref m_MiddleClickAction , OnMiddleClickPerformed ) ;
664
+ UnregisterAction ( ref m_RightClickAction , OnRightClickPerformed ) ;
665
+ UnregisterAction ( ref m_ScrollWheelAction , OnScrollWheelPerformed ) ;
690
666
691
667
if ( m_InputActionAsset != null )
692
668
m_InputActionAsset . Disable ( ) ;
0 commit comments