|
17 | 17 | using Object = UnityEngine.Object; |
18 | 18 | using Gyroscope = UnityEngine.InputSystem.Gyroscope; |
19 | 19 | using Is = UnityEngine.TestTools.Constraints.Is; |
| 20 | +using UnityEngine.InputSystem.OnScreen; |
20 | 21 |
|
21 | 22 | /// <summary> |
22 | 23 | /// Tests for <see cref="PlayerInput"/> and <see cref="PlayerInputManager"/>. |
@@ -663,6 +664,58 @@ public void PlayerInput_CanAutoSwitchControlSchemesInSinglePlayer() |
663 | 664 | })); |
664 | 665 | } |
665 | 666 |
|
| 667 | + [Test] |
| 668 | + [Category("PlayerInput")] |
| 669 | + public void PlayerInput_AutoSwitchControlSchemesInSinglePlayerWithOnScreenControl_AutoSwitchToTargetDeviceAndIgnoreMouse() |
| 670 | + { |
| 671 | + var keyboard = InputSystem.AddDevice<Keyboard>(); |
| 672 | + var mouse = InputSystem.AddDevice<Mouse>(); |
| 673 | + |
| 674 | + var go = new GameObject(); |
| 675 | + |
| 676 | + var onScreenButton = go.AddComponent<OnScreenButton>(); |
| 677 | + onScreenButton.enabled = false; |
| 678 | + onScreenButton.controlPath = "<Gamepad>/buttonSouth"; |
| 679 | + |
| 680 | + var playerInput = go.AddComponent<PlayerInput>(); |
| 681 | + playerInput.defaultControlScheme = "Keyboard&Mouse"; |
| 682 | + playerInput.defaultActionMap = "gameplay"; |
| 683 | + playerInput.actions = InputActionAsset.FromJson(kActions); |
| 684 | + |
| 685 | + Assert.That(playerInput.devices, Is.EquivalentTo(new InputDevice[] { keyboard, mouse })); |
| 686 | + |
| 687 | + // enable the OnScreenButton, it should switch to Gamepad |
| 688 | + onScreenButton.enabled = true; |
| 689 | + var gamepad = onScreenButton.control.device; |
| 690 | + Assert.That(gamepad, Is.TypeOf<Gamepad>()); |
| 691 | + Assert.That(playerInput.devices, Is.EquivalentTo(new InputDevice[] { gamepad })); |
| 692 | + Assert.That(playerInput.user.controlScheme, Is.Not.Null); |
| 693 | + Assert.That(playerInput.user.controlScheme.Value.name, Is.EqualTo("Gamepad")); |
| 694 | + |
| 695 | + // Perform mouse move and click. to try to switch to Keyboard&Mouse scheme |
| 696 | + Move(mouse.position, new Vector2(0.123f, 0.234f)); |
| 697 | + Click(mouse.leftButton); |
| 698 | + Move(mouse.position, new Vector2(100f, 100f)); |
| 699 | + InputSystem.Update(); |
| 700 | + |
| 701 | + // The controlScheme shouldn't have changed |
| 702 | + Assert.That(playerInput.devices, Is.EquivalentTo(new[] { gamepad })); |
| 703 | + Assert.That(playerInput.user.controlScheme, Is.Not.Null); |
| 704 | + Assert.That(playerInput.user.controlScheme.Value.name, Is.EqualTo("Gamepad")); |
| 705 | + |
| 706 | + // disabling the OnScreenButton to ensure that it will now switch to Keyboard&Mouse as expected |
| 707 | + onScreenButton.enabled = false; |
| 708 | + |
| 709 | + // Perform mouse move and click. to try to switch to Keyboard&Mouse scheme |
| 710 | + Move(mouse.position, new Vector2(0.123f, 0.234f)); |
| 711 | + Click(mouse.leftButton); |
| 712 | + Move(mouse.position, new Vector2(100f, 100f)); |
| 713 | + |
| 714 | + Assert.That(playerInput.devices, Is.EquivalentTo(new InputDevice[] { keyboard, mouse })); |
| 715 | + Assert.That(playerInput.user.controlScheme, Is.Not.Null); |
| 716 | + Assert.That(playerInput.user.controlScheme.Value.name, Is.EqualTo("Keyboard&Mouse")); |
| 717 | + } |
| 718 | + |
666 | 719 | [Test] |
667 | 720 | [Category("PlayerInput")] |
668 | 721 | public void PlayerInput_CanAutoSwitchControlSchemesInSinglePlayer_WithSomeDevicesSharedBetweenSchemes() |
|
0 commit comments