-
Notifications
You must be signed in to change notification settings - Fork 324
/
Copy pathPlayerInput.cs
2025 lines (1812 loc) · 88.9 KB
/
PlayerInput.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using UnityEngine.Events;
using UnityEngine.InputSystem.LowLevel;
using UnityEngine.InputSystem.Users;
using UnityEngine.InputSystem.Utilities;
#if UNITY_EDITOR
using UnityEngine.InputSystem.Editor;
#endif
#if PACKAGE_DOCS_GENERATION || UNITY_INPUT_SYSTEM_ENABLE_UI
using UnityEngine.InputSystem.UI;
#endif
////TODO: add support for keeping a player's InputUser alive and reconnecting back to it
////TODO: when joining is *off*, allow auto-switching even in multiplayer
////TODO: differentiate not only by already paired devices but rather take control schemes into account; allow two players to be on the same
//// device as long as they are using different control schemes
////TODO: allow PlayerInput to be set up in a way where it's in an unpaired/non-functional state and expects additional configuration
////REVIEW: callback behaviors have been very confusing for users; simplify&clarify this
////REVIEW: having everything coupled to component enable/disable is quite restrictive; can we allow PlayerInputs
//// to be disabled without them leaving the game? would help when wanting to keep players around in the background
//// and only temporarily disable them
////TODO: add support for "continuous" callbacks
////TODO: add event for control scheme switches
////TODO: add ability to name players
////TODO: refresh caches when asset is modified at runtime
////TODO: handle required actions ahead of time so that we catch it if a device matches by type but doesn't otherwise
////TODO: handle case of control scheme not having any devices in its requirements
////TODO: add method to pass an object implementing a generated action interface (IXXXActions) and have it hooked up automatically
//// (or maybe look for implementation on components in same object?)
////TODO: warn if control schemes have no device requirements
////FIXME: why can't I join with a mouse left click?
namespace UnityEngine.InputSystem
{
/// <summary>
/// Represents a separate player in the game complete with a set of actions exclusive
/// to the player and a set of paired device.
/// </summary>
/// <remarks>
/// PlayerInput is a high-level wrapper around much of the input system's functionality
/// which is meant to help getting set up with the new input system quickly. It takes
/// care of <see cref="InputAction"/> bookkeeping and has a custom UI(requires the "Unity UI" package) to help
/// setting up input.
///
/// The component supports local multiplayer implicitly. Each PlayerInput instance
/// represents a distinct user with its own set of devices and actions. To orchestrate
/// player management and facilitate mechanics such as joining by device activity, use
/// <see cref="UnityEngine.InputSystem.PlayerInputManager"/>.
///
/// The way PlayerInput notifies script code of events is determined by <see cref="notificationBehavior"/>.
/// By default, this is set to <see cref="UnityEngine.InputSystem.PlayerNotifications.SendMessages"/> which will use
/// <see cref="GameObject.SendMessage(string,object)"/> to send messages to the <see cref="GameObject"/>
/// that PlayerInput sits on.
///
/// <example>
/// <code>
/// // Component to sit next to PlayerInput.
/// [RequireComponent(typeof(PlayerInput))]
/// public class MyPlayerLogic : MonoBehaviour
/// {
/// public GameObject projectilePrefab;
///
/// private Vector2 m_Look;
/// private Vector2 m_Move;
/// private bool m_Fire;
///
/// // 'Fire' input action has been triggered. For 'Fire' we want continuous
/// // action (that is, firing) while the fire button is held such that the action
/// // gets triggered repeatedly while the button is down. We can easily set this
/// // up by having a "Press" interaction on the button and setting it to repeat
/// // at fixed intervals.
/// public void OnFire()
/// {
/// Instantiate(projectilePrefab);
/// }
///
/// // 'Move' input action has been triggered.
/// public void OnMove(InputValue value)
/// {
/// m_Move = value.Get<Vector2>();
/// }
///
/// // 'Look' input action has been triggered.
/// public void OnLook(InputValue value)
/// {
/// m_Look = value.Get<Vector2>();
/// }
///
/// public void OnUpdate()
/// {
/// // Update transform from m_Move and m_Look
/// }
/// }
/// </code>
/// </example>
///
/// It is also possible to use the polling API of <see cref="InputAction"/>s (see
/// <see cref="InputAction.triggered"/> and <see cref="InputAction.ReadValue{TValue}"/>)
/// in combination with PlayerInput.
///
/// <example>
/// <code>
/// // Component to sit next to PlayerInput.
/// [RequireComponent(typeof(PlayerInput))]
/// public class MyPlayerLogic : MonoBehaviour
/// {
/// public GameObject projectilePrefab;
///
/// private PlayerInput m_PlayerInput;
/// private InputAction m_LookAction;
/// private InputAction m_MoveAction;
/// private InputAction m_FireAction;
///
/// public void OnUpdate()
/// {
/// // First update we look up all the data we need.
/// // NOTE: We don't do this in OnEnable as PlayerInput itself performing some
/// // initialization work in OnEnable.
/// if (m_PlayerInput == null)
/// {
/// m_PlayerInput = GetComponent<PlayerInput>();
/// m_FireAction = m_PlayerInput.actions["fire"];
/// m_LookAction = m_PlayerInput.actions["look"];
/// m_MoveAction = m_PlayerInput.actions["move"];
/// }
///
/// if (m_FireAction.triggered)
/// /* firing logic... */;
///
/// var move = m_MoveAction.ReadValue<Vector2>();
/// var look = m_LookAction.ReadValue<Vector2>();
/// /* Update transform from move&look... */
/// }
/// }
/// </code>
/// </example>
///
/// When enabled, PlayerInput will create an <see cref="InputUser"/> and pair devices to the
/// user which are then specific to the player. The set of devices can be controlled explicitly
/// when instantiating a PlayerInput through <see cref="Instantiate(GameObject,int,string,int,InputDevice[])"/>
/// or <see cref="Instantiate(GameObject,int,string,int,InputDevice)"/>. This also makes it possible
/// to assign the same device to two different players, e.g. for split-keyboard play.
///
/// <example>
/// <code>
/// var p1 = PlayerInput.Instantiate(playerPrefab,
/// controlScheme: "KeyboardLeft", device: Keyboard.current);
/// var p2 = PlayerInput.Instantiate(playerPrefab,
/// controlScheme: "KeyboardRight", device: Keyboard.current);
/// </code>
/// </example>
///
/// If no specific devices are given to a PlayerInput, the component will look for compatible
/// devices present in the system and pair them to itself automatically. If the PlayerInput's
/// <see cref="actions"/> have control schemes defined for them, PlayerInput will look for a
/// control scheme for which all required devices are available and not paired to any other player.
/// It will try <see cref="defaultControlScheme"/> first (if set), but then fall back to trying
/// all available schemes in order. Once a scheme is found for which all required devices are
/// available, PlayerInput will pair those devices to itself and select the given scheme.
///
/// If no control schemes are defined, PlayerInput will try to bind as many as-of-yet unpaired
/// devices to itself as it can match to bindings present in the <see cref="actions"/>. This means
/// that if, for example, there's binding for both keyboard and gamepad and there is one keyboard
/// and two gamepads available when PlayerInput is enabled, all three devices will be paired to
/// the player.
///
/// Note that when using <see cref="PlayerInputManager"/>, device pairing to players is controlled
/// from the joining logic. In that case, PlayerInput will automatically pair the device from which
/// the player joined. If control schemes are present in <see cref="actions"/>, the first one compatible
/// with that device is chosen. If additional devices are required, these will be paired from the pool
/// of currently unpaired devices.
///
/// Device pairings can be changed at any time by either manually controlling pairing through
/// <see cref="InputUser.PerformPairingWithDevice"/> (and related methods) using a PlayerInput's
/// assigned <see cref="user"/> or by switching control schemes (e.g. using
/// <see cref="SwitchCurrentControlScheme(string,InputDevice[])"/>), if any are present in the PlayerInput's
/// <see cref="actions"/>.
///
/// When a player loses a device paired to it (e.g. when it is unplugged or loses power), <see cref="InputUser"/>
/// will signal <see cref="InputUserChange.DeviceLost"/> which is also surfaced as a message,
/// <see cref="deviceLostEvent"/>, or <see cref="onDeviceLost"/> (depending on <see cref="notificationBehavior"/>).
/// When a device is reconnected, <see cref="InputUser"/> will signal <see cref="InputUserChange.DeviceRegained"/>
/// which also is surfaced as a message, as <see cref="deviceRegainedEvent"/>, or <see cref="onDeviceRegained"/>
/// (depending on <see cref="notificationBehavior"/>).
///
/// When there is only a single active PlayerInput in the game, joining is not enabled (see
/// <see cref="PlayerInputManager.joiningEnabled"/>), and <see cref="neverAutoSwitchControlSchemes"/> is not
/// set to <c>true</c>, device pairings for the player will also update automatically based on device usage.
///
/// If control schemes are present in <see cref="actions"/>, then if a device is used (not merely plugged in
/// but rather receives input on a non-noisy, non-synthetic control) which is compatible with a control scheme
/// other than the currently used one, PlayerInput will attempt to switch to that control scheme. Success depends
/// on whether all device requirements for that scheme are met from the set of available devices. If a control
/// scheme happens, <see cref="InputUser"/> signals <see cref="InputUserChange.ControlSchemeChanged"/> on
/// <see cref="InputUser.onChange"/>.
///
/// If no control schemes are present in <see cref="actions"/>, PlayerInput will automatically pair any newly
/// available device to itself if the given device has any bindings available for it.
///
/// Both behaviors described in the previous two paragraphs are automatically disabled if more than one
/// PlayerInput is active.
/// </remarks>
/// <seealso cref="UnityEngine.InputSystem.PlayerInputManager"/>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1724:TypeNamesShouldNotMatchNamespaces")]
[AddComponentMenu("Input/Player Input")]
[DisallowMultipleComponent]
[HelpURL(InputSystem.kDocUrl + "/manual/PlayerInput.html")]
public class PlayerInput : MonoBehaviour
{
/// <summary>
/// Name of the message that is sent with <c>UnityEngine.Object.SendMessage</c> when a
/// player loses a device.
/// </summary>
/// <seealso cref="onDeviceLost"/>
public const string DeviceLostMessage = "OnDeviceLost";
/// <summary>
/// Name of the message that is sent with <c>UnityEngine.Object.SendMessage</c> when a
/// player regains a device.
/// </summary>
/// <seealso cref="onDeviceRegained"/>
public const string DeviceRegainedMessage = "OnDeviceRegained";
/// <summary>
/// Name of the message that is sent with <c>UnityEngine.Object.SendMessage</c> when the
/// controls used by a player are changed.
/// </summary>
/// <seealso cref="onControlsChanged"/>
public const string ControlsChangedMessage = "OnControlsChanged";
/// <summary>
/// Whether input is on the player is active.
/// </summary>
/// <value>If true, the player is receiving input.</value>
/// <seealso cref="ActivateInput"/>
/// <seealso cref="DeactivateInput"/>
public bool inputIsActive => m_InputActive;
[Obsolete("Use inputIsActive instead.")]
public bool active => inputIsActive;
/// <summary>
/// Unique, zero-based index of the player. For example, <c>2</c> for the third player.
/// </summary>
/// <value>Unique index of the player.</value>
/// <remarks>
/// Once assigned, a player index will not change.
///
/// Note that the player index does not necessarily correspond to the player's index in <see cref="all"/>.
/// The array will always contain all currently enabled players so when a player is disabled or destroyed,
/// it will be removed from the array. However, the player index of the remaining players will not change.
/// </remarks>
public int playerIndex => m_PlayerIndex;
/// <summary>
/// If split-screen is enabled (<see cref="UnityEngine.InputSystem.PlayerInputManager.splitScreen"/>),
/// this is the index of the screen area used by the player.
/// </summary>
/// <value>Index of split-screen area assigned to player or -1 if the player is not
/// using split-screen.</value>
/// <remarks>
/// Split screen areas are enumerated row by row and within rows, column by column. So, if, for example,
/// there are four separate split-screen areas, the upper left one is #0, the upper right one is #1,
/// the lower left one is #2, and the lower right one is #3.
///
/// Split screen areas are usually assigned automatically but players can also be assigned to
/// areas explicitly through <see cref="Instantiate(GameObject,int,string,int,InputDevice)"/> or
/// <see cref="PlayerInputManager.JoinPlayer(int,int,string,InputDevice)"/>.
/// </remarks>
/// <seealso cref="camera"/>
/// <seealso cref="PlayerInputManager.splitScreen"/>
public int splitScreenIndex => m_SplitScreenIndex;
/// <summary>
/// Input actions associated with the player.
/// </summary>
/// <value>Asset holding the player's input actions.</value>
/// <remarks>
/// Note that every player will maintain a unique copy of the given actions such that
/// each player receives an identical copy. When assigning the same actions to multiple players,
/// the first player will use the given actions as is but any subsequent player will make a copy
/// of the actions using <see cref="Object.Instantiate(Object)"/>.
///
/// The asset may contain an arbitrary number of action maps. By setting <see cref="defaultActionMap"/>,
/// one of them can be selected to enabled automatically when PlayerInput is enabled. If no default
/// action map is selected, none of the action maps will be enabled by PlayerInput itself. Use
/// <see cref="SwitchCurrentActionMap"/> or just call <see cref="InputActionMap.Enable"/> directly
/// to enable a specific map.
///
/// Notifications will be sent for all actions in the asset, not just for those in the first action
/// map. This means that if additional maps are manually enabled and disabled, notifications will
/// be sent for their actions as they receive input.
/// </remarks>
/// <seealso cref="InputUser.actions"/>
/// <seealso cref="SwitchCurrentActionMap"/>
public InputActionAsset actions
{
get
{
if (!m_ActionsInitialized && gameObject.activeInHierarchy)
InitializeActions();
return m_Actions;
}
set
{
if (m_Actions == value)
return;
// Make sure that if we already have actions, they get disabled.
if (m_Actions != null)
{
m_Actions.Disable();
if (m_ActionsInitialized)
UninitializeActions();
}
m_Actions = value;
if (m_Enabled)
{
ClearCaches();
AssignUserAndDevices();
InitializeActions();
if (m_InputActive)
ActivateInput();
}
}
}
/// <summary>
/// Name of the currently active control scheme.
/// </summary>
/// <value>Name of the currently active control scheme or <c>null</c>.</value>
/// <remarks>
/// Note that this property will be <c>null</c> if there are no control schemes
/// defined in <see cref="actions"/>.
/// </remarks>
/// <seealso cref="SwitchCurrentControlScheme(UnityEngine.InputSystem.InputDevice[])"/>
/// <seealso cref="defaultControlScheme"/>
/// <seealso cref="InputActionAsset.controlSchemes"/>
public string currentControlScheme
{
get
{
if (!m_InputUser.valid)
return null;
var scheme = m_InputUser.controlScheme;
return scheme?.name;
}
}
/// <summary>
/// The default control scheme to try.
/// </summary>
/// <value>Name of the default control scheme.</value>
/// <remarks>
/// When PlayerInput is enabled and this is not <c>null</c> and not empty, the PlayerInput
/// will look up the control scheme in <see cref="InputActionAsset.controlSchemes"/> of
/// <see cref="actions"/>. If found, PlayerInput will try to activate the scheme. This will
/// succeed only if all devices required by the control scheme are either already paired to
/// the player or are available as devices not used by other PlayerInputs.
///
/// Note that this property only determines the first control scheme to try. If using the
/// control scheme fails, PlayerInput will fall back to trying the other control schemes
/// (if any) available from <see cref="actions"/>.
/// </remarks>
/// <seealso cref="SwitchCurrentControlScheme(InputDevice[])"/>
/// <seealso cref="currentControlScheme"/>
public string defaultControlScheme
{
get => m_DefaultControlScheme;
set => m_DefaultControlScheme = value;
}
/// <summary>
/// If true, do not automatically switch control schemes even when there is only a single player.
/// By default, this property is false.
/// </summary>
/// <value>If true, do not switch control schemes when other devices are used.</value>
/// <remarks>
/// By default, when there is only a single PlayerInput enabled, we assume that the game is in
/// single-player mode and that the player should be able to freely switch between the control schemes
/// supported by the game. For example, if the player is currently using mouse and keyboard, but is
/// then switching to a gamepad, PlayerInput should automatically switch to the control scheme for
/// gamepads, if present.
///
/// When there is more than one PlayerInput or when joining is enabled <see cref="PlayerInputManager"/>,
/// this behavior is automatically turned off as we wouldn't know which player is switching if a
/// currently unpaired device is used.
///
/// By setting this property to true, auto-switching of control schemes is forcibly turned off and
/// will thus not be performed even if there is only a single PlayerInput in the game.
///
/// Note that you can still switch control schemes manually using <see
/// cref="SwitchCurrentControlScheme(string,InputDevice[])"/>.
/// </remarks>
/// <seealso cref="currentControlScheme"/>
/// <seealso cref="isSinglePlayer"/>
public bool neverAutoSwitchControlSchemes
{
get => m_NeverAutoSwitchControlSchemes;
set
{
if (m_NeverAutoSwitchControlSchemes == value)
return;
m_NeverAutoSwitchControlSchemes = value;
if (m_Enabled)
{
if (!value && !m_OnUnpairedDeviceUsedHooked)
StartListeningForUnpairedDeviceActivity();
else if (value && m_OnUnpairedDeviceUsedHooked)
StopListeningForUnpairedDeviceActivity();
}
}
}
////REVIEW: this is inconsistent; currentControlScheme is a string, this is an InputActionMap
/// <summary>
/// The currently enabled action map.
/// </summary>
/// <value>Reference to the currently enabled action or <c>null</c> if no action
/// map has been enabled by PlayerInput.</value>
/// <remarks>
/// Note that the concept of "current action map" is local to PlayerInput. You can still freely
/// enable and disable action maps directly on the <see cref="actions"/> asset. This property
/// only tracks which action map has been enabled under the control of PlayerInput, i.e. either
/// by means of <see cref="defaultActionMap"/> or by using <see cref="SwitchCurrentActionMap"/>.
/// </remarks>
/// <seealso cref="SwitchCurrentActionMap"/>
public InputActionMap currentActionMap
{
get => m_CurrentActionMap;
set
{
// If someone switches maps from an action callback, we may get here recursively
// from Disable(). To avoid that, we null out the current action map while
// we disable it.
var oldMap = m_CurrentActionMap;
m_CurrentActionMap = null;
oldMap?.Disable();
// Switch to new map.
m_CurrentActionMap = value;
m_CurrentActionMap?.Enable();
}
}
/// <summary>
/// Name (see <see cref="InputActionMap.name"/>) or ID (see <see cref="InputActionMap.id"/>) of the action
/// map to enable by default.
/// </summary>
/// <value>Action map to enable by default or <c>null</c>.</value>
/// <remarks>
/// By default, when enabled, PlayerInput will not enable any of the actions in the <see cref="actions"/>
/// asset. By setting this property, however, PlayerInput can be made to automatically enable the respective
/// action map.
/// </remarks>
/// <seealso cref="currentActionMap"/>
/// <seealso cref="SwitchCurrentActionMap"/>
public string defaultActionMap
{
get => m_DefaultActionMap;
set => m_DefaultActionMap = value;
}
/// <summary>
/// Determines how the component notifies listeners about input actions and other input-related
/// events pertaining to the player.
/// </summary>
/// <value>How to trigger notifications on events.</value>
/// <remarks>
/// By default, the component will use <see cref="GameObject.SendMessage(string,object)"/> to send messages
/// to the <see cref="GameObject"/>. This can be changed by selecting a different <see cref="UnityEngine.InputSystem.PlayerNotifications"/>
/// behavior.
/// </remarks>
/// <seealso cref="actionEvents"/>
/// <seealso cref="deviceLostEvent"/>
/// <seealso cref="deviceRegainedEvent"/>
public PlayerNotifications notificationBehavior
{
get => m_NotificationBehavior;
set
{
if (m_NotificationBehavior == value)
return;
if (m_Enabled)
UninitializeActions();
m_NotificationBehavior = value;
if (m_Enabled)
InitializeActions();
}
}
/// <summary>
/// List of events invoked in response to actions being triggered.
/// </summary>
/// <remarks>
/// This array is only used if <see cref="notificationBehavior"/> is set to
/// <see cref="UnityEngine.InputSystem.PlayerNotifications.InvokeUnityEvents"/>.
/// </remarks>
public ReadOnlyArray<ActionEvent> actionEvents
{
get => m_ActionEvents;
set
{
if (m_Enabled)
UninitializeActions();
m_ActionEvents = value.ToArray();
if (m_Enabled)
InitializeActions();
}
}
/// <summary>
/// Event that is triggered when the player loses a device (e.g. the batteries run out).
/// </summary>
/// <remarks>
/// This event is only used if <see cref="notificationBehavior"/> is set to
/// <see cref="UnityEngine.InputSystem.PlayerNotifications.InvokeUnityEvents"/>.
/// </remarks>
public DeviceLostEvent deviceLostEvent
{
get
{
if (m_DeviceLostEvent == null)
m_DeviceLostEvent = new DeviceLostEvent();
return m_DeviceLostEvent;
}
}
/// <summary>
/// Event that is triggered when the player recovers from device loss and is good to go again.
/// </summary>
/// <remarks>
/// This event is only used if <see cref="notificationBehavior"/> is set to
/// <see cref="UnityEngine.InputSystem.PlayerNotifications.InvokeUnityEvents"/>.
/// </remarks>
public DeviceRegainedEvent deviceRegainedEvent
{
get
{
if (m_DeviceRegainedEvent == null)
m_DeviceRegainedEvent = new DeviceRegainedEvent();
return m_DeviceRegainedEvent;
}
}
/// <summary>
/// Event that is triggered when the controls used by the player change.
/// </summary>
/// <remarks>
/// This event is only used if <see cref="notificationBehavior"/> is set to
/// <see cref="UnityEngine.InputSystem.PlayerNotifications.InvokeUnityEvents"/>.
///
/// The event is trigger when the set of <see cref="devices"/> used by the player change,
/// when the player switches to a different control scheme (see <see cref="currentControlScheme"/>),
/// or when the bindings used by the player are changed (e.g. when rebinding them). Also,
/// for <see cref="Keyboard"/> devices, the event is triggered when the currently used
/// keyboard layout (see <see cref="Keyboard.keyboardLayout"/>) changes.
/// </remarks>
public ControlsChangedEvent controlsChangedEvent
{
get
{
if (m_ControlsChangedEvent == null)
m_ControlsChangedEvent = new ControlsChangedEvent();
return m_ControlsChangedEvent;
}
}
/// <summary>
/// If <see cref="notificationBehavior"/> is set to <see cref="PlayerNotifications.InvokeCSharpEvents"/>, this
/// event is triggered when an action fires.
/// </summary>
/// <value>Callbacks that get called when an action triggers.</value>
/// <remarks>
/// If <see cref="notificationBehavior"/> is not set to <see cref="PlayerNotifications.InvokeCSharpEvents"/>, the
/// value of this property is ignored.
///
/// The callbacks are called in sync (and with the same argument) with <see cref="InputAction.started"/>,
/// <see cref="InputAction.performed"/>, and <see cref="InputAction.canceled"/>.
/// </remarks>
/// <seealso cref="InputActionMap.actionTriggered"/>
/// <seealso cref="InputAction.started"/>
/// <seealso cref="InputAction.performed"/>
/// <seealso cref="InputAction.canceled"/>
/// <seealso cref="actions"/>
public event Action<InputAction.CallbackContext> onActionTriggered
{
add
{
if (value == null)
throw new ArgumentNullException(nameof(value));
m_ActionTriggeredCallbacks.AddCallback(value);
}
remove
{
if (value == null)
throw new ArgumentNullException(nameof(value));
m_ActionTriggeredCallbacks.RemoveCallback(value);
}
}
/// <summary>
/// If <see cref="notificationBehavior"/> is <see cref="PlayerNotifications.InvokeCSharpEvents"/>, this event
/// is triggered when a device paired to the player is disconnected.
/// </summary>
/// <value>Callbacks that get called when the player loses a device.</value>
/// <remarks>
/// If <see cref="notificationBehavior"/> is not <see cref="PlayerNotifications.InvokeCSharpEvents"/>, the value
/// of this property is ignored.
///
/// The argument is the player that lost its device (i.e. the player on which the callback is installed).
/// </remarks>
/// <seealso cref="onDeviceRegained"/>
/// <seealso cref="InputUserChange.DeviceLost"/>
public event Action<PlayerInput> onDeviceLost
{
add
{
if (value == null)
throw new ArgumentNullException(nameof(value));
m_DeviceLostCallbacks.AddCallback(value);
}
remove
{
if (value == null)
throw new ArgumentNullException(nameof(value));
m_DeviceLostCallbacks.RemoveCallback(value);
}
}
/// <summary>
/// If <see cref="notificationBehavior"/> is <see cref="PlayerNotifications.InvokeCSharpEvents"/>, this event
/// is triggered when the player previously lost a device and has now regained it or an equivalent device.
/// </summary>
/// <value>Callbacks that get called when the player regains a device.</value>
/// <remarks>
/// If <see cref="notificationBehavior"/> is not <see cref="PlayerNotifications.InvokeCSharpEvents"/>, the value
/// of this property is ignored.
///
/// The argument is the player that regained a device (i.e. the player on which the callback is installed).
/// </remarks>
/// <seealso cref="onDeviceLost"/>
/// <seealso cref="InputUserChange.DeviceRegained"/>
public event Action<PlayerInput> onDeviceRegained
{
add
{
if (value == null)
throw new ArgumentNullException(nameof(value));
m_DeviceRegainedCallbacks.AddCallback(value);
}
remove
{
if (value == null)
throw new ArgumentNullException(nameof(value));
m_DeviceRegainedCallbacks.RemoveCallback(value);
}
}
/// <summary>
/// If <see cref="notificationBehavior"/> is <see cref="PlayerNotifications.InvokeCSharpEvents"/>, this event
/// is triggered when the controls used by the players are changed.
/// </summary>
/// <remarks>
/// The callback is invoked when the set of <see cref="devices"/> used by the player change,
/// when the player switches to a different control scheme (see <see cref="currentControlScheme"/>),
/// or when the bindings used by the player are changed (e.g. when rebinding them). Also,
/// for <see cref="Keyboard"/> devices, the callback is invoked when the currently used
/// keyboard layout (see <see cref="Keyboard.keyboardLayout"/>) changes.
/// </remarks>
public event Action<PlayerInput> onControlsChanged
{
add
{
if (value == null)
throw new ArgumentNullException(nameof(value));
m_ControlsChangedCallbacks.AddCallback(value);
}
remove
{
if (value == null)
throw new ArgumentNullException(nameof(value));
m_ControlsChangedCallbacks.RemoveCallback(value);
}
}
////TODO: clarify the relationship to raycasting in the UI input module
/// <summary>
/// Optional camera associated with the player.
/// </summary>
/// <value>Camera specific to the player or <c>null</c>.</value>
/// <remarks>
/// This is <c>null</c> by default.
///
/// Associating a camera with a player is necessary only when using split-screen (see <see cref="PlayerInputManager.splitScreen"/>).
/// </remarks>
public
#if UNITY_EDITOR
// camera property is deprecated and only available in Editor.
new
#endif
Camera camera
{
get => m_Camera;
set => m_Camera = value;
}
#if PACKAGE_DOCS_GENERATION || UNITY_INPUT_SYSTEM_ENABLE_UI
/// <summary>
/// UI InputModule that should have it's input actions synchronized to this PlayerInput's actions.
/// </summary>
public InputSystemUIInputModule uiInputModule
{
get => m_UIInputModule;
set
{
if (m_UIInputModule == value)
return;
if (m_UIInputModule != null && m_UIInputModule.actionsAsset == m_Actions)
m_UIInputModule.actionsAsset = null;
m_UIInputModule = value;
if (m_UIInputModule != null && m_Actions != null)
m_UIInputModule.actionsAsset = m_Actions;
}
}
#endif
/// <summary>
/// The internal user tied to the player.
/// </summary>
public InputUser user => m_InputUser;
/// <summary>
/// The devices paired to the player.
/// </summary>
/// <value>List of devices paired to player.</value>
/// <remarks>
/// </remarks>
/// <seealso cref="InputUser.pairedDevices"/>
public ReadOnlyArray<InputDevice> devices
{
get
{
if (!m_InputUser.valid)
return new ReadOnlyArray<InputDevice>();
return m_InputUser.pairedDevices;
}
}
/// <summary>
/// Whether the player is missed required devices. This means that the player's
/// input setup is probably at least partially non-functional.
/// </summary>
/// <value>True if the player is missing devices required by the control scheme.</value>
/// <remarks>
/// This can happen, for example, if the a device is unplugged during the game.
/// </remarks>
/// <seealso cref="InputControlScheme.deviceRequirements"/>
/// <seealso cref="InputUser.hasMissingRequiredDevices"/>
public bool hasMissingRequiredDevices => user.valid && user.hasMissingRequiredDevices;
/// <summary>
/// List of all players that are currently joined. Sorted by <see cref="playerIndex"/> in
/// increasing order.
/// </summary>
/// <value>List of active PlayerInputs.</value>
/// <remarks>
/// While the list is sorted by <see cref="playerIndex"/>, note that this does not mean that the <see cref="playerIndex"/>
/// of a player corresponds to the index in this list. If, for example, three players join and then the second player leaves,
/// the list will contain one player with <see cref="playerIndex"/> 0 followed by one player with <see cref="playerIndex"/> 2.
/// </remarks>
/// <seealso cref="PlayerInputManager.JoinPlayer(int,int,string,InputDevice)"/>
/// <seealso cref="Instantiate(GameObject,int,string,int,InputDevice)"/>
public static ReadOnlyArray<PlayerInput> all => new ReadOnlyArray<PlayerInput>(s_AllActivePlayers, 0, s_AllActivePlayersCount);
/// <summary>
/// Whether PlayerInput operates in single-player mode.
/// </summary>
/// <value>If true, there is at most a single PlayerInput.</value>
/// <remarks>
/// Single-player mode is active while there is at most one PlayerInput (there can also be none) and
/// while joining is not enabled in <see cref="PlayerInputManager"/> (if one exists). See <see cref="PlayerInputManager.joiningEnabled"/>.
///
/// Automatic control scheme switching (if enabled) is predicated on single-player mode being active.
/// </remarks>
/// <seealso cref="neverAutoSwitchControlSchemes"/>
public static bool isSinglePlayer =>
s_AllActivePlayersCount <= 1 &&
(PlayerInputManager.instance == null || !PlayerInputManager.instance.joiningEnabled);
/// <summary>
/// Return the first device of the given type from <see cref="devices"/> paired to the player.
/// If no device of this type is paired to the player, return <c>null</c>.
/// </summary>
/// <typeparam name="TDevice">Type of device to look for (such as <see cref="Mouse"/>). Can be a supertype
/// of the actual device type. For example, querying for <see cref="Pointer"/>, may return a <see cref="Mouse"/>.</typeparam>
/// <returns>The first device paired to the player that is of the given type or <c>null</c> if the player
/// does not have a matching device.</returns>
/// <seealso cref="devices"/>
public TDevice GetDevice<TDevice>()
where TDevice : InputDevice
{
foreach (var device in devices)
if (device is TDevice deviceOfType)
return deviceOfType;
return null;
}
/// <summary>
/// Enable input on the player.
/// </summary>
/// <remarks>
/// Input will automatically be activated when the PlayerInput component is enabled. However, this method
/// can be called to reactivate input after deactivating it with <see cref="DeactivateInput"/>.
///
/// Note that activating input will activate the current action map only (see <see cref="currentActionMap"/>).
/// </remarks>
/// <see cref="inputIsActive"/>
/// <seealso cref="DeactivateInput"/>
public void ActivateInput()
{
m_InputActive = true;
// If we have no current action map but there's a default
// action map, make it current.
if (m_CurrentActionMap == null && m_Actions != null && !string.IsNullOrEmpty(m_DefaultActionMap))
SwitchCurrentActionMap(m_DefaultActionMap);
else
m_CurrentActionMap?.Enable();
}
/// <summary>
/// Disable input on the player.
/// </summary>
/// <remarks>
/// Input is automatically activated when the PlayerInput component is enabled. This method can be
/// used to deactivate input manually.
///
/// Note that activating input will deactivate the current action map only (see <see cref="currentActionMap"/>).
/// </remarks>
/// <see cref="ActivateInput"/>
/// <see cref="inputIsActive"/>
public void DeactivateInput()
{
m_CurrentActionMap?.Disable();
m_InputActive = false;
}
[Obsolete("Use DeactivateInput instead.")]
public void PassivateInput()
{
DeactivateInput();
}
/// <summary>
/// Switch the current control scheme to one that fits the given set of devices.
/// </summary>
/// <param name="devices">A list of input devices. Note that if any of the devices is already paired to another
/// player, the device will end up paired to both players.</param>
/// <returns>True if the switch was successful, false otherwise. The latter can happen, for example, if
/// <see cref="actions"/> does not have a control scheme that fits the given set of devices.</returns>
/// <exception cref="ArgumentNullException"><paramref name="devices"/> is <c>null</c>.</exception>
/// <exception cref="InvalidOperationException"><see cref="actions"/> has not been assigned.</exception>
/// <remarks>
/// The player's currently paired devices (see <see cref="devices"/>) will get unpaired.
///
/// <example>
/// <code>
/// // Switch the first player to keyboard and mouse.
/// PlayerInput.all[0]
/// .SwitchCurrentControlScheme(Keyboard.current, Mouse.current);
/// </code>
/// </example>
/// </remarks>
/// <seealso cref="currentControlScheme"/>
/// <seealso cref="InputActionAsset.controlSchemes"/>
public bool SwitchCurrentControlScheme(InputDevice[] devices)
{
if (actions == null)
throw new InvalidOperationException(
"Must set actions on PlayerInput in order to be able to switch control schemes");
// Find control scheme matching given devices in associated action asset
var scheme = InputControlScheme.FindControlSchemeForDevices(devices, actions.controlSchemes);
if (!scheme.HasValue)
return false;
var controlScheme = scheme.Value;
SwitchControlSchemeInternal(ref controlScheme, devices);
return true;
}
////REVIEW: these should just be SwitchControlScheme
/// <summary>
/// Switch the player to use the given control scheme together with the given devices.
/// </summary>
/// <param name="controlScheme">Name of the control scheme. See <see cref="InputControlScheme.name"/>.</param>
/// <param name="devices">A list of devices.</param>
/// <exception cref="ArgumentNullException"><paramref name="devices"/> is <c>null</c> -or- <paramref name="controlScheme"/> is
/// <c>null</c> or empty.</exception>
/// <remarks>
/// This method can be used to explicitly force a combination of control scheme and a specific set of
/// devices.
///
/// <example>
/// <code>
/// // Put player 1 on the "Gamepad" control scheme together
/// // with the second gamepad.
/// PlayerInput.all[0].SwitchControlScheme(
/// "Gamepad",
/// Gamepad.all[1]);
/// </code>
/// </example>
///
/// The player's currently paired devices (see <see cref="devices"/>) will get unpaired.
/// </remarks>
/// <seealso cref="InputActionAsset.controlSchemes"/>
/// <seealso cref="currentControlScheme"/>
public void SwitchCurrentControlScheme(string controlScheme, InputDevice[] devices)
{
if (string.IsNullOrEmpty(controlScheme))
throw new ArgumentNullException(nameof(controlScheme));
user.FindControlScheme(controlScheme, out InputControlScheme scheme); // throws if not found
SwitchControlSchemeInternal(ref scheme, devices);
}
public void SwitchCurrentActionMap(string mapNameOrId)
{
// Must be enabled.
if (!m_Enabled)
{
Debug.LogError($"Cannot switch to actions '{mapNameOrId}'; input is not enabled", this);
return;
}
// Must have actions.
if (m_Actions == null)
{
Debug.LogError($"Cannot switch to actions '{mapNameOrId}'; no actions set on PlayerInput", this);
return;
}
// Must have map.
var actionMap = m_Actions.FindActionMap(mapNameOrId);
if (actionMap == null)
{
Debug.LogError($"Cannot find action map '{mapNameOrId}' in actions '{m_Actions}'", this);
return;
}
currentActionMap = actionMap;
}
/// <summary>
/// Return the Nth player.
/// </summary>
/// <param name="playerIndex">Index of the player to return.</param>
/// <returns>The player with the given player index or <c>null</c> if no such
/// player exists.</returns>
/// <seealso cref="PlayerInput.playerIndex"/>
public static PlayerInput GetPlayerByIndex(int playerIndex)
{
for (var i = 0; i < s_AllActivePlayersCount; ++i)
if (s_AllActivePlayers[i].playerIndex == playerIndex)
return s_AllActivePlayers[i];
return null;
}